CakePHP的/ MVC管理功能布局 [英] CakePHP/MVC Admin functions placement

查看:113
本文介绍了CakePHP的/ MVC管理功能布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个问题,更多的意见,而不是解决一个具体问题。

我与CakePHP的工作是第一次,现在我的工作对网站的管理部分。

你在哪里,作为一个MVC或CakePHP的开发者喜欢把你的管理职能?

最初我把他们在一个AdminController,但由于改变为把功能在于意指数据被操纵的类型的控制器。比如,我把用户列表/编辑在UserController的。

对我来说这更有意义,因为有可能是在UserController的功能,可能是有用的。

如果你留下一个回应,请你留下只言片语说为什么?也许这是一个有争议的问题。

照顾,

- 编辑

 如果($这个 - > Auth->用户('USER_TYPE')== 1){//仔细检查该用户是管理员
            $这个 - > Auth->允许('显示器');
            $这个 - > Auth->允许('表');


解决方案

我不认为这是一个有争议的问题。把管理职能在各自的控制器(即不都在一个管理员控制器一起),并使用管理员preFIX路由,内置了蛋糕,让他们安全。这是CakePHP的制裁办法做到这一点,和CakePHP让你在通过烘烤控制台这种方式创建管理功能。

您可以通过保护与ADMIN_在你的AppController code的几行简单​​pfixed所有控制器功能$ P $,所有的管理功能都可以通过像这样整齐,一致的URL来访问:<一个href=\"http://www.example.com/admin/my_controller/my_function\">http://www.example.com/admin/my_controller/my_function

这应该让你开始:<一href=\"http://book.cakephp.org/2.0/en/development/routing.html#$p$pfix-routing\">http://book.cakephp.org/2.0/en/development/routing.html#$p$pfix-routing

让我知道如果你需要更多的帮助,我会用更多的信息来更新我的答案。

编辑:更多信息...

下面是一些步骤来设置管理路由:

1 /在app /配置/ core.php中,周围线113,确保这条线存在,并且取消注释:

 配置::写(路由prefixes。',阵列('管理员'));

2 /在app /控制器/ AppController.php(即控制器超类),测试在beforeFilter方法​​管理路由。这是不符合DRY原则调整 - 不要在每个控制器的beforeFilter做到这一点。这里是我的前滤波方法为例:

 函数beforeFilter(){
    如果(使用isset($这个 - &GT;请求 - &GT; PARAMS ['管理员'])){
            //用户访问的管理功能,因此相应的处理。
        $这个 - &GT;布局='管理员';
        $这个 - &GT; Auth-&GT; loginRedirect =阵列(控制器= GT;'用户','行动'=&GT;'指数');
        $这个 - &GT; Auth-&GT;允许('密码');
    }其他{
            //用户已经访问的非管理功能,因此相应的处理。
        $这个 - &GT; Auth-&GT;允许();    }
}

3 / preFIX与ADMIN_所有的管理功能,他们应该自动地通过preFIX路由可用。

如:

 函数admin_dostuff(){回声喜从管理功能; } //这将通过http://www.example.com/admin/my_controller/dostuff可用功能dostuff(){回声喜从非管理功能; } //这将通过http://www.example.com/my_controller/dostuff可用

一旦你的设置,所有你需要做的就是用ADMIN_ preFIX管理职能,蛋糕会处理这一切为您服务。有意义吗?

编辑2:

下面是一些快速编写例如code,应该帮助你的情况。

 函数beforeFilter(){
    如果(使用isset($这个 - &GT;请求 - &GT; PARAMS ['管理员'])){
        //用户访问的ADMIN_功能,因此检查它们是否是管理员。
        如果($这个 - &GT; Auth-&gt;用户('USER_TYPE')== 1){
            //管理员用户已经访问管理员功能。我们总是可以允许的。
            $这个 - &GT; Auth-&GT;允许();
        }其他{
            //一个非管理员用户已经访问管理员功能,所以我们不应该允许它。
            //在这里,您可以重定向它们,或给出错误信息,或一些
        }
    }其他{
        //用户已经访问的非管理功能,因此处理它不过你想要的。
        $这个 - &GT; Auth-&GT;允许(); //这个例子给所有非管理员功能的公共访问。
    }
}

This is a question more for opinions rather than for a solution to a specific problem.

I am working with CakePHP for the first time and am working on the admin part of the site now.

Where do you, as an MVC or CakePHP developer like to put your admin functions?

Initially I was putting them in an AdminController, but have since changed to putting the functions in a controller that is meant for the type of data being manipulated. For example, I put the user listings/editing in the UserController.

To me this makes more sense since there is likely to be functionality in the UserController that may be useful.

If you leave a response, could you please leave a few words saying why? Perhaps it is a moot point.

take care, lee

-- Edit

if ($this->Auth->user('user_type') == 1){//double-check the user is Admin
            $this->Auth->allow('display');
            $this->Auth->allow('watch');

解决方案

I don't think it's a moot point. Put the admin functions in their respective controllers (ie, not all together in one 'admin' controller), and use 'admin' prefix routing, built into Cake, to keep them secure. This is the CakePHP sanctioned way to do it, and CakePHP allows you to create admin functions in this way via the Bake console.

You can protect all controller functions prefixed by admin_ with a few simple lines of code in your AppController, and all admin functions can be accessed via tidy, consistent URLs like this: http://www.example.com/admin/my_controller/my_function

This should get you started: http://book.cakephp.org/2.0/en/development/routing.html#prefix-routing

Let me know if you need more help and I'll update my answer with more info.

EDIT: More info...

Here's some steps to set up admin routing:

1/ in app/Config/core.php, around line 113, make sure this line exists and is uncommented:

    Configure::write('Routing.prefixes', array('admin'));

2/ In app/Controller/AppController.php (ie, the controller superclass), test for admin routing in your beforeFilter method. Do NOT do this in the beforeFilter of each controller - that is not in tune with DRY principles. Here's my before filter method as an example:

function beforeFilter() {
    if (isset($this->request->params['admin'])) {
            // the user has accessed an admin function, so handle it accordingly.
        $this->layout = 'admin';
        $this->Auth->loginRedirect = array('controller'=>'users','action'=>'index');
        $this->Auth->allow('login');
    } else {
            // the user has accessed a NON-admin function, so handle it accordingly.
        $this->Auth->allow();

    }
}

3/ Prefix all your admin functions with admin_ and they should automatically be available via prefix routing.

eg.

function admin_dostuff () { echo 'hi from the admin function'; } // This will be available via http://www.example.com/admin/my_controller/dostuff

function dostuff () { echo 'hi from the NON-admin function'; } // This will be available via http://www.example.com/my_controller/dostuff

Once you've got that set up, all you need to do is prefix admin functions with admin_, and Cake will handle it all for you. Make sense?

EDIT 2:

Here's some quickly-written example code that should help your situation.

function beforeFilter() {
    if (isset($this->request->params['admin'])) {
        // the user has accessed an admin_ function, so check if they are an admin.
        if ($this->Auth->user('user_type') == 1){
            // an Admin user has accessed an admin function. We can always allow that.
            $this->Auth->allow();
        } else {
            // A non-admin user has accessed an admin function, so we shouldn't allow it.
            // Here you can redirect them, or give an error message, or something
        }
    } else {
        // the user has accessed a NON-admin function, so handle it however you want.
        $this->Auth->allow(); // this example gives public access to all non-admin functions.
    }
}

这篇关于CakePHP的/ MVC管理功能布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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