- 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.
推薦閱讀
- Photoshop后期強:數碼攝影后期完全寶典
- Entity Framework Tutorial
- Django 1.2 E/commerce
- Adobe創意大學Illustrator產品專家認證標準教材(CS6修訂版)
- ASP.NET MVC 2 Cookbook
- 中文版Photoshop CC平面設計實用教程
- Indesign平面排版技術應用
- PPT設計與制作實戰教程
- 好用,Excel數據處理高手
- 中文版SolidWorks 2018完全實戰技術手冊
- Revit建模進階標準教程(實戰微課版)
- 卷珠簾:Photoshop古風插畫技法完全教程
- 中文版3ds Max 2014基礎培訓教程
- Premiere視頻編輯案例教程:Premiere Pro 2020(微課版·第2版)
- 同花順軟件操作技巧與實戰指南