Controllers and routes

Source: Internet
Author: User

1. Controller as suffix, inherit from Yii\web\controller

2. Action is prefixed with action, public access adornment

3. Controller ID go to the class name suffix, all to lowercase, if the hump name more than one word, to the middle bar separated

4. Action ID go to prefix, all to lowercase, if the hump name more than one word, to the middle bar separated.

5. How the route points to the action Controller ID, action ID

6.URL access rule index.php?r= routing parameters using & parameter = value method

7. The default Action ID index is defined in Yii\base\controller:: $defaultAction

URL generation

A demonstration of how the URL helper class works

Use Yii\helpers\url;

Other action in the same controller

Echo url::toroute ([' Update ', ' id ' = ' 1 ']);//If there are no arguments, you can pass character channeling, do not need an array

Action for different controllers (same module)

echo url::toroute (' Post/index ');

Absolute path (you can jump out of the current module, if you start the module, you need to add the module ID in front)

echo url::toroute ('/site/index ');

Action that consists of multiple words in the current controller, such as Actionuserinfo

echo url::toroute (' User-info '); Controller Similar

URL with a domain name

echo url::toroute (' Site/index ', true);

Route aliases

echo url::toroute (' @register ');//Routing alias, you need to register aliases first, such as Yii::setalias (' @register ', ' site/register ');

Stitching image, CSS, etc.

Echo Url::base (); /project/web

Ehco Url:base (TRUE); Http://localhost/project/web

Echo url::to (); Empty is the time to return the current URL, including the parameter

echo url::to (array)//equivalent to Url::toroute (array)

Echo url::to ()

Jump

return $this->redirect ([' foot-bar/user-info ']);

Route rewriting

Open the config file in web.php.

/*
' Urlmanager ' = [
' Enableprettyurl ' = true, //remove? r=
' Showscriptname ' = False, //Remove index.php requires Apache to open rewrite module and Add. htaccess
' Rules ' = [
],
],
*/
And Apache opens the rewrite module and adds the. htaccess

Small point:

Ajax requests with renderpartial (do not load the tail of the head), usually with render on the line

Controllers and routes

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.