Php Framework Development 2 (SPL library and controller)

Source: Internet
Author: User
Tags autoloader spl
Php Framework Development 2 (SPL library and controller) is based on the preceding directory structure (if not clear, refer to the previous article ). In the simple folder, I created a simple. php. require 'shortdes/exceptions. php'; require 'include php Framework Development 2 (SPL library and controller)

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.

 

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.