Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the entry points of YiiFramework

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what are the entry knowledge points of YiiFramework". In daily operation, I believe many people have doubts about what are the entry knowledge points of YiiFramework. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "what are the entry knowledge points of YiiFramework"! Next, please follow the small series to learn together!

Create a Yii app skeleton

web is the root directory of the website

yiic webapp /web/demo

Note when creating models and CURDs with GII

1. Model Generator Operation

Even if there is a table prefix, fill in the full name of the table in Table Name, that is, include the table prefix. As shown below:

2. Crud Generator operation

In this interface, fill in the model name in Model Class. Capital letters. You can also refer to file names generated by model generator in the procted/models directory when generating models. As shown below:

If CURD controllers are generated for the three tables news, newstype, and statustype, enter News, newsType, and StatusType in Model Generator and Model Class. Case is the same as the case of the file name created. If you write NEWS or NeWs, you can't.

Module creation considerations

Modules are created through GII, and Module IDs are generally lower-case. Regardless, the ID filled in here determines the configuration in the main.php configuration file. As follows:

'modules'=>array ('admin '=>array(//admin in this line is Module ID. Consistent with Module ID italicized when creating Module 'class'=>'application.modules.admin. AdminModule',//admin here doesn't matter in Windows OS case, but it's best to be consistent with the actual directory. ),),

routing

system represents the framework directory of the yii framework

application represents the protected directory under the created application (for example, d:\wwwroot\blog).

application.modules.Admin.AdminModule

Represents the AdminModules.php file in the Admin directory under the modules directory under the application directory (e.g. d:\wwwroot\blog\protected)(actually pointing to the class name of the file)

system.db.*

Represents all files under db directory under framework directory under YII framework.

accessRules description in controller

/** * Specifies the access control rules. * This method is used by the 'accessControl' filter. * @return array access control rules */public function accessRules(){ return array( array('allow', // allow all users to perform 'index' and 'view' actions 'actions'=>array ('index ',' view'),//indicates that any user can access index, view methods 'users'=>array ('*'),//indicates any user ), array('allow', // allow authenticated user to perform 'create' and 'update' actions 'actions'=>array ('create ',' update'),//indicates that only authenticated users can operate the create, update methods 'users'=>array ('@'),//indicates authenticated users ), array('allow', // allow admin user to perform 'admin' and 'delete' actions 'actions'=>array ('admin ',' delete'),//indicates that only admin can access admin, delete methods 'users'=>array ('admin '),//indicates the specified user, here user: admin ), array('deny', // deny all users 'users'=>array('*'), ), );}

See the code comments above.

user: represents the user session information. See API: CWebUser for details

CWebUser represents the persistent state of a Web application.

CWebUser as an application component with ID user. Therefore, user status can be accessed from anywhere via Yii::app()->user

public function beforeSave(){ if(parent::beforeSave()) { if($this->isNewRecord) { $this->password=md5($this->password); $this->create_user_id=Yii::app()->user->id;//Start with User::model()->user->id;(Error) //$this->user->id;(error) $this->create_time=date('Y-m-d H:i:s'); } else { $this->update_user_id=Yii::app()->user->id; $this->update_time=date('Y-m-d H:i:s'); } return true; } else { return false; }}

getter method or/and setter method

2. Display the content in the controller and pass it to the view through the second parameter of render.

The controller method contains:

$theTime=date("Y-m-d H:i:s");$this->render('helloWorld',array('time'=>$theTime));

View file:

The copy code is as follows:

The second argument to the render() method called is an array(array type), the render() method extracts the values from the array and supplies them to the view script, and the key in the array is the variable name supplied to the view script. In this example, the array key is time and the value is $theTime and the extracted variable name $time is for the view script. This is one way to pass controller data to the view.

Views and controllers are very close brothers, so $this in the view file refers to the controller that renders the view. Modify the previous example to define a class public property in the controller, rather than a local variable, whose value is the current date and time. Then access the properties of this class in the view via $this.

View naming conventions

Name the view file as ActionID. Remember, however, that this is only a recommended naming convention. In fact, the view file name does not have to be the same as ActionID, just need to pass the file name as the first parameter to render().

DB correlation

$Prerfp = Prerfp::model()->findAll( array( 'limit'=>'5', 'order'=>'releasetime desc' ));$model = Finishrfp::model()->findAll( array( 'select' => 'companyname,title,releasetime', 'order'=>'releasetime desc', 'limit' => 10 );foreach($model as $val){ $noteArr [] = " 在".$ val->title. "Bidding,".$ val->companyname. "Win the bid. ";}$model = Cgnotice::model()->findAll ( array( 'select' => 'status,content,updatetime', 'condition'=> 'status = :status ', 'params' => array(':status'=>0), 'order'=>'updatetime desc', 'limit' => 10 ));foreach($model as $val){ $noticeArr[] = $val->content;}$user=User::model()->find('LOWER(username)=? ',array($username));$notetype = Dictionary::model()->find(array ('condition ' => '`type` = "notetype"');//find the line with postID=10 $post=Post::model()->find ('postID =: postID', array (': postID'=>10));

You can also use $condition to specify more complex query conditions. Instead of using strings, we can make $condition an instance of CDbCriteria, which allows us to specify conditions other than WHERE. For example:

$criteria=new CDbCriteria;$criteria->select='title';//select only 'title' column $criteria->condition='postID=: postID';$criteria->params=array (': postID'=>10);$post=Post::model()->find($criteria); // $params Not required

Note that when CDbCriteria is used as a query condition, the $params parameter is no longer needed because it can be specified in CDbCriteria, as above.

An alternative to CDbCriteria is to pass an array to the find method. The keys and values of the array correspond to the attribute names and values of the criterion, respectively. The above example can be rewritten as follows:

$post=Post::model()->find(array( 'select'=>'title', 'condition'=>'postID=:postID', 'params'=>array(':postID'=>10),));

other

1. Links

The copy code is as follows:

Find API documentation specifically: CHtml link() method

The copy code is as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report