Cakephp:抽象AppController另一个层次,可能吗? [英] Cakephp: Abstracting AppController another level, possible?

查看:171
本文介绍了Cakephp:抽象AppController另一个层次,可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能在AppController和我的应用程序的其他控制器之间添加另一个抽象控制器。



这样我的控制器UsersController扩展了SecureController
和SecureController扩展了AppController。



还有我想让其他控制器直接扩展AppController:
SomeNonSecureController扩展AppController。 / p>

这是因为我当前的AppController在其beforeFilter有各种Auth和ACL的东西,但我也有控制器不需要安全的东西(在一切需要之前安全,没有添加新规格)..
但是因为有些控制器确实需要它,所以将代码复制粘贴到所有需要的控制器是没有意义的。



我想把所有的beforeFilter安全性的东西变成一个SecureController - 这种方式任何需要安全简单的控制器扩展它,而其他的继承自AppController直接。



您会如何继续这样做?



提前感谢,
Ken。

解决方案

我的第一个想法是看看是否可以从beforeFilter抽象一些功能到一个组件 - 记住组件可以使用其他组件,只需将它们包含在组件$ components属性,所以你可以访问AuthComponent和AclComponent等。



如果这不合适,我会去你的路线,为了做到,只是include('secure_controller.php');



通过创建一个BaseController,我在我的所有项目中使用它提供了所有我的管理CRUD操作标准。然后我有我的AppController扩展这包含应用程序特定的,控制器广泛的功能,然后个别控制器扩展,并最终几乎空。我所做的是:

  // app / base_controller.php 
<?php class BaseController extends Controller {} ?>

// app / app_controller.php
<?php
include('base_controller.php');
class AppController extends BaseController {}
?>

// app / controllers / my_controller.php
<?php class MyController extends AppController {}?>


I was wondering if it's somehow possible to add another abstraction controller between AppController and my app's other controllers?

So that my controllers, e.g. UsersController extends SecureController and SecureController extends AppController.

Also I want to be able to have other controllers extend AppController directly: SomeNonSecureController extends AppController.

this is because my current AppController has all sorts of Auth and ACL stuff in its beforeFilter, but i also have controllers that don't need that security stuff (before everything needed the security, no new specs have been added).. but because some many controllers do need it, it doesn't make sense to copy-paste the code to all needy controllers.

I was thinking to but all the beforeFilter security stuff into a SecureController - that way any controllers that need security simpley extend it, while others inherit from AppController directly.

How would you go on about doing something like this?

Thanks in advance, Ken.

解决方案

My first thoughts would be to see if I could abstract some of the functionality from the beforeFilter into a component - remember components can use other components too, just include them in your component's $components property, so you can access the AuthComponent and AclComponent etc.

If this was not suitable then I'd go for your route, in order to do it, just include('secure_controller.php'); before your individual controller class declaration in it's file.

I have done something similar by creating a BaseController that I use in all my projects which provides all my admin CRUD actions that are standard. I then have my AppController extend this which contains application specific, controller wide functionality, then individual controllers extend that, and end up being practically empty. All I do is:

// app/base_controller.php
<?php class BaseController extends Controller {} ?>

// app/app_controller.php
<?php
include('base_controller.php');
class AppController extends BaseController {}
?>

// app/controllers/my_controller.php
<?php class MyController extends AppController {} ?>

这篇关于Cakephp:抽象AppController另一个层次,可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆