Mu-NET, my YII2 study notes (Basic article)

Source: Internet
Author: User

First, about PHP knowledge points

1, Namespace: Storage of the space of the class file, can be arbitrarily defined, the recommendation and class file directory consistent, convenient management.

Note: (1), if the class does not have a namespace set, the default is the top-level namespace, which can be instantiated in the program in a way.

$model = new \class ()

(2), if the introduction of the same class name, you can use the code to distinguish.

 use a\Class;  use b\Class as B_class;

2. Session:

Each browser stores the site's session with a PHPSESSID that corresponds to the name of the server that stores the session file.

Second, about the Yii knowledge point

Tutorial 3-2: Controller Request processing

When you get the Get/post parameter, you can set the second parameter to the default value if it does not exist.

$request = Yii:: $app->request;
$request->get ("id", 10);

Learn to read documents first.

$request->userip; Getting the user's IP address is a bit bad.

$userHost = Yii:: $app->request->userhost; Returns the example.com host name in the URL.







/* Request method is PUT */}

Tutorial 3-3: Controller response Processing

Official Document Address: Http://www.yiichina.com/doc/guide/2.0/runtime-responses

// two common applications for corresponding components: jump and download $response // add a "jump" to the header file with the corresponding component $this // the $this method calls the Yii encapsulated redirect method to implement the "Jump" $response // saves the requested data as an attachment and can be downloaded $response // download the specified file by responding to the assembly-encapsulated Sendfile () method, typically by calling the file specified in the index.php portal file to find the file by './filename '

Tutorial 3-4: Session Handling of the controller

The class that implements the Arrayaccess interface can be used as an array

Different browsers will produce different sessions, the system is based on SessionID to identify

//1. Calling the session component    $session= \yii::$app-session;//2. Determine if the session is openif($session-isActive) {Echo"Session is not acive";}//3. Open Session    $session-Open;//4. Set Session value    $sessionSet (' User ', ' Zhang San '));//5. Get Session value    Echo $session-Get (' user ');//6. Delete Session value    $session-Remove (' user ');

//manipulating the session by array mode$session[' user '] = "Zhang San";//Set Session valueEcho $session[' User '];//Remove Session Dataunset($session[' user ']);//Delete Session by unset

Tutorial 3-5: The controller's cookie processing

1. Obtaining Cookies

$cookies = Yii::$app->request->cookies; $user $cookies->getvalue ("User", "value"); // The second parameter is a default value


if (isset ($cookies [' language '])) {$language = $cookies [   

if ($cookies->has (' language ')) ...
if (isset ($cookies [' language '] ) ...

2. Set Cookies

//get a collection of cookies from the "Response" component (Yii\web\cookiecollection)$cookies= Yii::$app->response->cookies;//add a new cookie in the response to be sent$cookies->add (New\yii\web\cookie ([' Name ' = ' language ', ' value ' = ' ZH-CN ',]));//Delete a cookie$cookies->remove (' language ');//equivalent to the following delete codeunset($cookies[' language ']);

3. Verification of Cookies

Settings in the configuration file, it is recommended that you do not place them in the version controller.

return [' components ' + =        [            ' request ' = ' = ' cookievalidationkey ' + ' fill in a '      Secret key here ',        ],    ],];

Mu-NET, my YII2 study notes (Basic article)

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.