PHP Framework Development II (SPL Library and Controller) _php tutorial

Source: Internet
Author: User
Tags autoloader spl php framework
According to the directory structure of the words (if you are not clear can see an article). I created a new simple.php within the simple folder.

[PHP]

Require ' includes/exceptions.php ';
Require ' includes/autoloader.php ';
Session_Start ();

$view = new View ();
Lib::set_item (' controller ', new Controller ());
Lib::get_item (' controller ', Lib::P Ersist_storge)->render ();
$content = $view->finish ();


This chapter is mainly about SPL, so we only look at the second line of autoloader.php. When we join in the index.php

[PHP]
Require ' simple/simple.php ';
After.

Let's take a look at the source code of autoloader.php

[PHP]
Class autoloader{

public static function Includesautoloader ($class) {
$path = defined (' Simple_path ')? Simple_path: $_server[' document_root '];
$file _name = $path. '/includes/'. $class. '. php ';
if (is_readable ($file _name))
Require $file _name;
}

public static function Modulesautoloader ($class) {
$path = defined (' Simple_path ')? Simple_path: $_server[' document_root '];
$file _name = $path. '/modules/'. $class. '. php ';
if (is_readable ($file _name))
Require $file _name;
}

public static function Controllerautoloader ($class) {
$path = defined (' Simple_path ')? Simple_path: $_server[' document_root '];
$file _name = $path. '/controller/'. $class. '. php ';
if (is_readable ($file _name))
Require $file _name;
}

}

Spl_autoload_register (' Autoloader::includesautoloader ');
Spl_autoload_register (' Autoloader::modulesautoloader ');
Spl_autoload_register (' Autoloader::controllerautoloader ');

This class is a static method, when the PHP file is referenced, the program will automatically load the contents of these three folders, so that we do not have to repeat the required or include. If you do not understand the SPL, you can go directly to Baidu, here just to tell you, this SPL role is quite wide.

Then create a new lib.php within the includes folder

[HTML]
Class lib{

Const Setting_array = true;
Const Persist_storge = false;

public static function Set_item ($name, $value, $is _array = False) {
if ($is _array) {
$_session[$name] = array ();
$_session[$name] [] = $value;
}
else{
$_session[$name] = $value;
}
}

public static function get_item ($name, $persist = True) {
$result = null;
if (Isset ($_session[$name])) {
$result = $_session[$name];
}
if (! $persist) {
Unset ($_session[$name]);
}
return $result;
}

public static function sendto ($url = ") {
if (empty ($url)) {
$url = '/';
}
Die (the header (' Location: '. $url));
}
}
This class is currently only used to set session-related content.

When executed to Lib::set_item (' controller ', new Controller ()), a new control object is created here and the object is stored in the session.

controller.php

[PHP]
Class controller{
URL section
protected $parts;
Method parameters
protected $params;

Public Function __construct () {
$this->parts = Array ();
$this->analysis ();
}

Public Function analysis () {
$path _info = $_server[' path_info ');
if (substr ($path _info, 0, 1) = = '/') {
$path _info = substr ($path _info, 1);
}
$parts = explode ('/', $path _info);
if (Emptyempty ($parts [0])) $parts [0] = ' index ';
if (Emptyempty ($parts [1])) $parts [1] = ' demo ';

$this->parts = $parts;
Array_shift ($parts);
Array_shift ($parts);
$this->params = $parts;
}

Public function render () {
if (!class_exists ($this->parts[0])) {
throw new Controllerdoesntexistsexception ($this->parts[0]. ' Not exists! ');
}
if (!method_exists ($this->parts[0], $this->parts[1])) {
throw new Actiondoesntexistsexception ($this->parts[0]. ' of '. $this->parts[1]. ' Not exists! ');
}

$new _controller = new $this->parts[0];
$called = Call_user_func_array (Array ($new _controller, $this->parts[1]), $this->params);
if ($called = = = False) {
throw new Actionfailedexception ($this->parts[0]. ' of '. $this->parts[1]. ' Failed to Excute property! ');
}
}
}

This class only uses the PATH_INFO mode to get the called Controller and Action, and passes the arguments to the appropriate action method. When the controller initializes, the contents of the Path_info are automatically parsed,

Lib::get_item (' controller ', Lib::P Ersist_storge)->render ();

The call to render () will then find the Controller and action in the appropriate controller directory.

For example, I create a new index.php within the Controller folder

[PHP]
Class index{

Public Function demo () {
echo "SDFSDF";
}
}
Then execution Http://localhost/index.php/index/demo will output "SDFSDF";(This is my local path).

The above code is not difficult, if you do not understand the classmate, please q me, you can also go to the PHP official online to find the use of the corresponding function.

The next section is a simple introduction to the view.
Author: Tomyjohn

http://www.bkjia.com/PHPjc/478065.html www.bkjia.com true http://www.bkjia.com/PHPjc/478065.html techarticle according to the directory structure of the words (if you are not clear can see an article). I created a new simple.php within the simple folder. [PHP] Prequire includes/exceptions.php; Require includes/ ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.