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

问题描述

我想知道是否可以在 AppController 和我应用的其他控制器之间添加另一个抽象控制器?

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

这样我的控制器,例如UsersController 扩展了 SecureController和 SecureController 扩展了 AppController.

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

我还希望能够让其他控制器直接扩展 AppController:SomeNonSecureController 扩展了 AppController.

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

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

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.

我想将所有 beforeFilter 安全性内容都放到 SecureController 中 - 这样任何需要安全性的控制器都可以简单地扩展它,而其他控制器则直接从 AppController 继承.

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?

提前致谢,肯.

推荐答案

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

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.

如果这不合适,那么我会选择你的路线,为了做到这一点,只需 include('secure_controller.php');在文件中的单个控制器类声明之前.

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.

我通过创建一个 BaseController 做了类似的事情,我在我所有的项目中都使用了它,它提供了我所有的标准管理 CRUD 操作.然后我让我的 AppController 扩展它,它包含特定于应用程序的控制器范围的功能,然后各个控制器扩展它,最终实际上是空的.我所做的就是:

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 抽象到另一个层次,可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 17:17