First, Model1, what is the model
- Models are part of the MVC pattern and are objects that represent business data, rules, and logic
- Model classes can be defined by inheriting Yii\base\model or its subclasses (ActiveRecord), and the base class Yii\base\model supports many useful features
- The model class is also the base class for more advanced models such as active record activity records.
2. Yii\base\model supports the following practical features:
Properties : Performance of business data that can be accessed like a normal class property or array (e.g. Public $name)
Property Label : Specifies the label that the property displays (for example: Models\contactform class,function Attributelabels () {} method, which defines the attribute tag)
Public function Attributelabels () { return [ // All Verifycode properties are displayed as verification in the page Code// All of the name properties are displayed as "name" two characters in the same polygon );}
3, Block assignment value: Support one step to assign values to many attributes;
$model->load (Yii::$app# Use the Load () method for block assignment, $app->request->post () Get all the data submitted by the front desk.
4, validation rules: ensure that data data conforms to the stated validation rules (for example, in the Models\contactform class, method defined inside)
public function rules () { return [[[ ' name ', ' email ', ' subject ', ' body '], ' required '], //
name, email, subject and body are required fields [' Name ', ' Integer '], // name must be an integer [' name ', ' compare ', ' comparevalue ' =>30, ' Operator ' = ' >= '], // name >=30 [' Email ', ' email ', // Email must be a legitimate email address [' Verifycode ', ' captcha '], // Verification code must be filled in correctly Span style= "COLOR: #000000" >];}
Validation uses the yii\base\model::validate () method to trigger data validation, and if data validation fails, the yii\base\model::haserrors () property to true ,
You can use Yii \base\model::geterrors () to view error details. e.g:
<? PHP $model = new Entryform (); $model , name = ' Carroll ' ; $model -e-mail = ' wrong ' ; if ( $model -> validate ()) { // validation successful }else { // validation failed//using $ Error = GetErrors (), $model; Var_dump ($errors); Get error details. }
- Yii\base\model for the parent class of the normal model class and is not related to the data table
- Yii\base\activerecord is used for the parent class of the normal model class and is related to the data table
- Yii\base\activerecord also inherits Yii\base\model, but adds database processing
5. The association between table and table ( Many-to-one or single -story, e.g. articles corresponding to article status, using HasOne () )
/* * * establish the link between the article class and the article state class, establish this connection, the equivalent of the original post class is a property status0, this attribute value is the object of the article State class * get state name can be used: $thePost Status0->name to get */publicfunction getStatus0 () { return$ this// Poststatus::classname is the table name of the article Status table, [' id ' = ' status '] is the condition of the Association }
(One -to-many , for example articles corresponding to article comments, using hasmany () )
/* * * Post class has a commets attribute, this property is an array, the array is composed of comment objects * Get comments using: $Post-and comments to get, but it is an object array, if you want to access a specific object, You need to use foreach to traverse * /publicfunction getcomments () { return$this->hasmany (Comment::classname (), [' post_id ' = ' id ']); }
Second, View1, layout files
- A layout is a special view that represents the public part of multiple views, using views/layouts/main.php by default
2. How to create a layout
- Layouts are also views, which can be created as normal views
- Layouts are stored by default under @app \view\layouts
3. How to change the layout
- @app \view\layouts\main.php layout file is used by default
- Replace the layout in the entire class : public $layout = ' DCR ';
- Replace The layout of one page: Add $this, layout = ' DCR ' in the method;
- If a page doesn't need any layout, just set the $this in the method ->layout=false;
4. layout file data
- Two predefined variables $this and $content
- As with $this in the view,$this to an instance of Yii\web\view
- $content is the result of the controller rendering , the results of each page are different.
5. Structure of layout file
<?php$this->beginpage ()?>//called at the beginning of the layout $this point to Yii\web\view to manage and render this view file to identify the beginning of an HTML file$this-Header()?>//called at the head tag to identify the location of the HTML file header$this->beginbody ()?>//called at the beginning of the body to identify the starting position of the body part of the HTML file<?php$this->endbody ()?>//called at the beginning of the body to identify the end position of the body part of the HTML file</body>$this->endpage ()?>//called at the end of the layout to identify the end of an HTML file
6, using <?=html::encode ($variable)?> can prevent malicious JavaScript XSS (cross-site scripting) attack, is to use Htmlspecialchars () to convert the pre-defined characters to HTML entities
Htmlspecialchars ($content, Ent_quotes | Ent_substitute, ' UTF-8 ', true);
- Ent_quotes-encode double and single quotation marks
- Ent_substitute-Replaces the invalid encoding with a specified Unicode substitution character u+fffd (UTF-8) or & #FFFD; character, instead of returning an empty string.
7, the application of render in view:
$this->render (' _form ', [ $model,?>
- _form is a file in the same directory that is designed to display the form
- Model is an array of objects queried from the database
Three, Controller1, controller
- The c part of the MVC structure
- Inherited from Yii\base\controller.
- Specifically, you get the data you need from the model object, and then call the view template file to render the data, and then generate the output response information
2. Composition
- The controller mainly has the movement composition
- Actions are methods in the Controller class
- One action corresponds to a page
3. Render ()
View method: View the render () method from all yii\web\controller\ methods in the class reference manual
public string render ($view, $params =[])
- $view refers to the view name
- $params is the data passed to the view
- Returns a string, which is the result of rendering good
- The exception that is thrown when the view or file is not saved
4, the method of the controller
- Yii uses the action prefix to differentiate between common methods and operations. The action behind the action is mapped to the ID of the action
- If the method name consists of multiple words, the view name is concatenated with hyphens, for example:
Public function Actionsayhello ($message= ' Hello ') { return$this Render (' Say-hello ', [' message ' = +$message]);
5, Yii\web\request::p Ost () method is responsible for collecting user-submitted data
Public functionActionentry () {$model=NewEntryform; if($model->load (Yii::$app->request->post ()) &&$model-Validate ()) {return $this->render (' entry-confirm ', [' model ' =$model]); }Else{return $this->render (' entry ', [' model ' =$model]); }}
- Yii:: $app Represents an application instance, a globally accessible singleton that provides components for specific functions such as request, response, DB , etc.
Note: This article for the author (44106-kangaroo) after reading Wei teach you to learn Yii2.0 video after the note, if reproduced please indicate the source: http://www.cnblogs.com/chrdai/p/8004737.html
YII2 Basic version of the MVC section