Basic Learning notes for yii2.0

Source: Internet
Author: User
Tags regular expression mysql database yii

Yii2.0 study notes are messy. Let's take a look.

1. Receive parameters

$ Request = \ yii: $ app-> request;
$ Request-> get ('A', 'defval') receives the parameters of a get request.
$ Request-> post ('A', 'defval') receives the parameters of a post request
$ Request-> isGet determines whether the request is a get request.
$ Request-> userIp get the visitor's ip

2. Response to the header

$ Resp = \ YII: $ app-> response;
$ Resp-> statusCode = 404;
$ Resp-> headers-> add ('pragm', 'no-cache ');
$ Resp-> headers-> set ('pragm', 'Max-age = 5 ');
$ Resp-> headers-> remove ('pragm ');
$ Resp-> headers-> add ('location', 'http: // www.baidu.com ');
$ This-> redirect ('http: // www.baidu.com ', 302 );
File download problems
$ Resp-> headers-> add ('content-... ', 'attachment ...');
$ Resp-> sendFile ('./index. Php ');

3. session component

$ Ses = \ YII: $ app-> session;
$ Ses-> open (); // enable session
Var_dump ($ ses-> isActive); // check whether sesson is enabled
$ Ses-> set ('name', 'reson'); // sets a session
$ Ses ['name'] = 'reson'; // you can use this method to set a session.
Echo $ ses-> get ('name'); // get the session
Echo $ ses ['name']; // you can use this method to obtain the session.
$ Ses-> remove ('name'); // deletes a session.
Unset ($ ses ['name']); // This method can also be used to delete a session.

4. cookie

$ Cook = \ YII: $ app-> response-> cookies;
$ CookData = array ('name' => 'name', 'value' => 'reson1 ');
$ Cook-> add (new Cookie ($ cookData); // add a cookie. To modify the Cookie, just change the information in $ cookData and add it again.
$ Cook-> remove ('name'); // delete a cookie
$ CookGet = \ YII: $ app-> request-> cookies;
Echo $ cookGet-> getValue ('name1', 'defval'); // obtain the value in the cookie. If no value exists, the default value in the second parameter is returned.
Cookie encryption: cookieValidationKey in config/web. php

5. Rendering Template

Return $ this-> renderPartial () and return $ this-> render ()
<? Php echo $ this-> render ('index', array ('KK '=> 'test. Php')?> // Introduce other templates to the template

<? = $ This-> blocks ['block1']?>
<? Php $ this-> beginBlock ('block1');?>
<H1> overwrite the specified region in the global template <? Php $ this-> endBlock ();?>

6. Basic concepts

I. Portal file
Entry file content: The general format is as follows:
 
$ Yii = dirname (_ FILE _). '/.../../framework/Yii. Php'; // location of the Yii framework
$ Config = dirname (_ FILE _). '/protected/config/main. Php'; // location of the main configuration FILE of the current application
 
// Remove the following line when deploying the formal environment
// Defined ('yii _ debug') or define ('yii _ debug', true); // whether to run in DEBUG mode
 
Require_once ($ Yii); // contains the Yii Framework
Yii: createWebApplication ($ config)-> run (); // create and run an application instance based on the main configuration file. You can use Yii: app () to access this instance anywhere in the current application.
 
 
7. Main configuration file

Save location: your application/protected/config/main. php
File content: The general format is as follows:
Return array (
'Basepath' => dirname (_ FILE _). DIRECTORY_SEPARATOR. '..', // The absolute physical path of the current application root directory
'Name' => 'yii Blog demo', // name of the current application
 
// Pre-load the log application component, which indicates that the application component will be created no matter whether it is accessed or not. The parameter configuration of this application is set in the array with the keyword "components" below.
'Preload' => array ('log'), // log is the component ID
 
// Automatically loaded models and component classes
'Import' => array (
'Application. models. * ', // load all model classes in the "application/models/" folder
'Application. components. * ', // load all application component classes in the "application/components/" folder
),
 
'Defaultcontroller' => 'post', // sets the default controller class.
 
// Configure the component of the current application. For more components available for configuration, see "core application components" below"
'Components' => array (
'User' => array (// user) component configuration, "user" is the component ID
// You can use cookie-based authentication.
'Allowautologin' => true, // allow automatic logon
),
'Cache' => array (// cache component
'Class' => 'cmcache', // cache component class
'Servers' => array (// MemCache Cache server configuration
Array ('host' => 'server1', 'port' => 11211, 'weight' => 60), // cache server 1
Array ('host' => 'server2', 'port' => 11211, 'weight' => 40), // cache server 2
),
),
'DB' => array (// db (database) component configuration, "db" is the component ID
'Connectionstring' => 'sqlite: protected/data/blog. Db', // The DSN string connecting to the database
'Tableprefix' => 'tbl _ ', // data table prefix
),
// If you want to use a MySQL database, cancel the comment below
/*
'DB' => array (
'Connectionstring' => 'MySQL: host = localhost; dbname = blog', // connect to the mysql database
'Default' => true,
'Username' => 'root', // MySQL database username
'Password' => '', // MySQL database user password
'Charset' => 'utf8', // MySQL database encoding
'Tableprefix' => 'tbl _ ', // MySQL database table prefix
),
*/
'Errorhandler' => array (
// Display the error using the actionError method in the SiteController controller class
'Erroraction' => 'site/error', // The operation that runs when an error occurs. The controller name and method name are both in lower case and separated by the slash "/"
),
// URL routing manager
'Urlmanager' => array (
'Urlformat' => 'path', // URL format. Two formats are supported: 'path' format (for example,/path/to/EntryScript. php/name1/value1/name2/value2 ...) and 'get' format (for example:/path/to/EntryScript. php? Name1 = value1 & name2 = value2 ...). When using the 'path' format, you need to set the following rules:
'Rules' => array (// URL rule. Syntax: <parameter name: Regular expression>
'Post // '=> 'Post/view', // point post/12/helloword to post/view? Id = 12 & title = helloword
'Posts/'=> 'Post/Index', // point posts/hahahaha to post/index? Tag = hahahaha
'/' => '/',
),
),
'Log' => array (// record
'Class' => 'clogrouter', // class for processing record information
'Routes '=> array (
Array (
'Class' => 'cfilelogroute ', // class for handling error messages
'Levels' => 'Error, warning', // error level
),
// To display the error message on the webpage, cancel the comment below.
/*
Array (
'Class' => 'cweblogroute ',
),
*/
),
),
), // The application component configuration is complete.
 
 
// Use Yii: app ()-> params ['parameter name'] to access parameters at the application layer
'Params' => require (dirname (_ FILE _). '/params. Php '),
);

Related 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.