Yii Framework Learning Notes (ii) integrating HTML front-end templates into the framework

Source: Internet
Author: User
Tags yii

7 Reasons to choose the Yii 2.0 version Framework http://blog.chedushi.com/archives/8988

just contact Yii to talk about the views and feelings of the YII framework http://bbs.csdn.net/topics/390807796

More content Baidu: Yii Front End

http://my.oschina.net/u/1472492/blog/221085

Summary

Yii Framework Learning Notes (ii) integrating HTML front-end templates into the framework original address: http://www.ldsun.com/1309.html

The previous section successfully introduced the YII framework and generated the project folder shop to be carried out.

Go to the protected folder and start the integration tour of the HTML template;

The protected folder needs to be aware of the Controller,models and views three folders, which is exactly the MVC pattern, the key to integrating HTML templates with the YII framework is this. The controller is the controller, the file in the director can set the class method, and then the class method to access the view in the corresponding views.

For example, the controller has sitecontroller.php:

<?phpClassSitecontrollerExtendsController{/** * Declares class-based actions. */PublicfunctionActions(){ReturnArrayCaptcha action renders the CAPTCHA image displayed on the contact page ' Captcha ' =-->array (' Class ' =' Ccaptchaaction ',' BackColor ' =0xFFFFFF,),Page action renders "static" pages stored under ' protected/views/site/pages 'They can accessed Via:index.php?r=site/page&view=filename' Page ' =Array' Class ' =' Cviewaction ',),); }/** * This is the default ' index ' action, which is invoked * if an action was not explicitly requested by users. */PublicfunctionActionindex(){Renders the view file ' protected/views/site/index.php 'Using the default layout ' protected/views/layouts/main.php '$this->render (' Index '); }/** * This was the action to handle external exceptions. */PublicfunctionActionerror(){if ($error =yii::app ()->errorhandler->error) {if (Yii::app ()->request->isajaxrequest)Echo $error [' Message ';Else$this->render (' Error ', $error); } }/** * Displays the contact page * *PublicfunctionActioncontact(){$model =New Contactform;IfIsset ($_post[' Contactform ']) {$model->attributes=$_post[' Contactform '];if ($model->validate ()) {$name ==? UTF-8? B? '. Base64_encode ($model->name).'? = '; $subject ==? UTF-8? B? '. Base64_encode ($model->subject).'? = '; $headers ="From: $name <{$model->email}>\r\n"."Reply-to: {$model->email}\r\n"."Mime-version:1.0\r\n"."Content-type:text/plain; Charset=utf-8 "; Mail (Yii::app ()->params[' AdminEmail '], $subject, $model->body, $headers); Yii::app ()->user->setflash (' Contact ',' Thank for contacting us. We'll respond to as soon as possible. ');$this->refresh (); } }$this->render (' Contact ',Array' Model ' = $model)); }/** * Displays the login page * *PublicfunctionActionlogin(){$model =New LoginForm;If it is AJAX validation requestIfIsset ($_post[' Ajax ']) && $_post[' Ajax ']===' Login-form ') {echo cactiveform::validate ($model); Yii::app ()->end (); }Collect user input dataIfIsset ($_post[' LoginForm ']) {$model->attributes=$_post[' LoginForm '];Validate user input and redirect to the previous page if validif ($model->validate () && $model->login ())$this->redirect (Yii::app ()->user->returnurl); }Display the Login form $this->render ( ' login ', array (/** * logs  out the current user and redirect to homepage. */ public  function actionlogout ()  { yii::app () User->logout ();   $this->redirect (Yii::app ()->homeurl);  }}   

You can see that there are many class methods in this controller, and by default, this method means that the view is index.php in the public function actionIndex() $this->render(‘index‘); rendered Views/site folder.

This understanding of the opening of the first page of the content of the reason:

The Web site enters from the shop/index.php portal file, and then accesses the default controller sitecontroller.php, which accesses the default method Actionindex (), and then accesses index.php in the views view to render first page content.

To figure this out, it is easier to integrate HTML templates into the framework, first create the desired controller, then define the method in the controller, and let this method jump to the corresponding template file in the Views view. For example, to integrate a homepage template into the framework, create a new controller indexcotroller.php (you can also use the default Sitecontroller controller), and define the method Actionindex (), Let it access the index.php file in view views, this index.php file is our homepage template.

indexcotroller.php:

<?phpClassIndexcontrollerExtendsController{Publicfunction  actionindex< span class= "PHP" > ()   {        //  render the following views   ' protected/views/index/ index.php '           $this --->render (< span class= "PHP" > ' index ' ?>       

If it is a new controller, such as Indexcotroller, you need to create the corresponding folder in views, such as index (the Sitecontroller controller corresponding to the View folder is site), Then the corresponding view folder to build your class method access to the file can be, as in this example method Actionindex () corresponding to the $this->render (' index '); Access views/index/ index.php, this integrates the HTML template and Yii with the controller controllers and views view--MV mode.

views/index/index.php:

How to access the integrated home page after integration? If accessed directly, that would jump to the default controller Sitecontroller, instead of our own defined Indexcotroller controller, you would need to use a route to access the question:

Http://localhost/shop/shop/index.php?r=index/index

In this route, http://localhost/shop/shop/is the root directory of the Web site, index.php is the portal file in the Shop folder, R=index indicates that the controller is indexcotroller,/ Index represents the index method in the Indexcotroller controller, and the access result is the effect we achieve:

Of course, you can create other controllers, such as the landing page to create a Usercontroller controller. For the same controller, such as Indexcotroller, you can create multiple methods in indexcotroller.php that correspond to different pages in the views/index/. Follow the principle of routing when you visit:

HTTP//Website domain name/index.php?r= Controller/method

Same thing, so you can do it. Various HTML front-end templates are integrated into the YII framework, where you also need to pay attention to the style file Css,js and the location of the picture. Previously said shop folder under the Assets folder for static resources, such as css,js,img, so put these resources into the assets folder, where the integration is the front page, JS General before and after the common, CSS and IMG general front and rear separation, So you can take the following directories:

This completes the front-end HTML template and the Yii integration.

"Note": In the template introduced Css,js,img, etc. due to the path problem and error prone, the trick is to set the path in the/protected/config/constants.php constant, just call it:

After you set up the static resource directory, you also need to introduce the constants.php configuration file into the portal file to make it effective.

Summarize:

1. Homepage Template integration with YII framework:

1. Create a Indexcontroller controller

2. Create a View views/index/index.php

3. Controller call View $this->render (index);

4. Introduce CSS and pictures, put the style directory and the picture directory into constant, unified call.

2. Routing

Inside the frame we get the controller and the method through the route

We have a controller approach that allows you to further interact with the view or model.

HTTP//URL/index.php?r= Controller/method

3. The controller and view are well understood

Controller: Indexcontroller (name Controller)

Method: Actionindex (action name)

views/below There are many directories, the directory principle is: Each controller name under the views of the corresponding name of the directory exists, inside the specific template files stored

Schematic diagram of PS:MVC design mode:

Original address: http://www.ldsun.com/1309.html

© Copyright belongs to the author
    • Category: logbook
    • Words:1547

Yii Framework Learning Notes (ii) integrating HTML front-end templates into the framework

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.