Php Framework Development 2 (SPL library and controller) is based on the preceding directory structure (if not clear, refer to the previous article ). I created a simple. php. requireincludesexceptions. php; requireincludesautoloader. php; session_start (); $ viewnew php Framework Development 2 (SPL library and controller) in the simple folder)
According to the above directory structure (if not clear, you can refer to the previous article ). I created a simple. 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::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
require 'simple/simple.php';
Later.
Let's take a look at the source code of autoloader. php.
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
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
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 (empty ($ parts [0]) $ parts [0] = 'index'; if (empty ($ parts [1]) $ parts [1] = 'demo'; $ this-> parts = $ 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]. ''. $ 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]. ''. $ 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.
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.