Introduction to YII directory structure, portal files, and routing settings _php instance

Source: Internet
Author: User
Tags naming convention php class yii

Project name from "Fantacms" modified to become "TESTYII" —————— vulgar!

1, the project directory structure analysis

2,yii Entry File Analysis

When Yii launches the project, the project master configuration file array is passed, and Yii binds a global Yii object throughout the application and can invoke the object in the following ways: Yii::app ()

The YII system binds the array values in the configuration file to the object through the form of a key value pair, for example, in the configuration file we configured:

' Name ' => ' My Web application ',

Then we can get the value of "name" from the object's properties at any point in the project by:

Yii::app ()->name;

3,yii Routing Settings

You should also know that for the controller name, Yii first detects whether the current controller name is a "module name" and, if it is a module name, navigates to the module first.

The "module" will be explained when the project is built later.

Through the above route to access is: sitecontroller.php class files in the Sitecontroller class in the Actionindex method

The controller file is located in: Protected/controllers directory, which is our controller file storage directory

Note the controller in Yii and the way the name of the action method, the controller has a unified suffix "Controller", the action method also has a unified prefix "action", and also requires the action method name of the naming convention with "except the first word of every other word to capitalize the first letter"

Because Yii's default controller name is: site

The default action name is: Index

Therefore, the above path and direct access through the specified controller name action name are the same for the http://localhost/testyii/effect

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: Protected/views/site directory

Where: Site is the corresponding Controller name folder, each controller name in the view should have a unique folder name and its corresponding

Then, in the action method, the ' index ' is used to specify that the specific view file displayed is the ' index.php ' view file specified under the site controller

Also need to note:

There are two ways to call a view:

$this->render----> will call the template file

And

$this->renderpartial-----> does not call template files

The difference between them is also mentioned above.

5, view template settings

Open the sitecontroller.php file, where the code screenshot is as follows:

We found that in YII applications, each controller is inherited from the public controller "Controller"

Then open the "Controller" Controller file: controller.php, which is located in: Protected/components directory

The "Controller" controller code screenshot is as follows:

Yii through: Public $layout = '//layouts/column1 '; To specify the public template file for the action method

The public template files are located in: Protected/views/layouts directory, as shown in the following figure:

Now we're going to create our template file: testlayout.php, the code is as follows:

where "<?php echo $content;?>" is the content substitution method in the template file specified in Yii

Then, modify the template file in the "Controller" controller as: public $layout = '//layouts/testlayout ';

Then visit: Http://localhost/testyii/index.php?r=site/index results as shown:

Then we find that the template file has become our own designation, and then if you don't need a view file to render the template file, you can use the view file in the action method: $this->renderpartial method

Or you don't need to call the template file for the entire project, you can call the view file in the action method all: $this->renderpartial

Alternatively, set the view template file to null, for example: public $layout = ';

Continue in the next section: Yii Magician: gii,yii modules and Modules customization

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.