Home > Article > Backend Development > Yii introductory tutorial directory structure, entry file and routing settings, yii introductory tutorial_PHP tutorial
The project name was changed from "FantaCMS" to "testyii"——————Vulgar!
1. Project directory structure analysis
2, Yii entry file analysis
When Yii starts the project, the project main configuration file array is passed, then Yii will bind a global Yii object in the entire application and the object can be called through the following method: Yii::app()
The Yii system binds the array value in the configuration file to the object in the form of key-value pairs. For example, in the configuration file we configured:
'name'=>'My Web Application',
Then we can get the value of "name" through the properties of the object anywhere in the project, the method is:
Yii::app()->name;
3, Yii routing settings
At the same time, you should know: For the controller name, Yii will first detect whether the current controller name is a "module name", and if it is a module name, it will first locate the module.
"Module" will be explained later when building the project.
What is accessed through the above route is: the actionIndex method in the SiteController class under the SiteController.php class file
The controller file is located in the protected/controllers directory, which is the directory where our controller files are stored
Pay attention to the way controller files and action method names are written in Yii. Controllers have a unified suffix "Controller", and action methods also have a unified prefix "action". At the same time, the naming convention of action method names is required to comply with "Except the first The first letter of every word except 1 must be capitalized "
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 and action name has the same effect as 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, the view file is located in the protected/views/site directory
Where: site is the corresponding controller name folder. Each controller name should have a unique folder name corresponding to it in the view
Then use ‘index’ in the action method to specify that the specific view file to be displayed is the specified ‘index.php’ view file under the site controller
Also note:
There are two ways to call a view:
$ this-& gt; render ---- & gt; will call the template file
and
$this->renderPartial -----> Template file will not be called
The differences between them are also as mentioned above.
5, View template settings
Open the SiteController.php file and the code screenshot is as follows:
We found that: in Yii applications, every controller must inherit from the public controller "Controller"
Then open the "Controller" controller file: Controller.php, which is located in the protected/components directory
The screenshot of the "Controller" controller code is as follows:
Yii uses: public $layout='//layouts/column1'; to specify the public template file of the action method
The public template file is located in the protected/views/layouts directory, as shown below:
Now let’s create our template file: testlayout.php, the code is as follows:
The "" 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 The result is as shown below:
Then we found that the template file has become our own specification, and if you do not need the view file to render the template file, then you can use the $this->renderPartial method when calling the view file in the action method.
Or if your entire project does not need to call template files, then you can use all of them when calling view files in the action method: $this->renderPartial
You can also set the view template file to "empty", for example: public $layout='';
Continued in the next section: Yii’s Magician: gii, Yii modules and module customization