- Yii 1.1 Application Development Cookbook
- Alexander Makarov
- 226字
- 2021-04-02 18:41:01
Using base controller
In many frameworks, the concept of a base controller that is being extended by other ones is described right in the guide. In Yii, it is not in the guide as you can achieve flexibility in many other ways. Still, using base controller is possible and can be useful.
Getting ready
A new application using yiic webapp
is to be set up.
Let's say we want to add some controllers that will be accessible only when the user is logged in. We can surely set this constraint for each controller separately, but we will do it in a better way.
How to do it...
- First, we will need a base controller that our user-only controllers will use. Let's create
SecureController.php
inprotected/components
with the following code:<?php class SecureController extends Controller { public function filters() { return array( 'accessControl', ); } public function accessRules() { return array( array('allow', 'users'=>array('@'), ), array('deny', 'users'=>array('*'), ), ); } }
- Now, go to the Gii controller generator and enter
SecureController
into theBase Class
field. You will get something like this:class TestController extends SecureController { public function actionIndex() { $this->render('index'); } … }
- Now, your
TestController
index will be only accessible if the user is logged in, even though we have not declared it explicitly in theTestController
class.
推薦閱讀
- 3ds Max 2014標(biāo)準(zhǔn)教程(全視頻微課版)
- IBM Cognos 8 Report Studio Cookbook
- 剪映AI視頻剪輯:AI腳本+AI繪畫+圖文生成+數(shù)字人制作
- iPad+Procreate室內(nèi)設(shè)計(jì)手繪表現(xiàn)技法
- AutoCAD入門教程全掌握
- Unity 2020游戲開發(fā)快速上手
- 行攝 Photoshop CC后期修片高手之道(第2版)
- Photoshop后期強(qiáng):多重曝光專業(yè)技法寶典
- UML 2.0 in Action: A project/based tutorial
- Deep Inside osCommerce: The Cookbook
- 中文版3ds Max 2020基礎(chǔ)教程
- 夢幻森林Procreate童話風(fēng)插畫繪制專業(yè)技法
- Moodle 2.0 for Business Beginner's Guide
- Photoshop圖像處理與制作
- Final Cut Pro視頻后期剪輯零基礎(chǔ)入門到精通