The Controller in the PHP framework can use constants in the configuration file at any time. The principle is to include the configuration file? Also, www. baidu. comindexdemo accesses the demo method in the index controller. Why can a URL instantiate a controller class? What is the principle? Thanks to the controllers in the PHP framework for using the constants in the configuration file at any time. The principle is to include the configuration file in?
There is also www.baidu.com/index/demo, which is the demo in the indexcontroller. to instantiate a controller class with a single URL, what is the principle? Thank you.
Reply content:
The Controller in the PHP framework can use constants in the configuration file at any time. The principle is to include the configuration file?
There is also www.baidu.com/index/demo, which is the demo in the indexcontroller. to instantiate a controller class with a single URL, what is the principle? Thank you.
If you can ask these two questions, it means that the subject thinks deeply during the use of the framework. I would like to give a thumbs up!
To get the answers to these two questions, you need to have a certain understanding of the nature of PHP. I am very happy if I can help you a little bit. 1. The content in the configuration file can be directly called anywhere in the code because the configuration file is
require
And assigned one or more variables.
Code can refer to: https://github.com/TinyLara/TinyLara/blob/master/bootstrap.php#L11
As a script language, PHP sends a string (Instruction) to a processor, and then the processor executes the corresponding action according to the instruction. The PHP framework with multiple files also runs in this way,require
,include
Or__autoload
Finally, a huge PHP code is combined and executed by the interpreter to obtain the result. The content of the configuration file will be assigned to a public variable in this huge PHP code, which is always available throughout the entire life process.
2. PHP code can indeed instantiate classes through URLs, or determine the URL content to determine which class to be loaded and which function to execute.
PHP can load files while executing code. Different files can be loaded based on different URLs. Currently, most frameworks use PHP automatic loading, such__autoload()
The magic method automatically loads the file of a class when calling a class in the code.
What you ask is
Routing
Nature, directly look at the code: https://github.com/TinyLara/TinyRoute/blob/master/TinyRoute.php#L86
In the above Code$last = 'HomeController@home'
.
I have to lament that PHP is really a string art!
Before PHP5, If PHP frameworks need to implement automatic class loading, they generally implement a traversal directory according to certain conventions and automatically load all classes or functions that comply with the agreed rules. Of course, PHP5's previous object-oriented support was not very good, and classes were not frequently used. After PHP5, when a PHP class is loaded, the Zend engine automatically calls the _ autoload function if the class file is not included or the class name is incorrect. You need to implement the _ autoload function by yourself. After PHP5.1.2, you can use the spl_autoload_register function to customize automatic loading of processing functions. If this function is not called, The spl_autoload function customized by SPL is used by default.
Instantiating a controller class or any other class does not have much to do with URL.
The actual logic is like this:
Assume that a url is as follows:http://your.domain.com/path/to/your/file
After receiving the request, the web server searchesyour.domain.com
Where is the root directory corresponding to this domain name?/www
.
The actual requested file is/www/path/to/your/file
. If this file isphp
Type. The web server submits the filePhp interpreter
Then return the result to the browser.
A single portal project only maps all requests to a similar project through web server configuration.index.php
. It is parsed by the program logic.url
The following parameters determine which controller to instantiate or call other methods.
The second question, you are right. The essence isinclude
Yes, yii is used as an example. Its configuration file is an array that loads the configuration file from the beginning of the framework.
When a controller/method is requested, the controller is searched for based on the autoload registered by the Framework. If the controller is found, it is referenced. If the method is not found, an exception is thrown.
I wrote the configuration information in an interface class,
Then load the class through spl_authload,
When calling, the class is inherited first, and then self: xxx directly
First, bind path context and controller class. If the access path matches a path context, the controller class will be instantiated.