- Yii 1.1 Application Development Cookbook
- Alexander Makarov
- 549字
- 2021-04-02 18:41:01
Using external actions
In Yii, you can define controller actions as separate classes and then connect them to your controllers. This way, you can reuse some common functionality.
For example, you can move backend for autocomplete fields to an action and save some time by not having to write it over and over again.
Another simple example that we will review is deleting a model.
Getting ready
- Set up a new application using
yiic webapp
. - Create a DB schema with the following script:
CREATE TABLE `post` ( `id` int(10) unsigned NOT NULL auto_increment, `created_on` int(11) unsigned NOT NULL, `title` varchar(255) NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `user` ( `id` int(10) unsigned NOT NULL auto_increment, `username` varchar(200) NOT NULL, `password` char(40) NOT NULL, PRIMARY KEY (`id`) );
- Generate
Post
andUser
models using Gii.
How to do it...
- Let's write a usual delete action for posts first, as follows:
class PostController extends CController { function actionIndex() { $posts = Post::model()->findAll(); $this->render('index', array( 'posts' => $posts, )); } function actionDelete($id) { $post = Post::model()->findByPk($id); if(!$post) throw new CHttpException(404); if($post->delete()) $this->redirect('post/index'); throw new CHttpException(500); } }
We have defined two actions. One lists all posts and another deletes a post specified if it exists and redirects back to
index
action. - Now, let's do the same in a separate action class. Create
DeleteAction.php
in yourprotected/components
directory as follows:class DeleteAction extends CAction { function run() { if(empty($_GET['id'])) throw new CHttpException(404); $post = Post::model()->findByPk($_GET['id']); if(!$post) throw new CHttpException(404); if($post->delete()) $this->redirect('post/index'); throw new CHttpException(500); } }
- Let's use it inside our controller. Delete
actionDelete
, we will not need it anymore. Then, add theactions
method:class PostController extends CController { function actions() { return array( 'delete' => 'DeleteAction', ); } … }
- OK. Now, we are using external delete action for post controller, but what about the user controller? To use our
DeleteAction
withUserController
we need to customize it first. We do this as follows:class DeleteAction extends CAction { public $pk = 'id'; public $redirectTo = 'index'; public $modelClass; function run() { if(empty($_GET[$this->pk])) throw new CHttpException(404); $model = CActiveRecord::model($this->modelClass)->findByPk($_GET[$this->pk]); if(!$model) throw new CHttpException(404); if($model->delete()) $this->redirect($this->redirectTo); throw new CHttpException(500); } }
- Now, we can use this action for both post controller and user controller. For post controller, we do this as follows:
class PostController extends CController { function actions() { return array( 'delete' => array( 'class' => 'DeleteAction', 'modelClass' => 'Post', ); ); } … }
- For user controller, we do this as follows:
class UserController extends CController { function actions() { return array( 'delete' => array( 'class' => 'DeleteAction', 'modelClass' => 'User', ); ); } … }
- This way, you can save yourself a lot of time by implementing and reusing external actions for tasks of a similar type.
How it works...
Every controller can be built from external actions like a puzzle from pieces. The difference is that you can make external actions very flexible and reuse them in many places. In the final version of DeleteAction
, we defined some public properties. As DeleteAction
is a component, we can set its properties through config. In our case, we pass config into the actions
controller method used to add actions to a module.
There's more…
For further information, refer to the following URLs:
- PS是這樣玩的:輕松掌握 Photoshop 通關(guān)秘籍
- 社會科學(xué)數(shù)據(jù)處理軟件應(yīng)用
- 云化虛擬現(xiàn)實(shí)技術(shù)與應(yīng)用
- Midjourney AI繪畫藝術(shù)創(chuàng)作教程:關(guān)鍵詞設(shè)置、藝術(shù)家與風(fēng)格應(yīng)用175例
- 我為PS狂 Photoshop照片處理一分鐘秘笈
- 邊做邊學(xué):Photoshop+CorelDRAW綜合實(shí)訓(xùn)教程
- Drools JBoss Rules 5.0 Developer's Guide
- SketchUp/Piranesi印象彩繪表現(xiàn)項(xiàng)目實(shí)踐
- Photoshop CS6實(shí)戰(zhàn)基礎(chǔ)培訓(xùn)教程(全視頻微課版)
- Elasticsearch實(shí)戰(zhàn)與原理解析
- 剪映短視頻剪輯與運(yùn)營全攻略:視頻剪輯+音頻處理+后期特效+運(yùn)營管理
- Lighttpd
- 中文版Photoshop CC2018從入門到精通(第4版)
- Designing and Implementing Linux Firewalls and QoS using netfilter, iproute2, NAT and l7/filter
- 零基礎(chǔ)學(xué)會聲會影2018(全視頻教學(xué)版)