Javascript-php in the framework of the controller can always use the constant in the configuration file, the principle is to load the configuration file in it?

Source: Internet
Author: User
The controller in the PHP framework can always use the constant in the configuration file, the principle is the configuration file include in it?

and Www.baidu.com/index/demo, access to the index controller in the demo method, why a URL can be instantiated to instantiate a controller class, what is the principle? Thank you

Reply content:

The controller in the PHP framework can always use the constant in the configuration file, the principle is the configuration file include in it?

and Www.baidu.com/index/demo, access to the index controller in the demo method, why a URL can be instantiated to instantiate a controller class, what is the principle? Thank you

Can ask these two questions, explain the main problem in the use of the framework of the process of deep thinking, first praise one!

To get the answers to these two questions, we need to have a certain understanding of the nature of PHP, and I would like to expand on that. I would be happy if I could help you a little bit.

1. The contents of the configuration file can be called directly anywhere in the code because the configuration file is require entered and assigned to one or more variables.

Code can refer to: https://github.com/TinyLara/TinyLara/blob/master/bootstrap.php#L11

PHP as a scripting language, the essence of execution is to send a string of strings (instructions) to a processor, and then the processor according to the instructions to perform the corresponding action. A multi-file PHP framework is also run in this way, or, finally, the require include combination of __autoload a huge long PHP code, executed by the interpreter, results. The contents of the configuration file are assigned to a public variable in the huge PHP code, which is available throughout the life process.

2. PHP code can actually instantiate a class through a URL, or you can judge the URL content to determine which class to load and which function to execute.

PHP can load files while executing code, and can choose different files to load according to the different URLs. Most frameworks now use automatic loading of PHP, such as the __autoload() magic method, which automatically loads the file where a class is called in code.

The essence of what you are asking 路由 , look directly at the code: https://github.com/TinyLara/TinyRoute/blob/master/TinyRoute.php#L86

In the code above $last = 'HomeController@home' .

Have to sigh, PHP is really the art of string!

Before PHP5, each PHP framework, if it was to implement the automatic loading of classes, would typically implement a class or function that iterates through the directory and automatically loads all files that conform to the Convention rules by some Convention. Of course, PHP5 's previous support for object-oriented is not very good, and the use of classes is not very frequent now. After PHP5, the Zend engine calls the __autoload function automatically when the PHP class is loaded, if the file is not included in the class, or if the class name is wrong. This function requires the user to implement the __autoload function themselves. After the PHP5.1.2 version, you can use the Spl_autoload_register function to customize the auto-load handler function. When this function is not called, the SPL custom spl_autoload function is used by default.

    1. Instantiating a controller class or any other class is not much related to URLs.

      The actual logic is probably this:
      Suppose such a URL:http://your.domain.com/path/to/your/file
      After the Web server receives the request, it searches for the your.domain.com root of the domain, assuming it is /www .
      So, the actual requested file is /www/path/to/your/file . If the file is php of type, the Web server will give the file to the php解释器 explanation and return the result to the browser.

      Single-entry projects, just through the Web server configuration, all the requests are mapped to a similar index.php entry file. Then the logic of the program to parse the url following parameters, then decide which controller to instantiate, or call other methods.

    2. The second question, you are right, the essence is stillinclude

Yes, already yii, for example, his profile is an array, and the configuration file is loaded at the beginning of the framework.
When requesting a controller/method, it will find the controller according to the framework's registered AutoLoad, find the reference, continue to find the method, cannot find the report exception.

I wrote the configuration information in an interface class,
And then load this class through Spl_authload,
When calling, inherit this class first, and then directly self::xxx

The path context and controller class are bound first, and if the access path matches a path context, the controller class is instantiated.

  • 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.