Php Framework Development 2 (SPL library and controller ). According to the above directory structure (if not clear, you can refer to the previous article ). In the simple folder, I created a simple. php. [php] prequireincludesexceptions. php; requireexcepdes based on the directory structure (if you are not clear about it, refer to the previous article ). I created a simple. php.
[Php]
Require 'Des/exceptions. php ';
Require 'events des/autoloader. php ';
Session_start ();
// $ View = new view ();
Lib: set_item ('controller', new controller ());
Lib: get_item ('controller', lib: PERSIST_STORGE)-> render ();
// $ Content = $ view-> finish ();
This chapter focuses on SPL, so we only look at the autoloader. php in the second line. when we add it to index. php
[Php]
Require 'simple/simple. php ';
Later.
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. '/utilities des/'. $ 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 this php file is referenced, the program will automatically load the content in these three folders, so we do not need to repeat required or include. If you are not familiar with SPL, you can directly go to Baidu. here, we will only tell you that this SPL has a wide range of functions.
Then create a new lib. php
[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 (header ('Location: '. $ url ));
}
}
This class is currently only used to set SESSION-related content.
When lib: set_item ('controller', new controller (); is executed, a new controller object is created and saved to the SESSION.
Controller. php
[Php]
Class controller {
// URL
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 obtain the called controller and ACTION, and passes the parameter to the corresponding ACTION method. After the controller is initialized, the content of PATH_INFO is automatically analyzed,
Lib: get_item ('controller', lib: PERSIST_STORGE)-> render ();
Then, call render () to find the controller and ACTION in the corresponding controller directory.
For example, I create an index. php file in the controller folder.
[Php]
Class index {
Public function demo (){
Echo "sdfsdf ";
}
}
Execute http: // localhost/index. php/index/demo to output "sdfsdf"; (this is my local path ).
The above code is not very difficult. if you do not understand it, please ask me, or go to the PHP official website to find the corresponding function usage.
The next section briefly introduces the view.
Author: tomyjohn
Else (if you are not clear about it, refer to the previous article ). I created a new simple. php. [php] prequire shortdes/exceptions. php; require shortdes/... in the simple folder /...