CakePHP 最佳实践:使用或不使用路由的管理

发布于 2024-09-09 07:58:20 字数 223 浏览 12 评论 0原文

我正在对在 CakePHP 1.2 下构建的 CakePHP 应用程序进行彻底修改。我已升级到 1.3,并正在考虑放弃我的应用程序的管理路由范例。我发现我的一些控制器由于前端和管理的重复功能而变得非常大。我的直觉是,创建一组管理控制器并将管理路由全部删除会更干净,但我想了解其他人正在做什么以及我将错过哪些功能(如果有)丢弃路由。

在这方面,健壮的 CakePHP 应用程序(或其他 MVC 框架)的最佳实践是什么?

I'm working on an overhaul of a CakePHP app I built under CakePHP 1.2. I've upgraded to 1.3 and am considering moving away from the admin routing paradigm for my application. I'm finding that some of my controllers are getting extremely large because of duplicate functions for front end and admin. My intuition is that it is much cleaner to just create a set of admin controllers and drop the admin routing all together, but I wanted to get input on what others are doing and what, if any, functionality I'm going to miss out on dropping the routing.

What are considered best practices for a robust CakePHP app (or other MVC framework) in this regard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

失退 2024-09-16 07:58:20

我建议简单地将前端应用程序和管理分成两个单独的应用程序(/app/admin)。只需将 admin 视为一个简单的前端应用程序,负责处理数据库的所有“脏”工作。

通过这样做,您将能够使用 URL 中的 /admin 前缀访问您的管理员,或将 DocumentRoot 设置为 /admin/webroot 并使用子域(即 admin.myapp.com)访问管理员。

为了避免模型代码重复,您可以将模型放入某个共享文件夹(即 /vendors/core/models)中,并将此路径添加到 bootstrap.php 文件中的模型路径中(App::build('models' => array(VENDORS . 'core/models/')) 对于 CakePHP 1.3,$modelPaths = array(VENDORS . 'core/models/ ')(对于 CakePHP 1.2)。

要向模型添加更多管理或应用程序特定的内容,您可以通过加载核心模型并扩展它来扩展 /models 中的核心模型:

App::import('Model', 'CoreModelName');

class CustomCoreModelA extends CoreModelA
{
    function specificMethod() {}
}

这可以用于共享组件、行为等。

I'd suggest to simply separate front-end application and admin into two separate applications (/app and /admin). Just think of admin as a simple front-end application, doing all the "dirty" work with the database.

By doing so, you'll be able to access your admin using /admin prefix in URL or set DocumentRoot to /admin/webroot and access admin using subdomain (i.e. admin.myapp.com).

To avoid model code duplication, you could put your models into some shared folder (i.e. /vendors/core/models) and add this path to model paths in bootstrap.php files (App::build('models' => array(VENDORS . 'core/models/')) for CakePHP 1.3, $modelPaths = array(VENDORS . 'core/models/') for CakePHP 1.2).

To add more admin or app specific stuff to your models, you could extend your core models in /models, by loading core model and extending it:

App::import('Model', 'CoreModelName');

class CustomCoreModelA extends CoreModelA
{
    function specificMethod() {}
}

That could be made for shared components, behaviors, etc.

迷途知返 2024-09-16 07:58:20

我使用或不使用管理路由构建应用程序,并且不使用管理路由的版本总是一团糟。如果您的某些方法相同,您可以执行以下操作。

function add(){
$this->_add();
}

function admin_add(){
$this->_add();
}

function _add(){
... your code ...
}

我敢打赌,并非所有代码都是相同的,并且不使用管理路由,您最终会得到很多代码 if(... is admin ...) { echo 'blaa'} else {回显'foo'; }

ive built apps using admin routing and not, and the not version is always a mess. if some of your methods are the same you can do the following.

function add(){
$this->_add();
}

function admin_add(){
$this->_add();
}

function _add(){
... your code ...
}

i would bet its not all your code that is the same, and not using admin routing you will end up with a lot of code doing if(... is admin ...) { echo 'blaa'} else { echo 'foo'; }

痕至 2024-09-16 07:58:20

如果管理路由不适合您的场景,请不要打扰它。我也不使用它,管理路径不适合我的应用程序。重复代码完全是浪费精力。

您可以对细粒度角色使用 ACL 规则,或者简单地在控制器的 beforeFilter() 或操作的第一行中检查角色(管理标志)。

我有一个组件函数 checkRole(array()),它在我的操作的第一行中调用。如果当前用户不具有提供的角色,则会记录并终止请求。

Do not bother with admin routing if it does not fit your scenario. I'm also not using it, admin paths do not fit my app. Duplicating code is totally waste of effort.

You can use ACL rules for fine grained roles, or simply check role (admin flag) in controller's beforeFilter() or in the first line of an action.

I have a Component function checkRole(array()), that is called in the first line of my actions. If the current user does not have the supplied roles, it logs and terminates the request.

涙—继续流 2024-09-16 07:58:20

我会其次使用 ACL/角色来进行真正的管理工作,并且可能不会在生产中使用管理路由。有时,我会为低级管理内容保留一个脚手架(因此需要最少的额外代码)管理路由,只有我可以访问,但这在强大的生产应用程序中可能并不明智。

评论后编辑:这不是最佳选择,但您可以将一些内容放在一起,使其在 URL 中看起来像您想要的那样,并且还可以将其组织到文件夹中。我还无法测试它,但想法如下:

在控制器文件夹中创建一个文件夹“admin”,并为用户管理员创建一个 users_admin_controller.php 控制器文件。它们折叠了文件夹结构,因此您仍然不能与根目录具有相同的名称,但您仍然可以将它们分成一个文件夹。

默认情况下,这将执行 /admin_users/add 类型的情况,但可以通过第二部分(一些路由)进行调整:

Router::connect('/admin/users/:action', array('controller'=>'admin_users'))

这必须为每个管理部分完成 - 不理想,但我可以在不修改 Cake 代码的情况下想不出更好的方法。

I'd second using ACL/roles for real admin stuff, and probably not using admin routing in production. I sometimes keep a scaffolded (so minimal extra code) admin routing for low-level admin stuff, accessible only to me, but that's probably not wise in a robust production app.

Edit after commentary: It's not optimal, but you may be able to put together something that will appear like you want it in the URLs, and also be organized into folders. I haven't been able to test it yet, but here's the idea:

Create a folder "admin" in your controllers folder and for the users admin, make a users_admin_controller.php controller file. They collapse the folder structure, so you still can't have the same names as your root dir, but you can still separate them into a folder.

This will by default do an /admin_users/add type situation, but that can be tweaked with the second part, some routing:

Router::connect('/admin/users/:action', array('controller'=>'admin_users'))

This would have to be done for each admin section - not ideal, but I can't figure out a better way without modifying Cake code.

浸婚纱 2024-09-16 07:58:20

我在我的应用程序中使用了 ACL,我发现它比使用管理路由要好得多。它更容易。如果您确实需要前缀,可以通过普通路由来实现。

I've used ACL for my application and I find it much better than using admin routing. Its much easier. If you really want a prefix, you can do that with normal routing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文