In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to understand Yii directory structure, entry files and routing settings". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand Yii directory structure, entry files and routing settings.
1. Analysis of project directory structure
Analysis of the entry documents of 2jinyii
When Yii starts the project, passing an array of project master profiles, Yii binds a global Yii object throughout the application and can call it in the following way: Yii::app ()
The Yii system binds the array values in the configuration file to the object in the form of key-value pairs, for example, in the configuration file we configure:
'name'= >' My Web Application'
Then we can get the value of "name" anywhere in the project through the properties of the object by:
Yii::app ()-> name
3Gen Yii routing settings
You should also know that for controller names, Yii first detects whether the current controller name is a "module name", and if so, navigates to the module first.
The "module" will be explained later when the project is built.
Access via the above route is the actionIndex method in the SiteController class under the SiteController.php class file
The controller file is located in: protected/controllers directory, that is, our controller file storage directory
Pay attention to the way the controller file and the action method name are written in Yii. The controller has a unified suffix "Controller" and the action method has a unified prefix "action". At the same time, the naming convention of the action method name conforms to "capitalize the first letter of every word except the first word".
Because the default controller name of Yii is: site
The default action name is: index
Therefore, the above path accessed by specifying the controller name action name is consistent with the direct access: http://localhost/testyii/
4, view call
In the action method, call: $this- > render ('index')
To specify the view file for the corresponding action method, which is located in the: protected/views/site directory
Where: site is the corresponding controller name folder, and each controller name should have a unique folder name corresponding to it in the view
Then in the action method, use 'index' to specify that the specific view file displayed is the specified' index.php' view file under the site controller.
In addition, it is important to note:
There are two ways to call a view:
$this- > render-> invokes template files
And
$this- > renderPartial-> will not call the template file
The difference between them is also mentioned above.
5, View template Settin
Open the SiteController.php file, and the code screenshot is as follows:
We find that in Yii applications, each controller inherits from the common controller "Controller".
Then open the "Controller" controller file: Controller.php, which is located in the: protected/components directory
Screenshots of the "Controller" controller code are as follows:
Yii specifies the public template file for the action method through: public $layout='//layouts/column1';
The common template file is located in: protected/views/layouts directory, as shown below:
Now let's create our template file: testlayout.php, the code is as follows:
Where "" is the content replacement method in the template file specified in Yii.
Then, modify the template file in the "Controller" controller to: public $layout='//layouts/testlayout'
Then visit: http://localhost/testyii/index.php?r=site/index results as shown in the figure:
Then we found that the template file has become our own specification, and if you don't need the view file to render the template file, you can use the $this- > renderPartial method when calling the view file in the action method.
Or you don't need to call the template file for the entire project, then you can use it all when calling the view file in the action method: $this- > renderPartial
Or set the view template file to empty, for example: public $layout=''
At this point, I believe you have a deeper understanding of "how to understand the Yii directory structure, entry files and routing settings". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un