- 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.
推薦閱讀
- 中文版AutoCAD 2015實用教程
- Moodle 2.0 E/Learning Course Development
- GIMP 2.6 cookbook
- 四時風月繪:國風水墨CG插畫繪畫技法
- Flash CC從入門到精通
- 24小時全速學會Photoshop 2021
- AutoCAD 2018中文版基礎教程
- NetLogo多主體建模入門
- Python Testing: Beginner's Guide
- Elgg Social Networking
- 三維建模與3D打印從入門到精通
- 剪映+Vlog+Premiere短視頻制作從新手到高手
- Maya 2020 超級學習手冊
- Photoshop CC入門與提高
- Practical Data Analysis and Reporting with BIRT