【问题标题】:CakePHP admin section routing and redirectingCakePHP 管理部分路由和重定向
【发布时间】:2013-05-29 17:20:26
【问题描述】:

我正在为在 CakePHP 项目中创建管理部分的概念而苦苦挣扎。 (2.3.5版)

我在 Config/core.php 中取消了注释:

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

我在 Config/routes.php 中添加了这一行:(就像他们在 CakePHP 食谱中建议的那样。)

Router::connect('/admin', array('controller'=>'pages', 'action'=>'index','admin' => true));

在 AppController.php 我有以下内容:

    public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect'=>array('controller'=>'pages','action'=>'index', 'admin'=>true),
        'logoutRedirect'=>array('controller'=>'pages','action'=>'display','home'),
        'authError'=>'you have no access.',
        'authorize'=>array('Controller')
    )
);

然后我添加了一个布局 View/Pages/admin_index.ctp 这是我想在登录后重定向的地方。我设法让我的登录在 UsersController.php 中工作。

所以问题是,我应该在 AppController.php 中重定向到哪里来让我登录的管理员访问 admin_view?我相信 loginRedirect 不知何故坏了..

我研究了一些关于这个主题的教程,但我发现只有这个 Youtube 视频http://www.youtube.com/watch?v=zvwQGZ1BxdM 所有其他教程似乎都与早期版本的 CakePHP 有关。

【问题讨论】:

  • 您在使用 ACL 吗?因为在那种情况下,您确定将其添加到 ACO 的列表中吗?我遇到了同样的问题,它不会重定向,因为我只是忘记更新我的 ACO 的 facepalm。如果不是这样:你会被重定向吗?当你让它重定向到非管理页面时会发生什么?
  • 不,我没有使用 ACL.. 对我来说太混乱了。
  • 啊,那是因为它可能正在记住前缀。创建锚标签时,我一直有这个问题。一旦您“使用该前缀”,它似乎会自动将前缀设置为 true。这就是为什么我扩展我的 HtmlHelper,它会自动将所有前缀设置为 false,除非我告诉它将其设置为 true。这可能会导致您的问题。这听起来像你的问题吗?如果没有,我们必须进一步搜索:)
  • 它应该破坏会话数据,是的。但是您的浏览器会在按下“返回”时缓存视图,以防止不必要的页面加载。您可以通过简单地在屏幕上打印date('H:i:s'); 来检查这一点,并检查它是否在您按下返回的同一时间。如果是这样,该页面将被缓存;)因此不会构成安全漏洞,因为该页面不会被更新。因为控制器需要授权用户。试试f5'ing。你会发现它不起作用。
  • 你是对的。它破坏了会话数据,并且 admin_index 只是浏览器缓存。换句话说,如果我尝试将 admin_index 上的链接推送到其他与管理员相关的操作,它会重定向到登录视图。所以它完美地工作。非常感谢@Jelmer!

标签: php cakephp


【解决方案1】:

我猜你可以在 AppController 中设置一个 loginAction,然后在这个动作中你可以这样做:

$this->redirect(array('controller'=>'someController','action'=>'someAction','admin'=>true));

【讨论】:

    【解决方案2】:

    试试这个

        public $components = array(
        'Auth' => array(
            'autoRedirect' => false,
            'loginRedirect' => array(
                'admin' => true,
                'controller' => 'dashboard',
                'action' => 'index',
            ),
    
            'loginAction' => array(
                'controller' => 'users',
                'action' => 'login',
                'admin' => false,
                'plugin' => false,
            ),
        ),
    );
        // Now the before Filter which tells it its okay to go to index/view/or display actions.
        public function beforeFilter() {    
        // Allow public views
        $this->Auth->allow('index', 'view', 'display');
        }
    

    对于用户登录,您可以这样做:

        public function login() {
        $this->set('title_for_layout', 'User Sign In');
         if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Auth->login()) {
                    return $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash('Username or password is incorrect', 'flash_bad');
            }
        }
    }
    

    您想要访问的任何方法都可以执行公共功能 admin_index、admin_view admin_settings 等,以便在小部件的控制器中路由将是 /admin/widgets/index /admin/widgets/index 等等在。允许其他页面在没有身份验证的情况下呈现的技巧只是将 $this->Auth->allow 放在 beforeFilter 中。

    【讨论】:

      【解决方案3】:

      我有另一个解决方案。网站的管理面板有另一个图形、javasctipt 等,所以我在我的应用程序文件夹中这样做:

      app_name
        +Model
        +Plugin
        +admin
        +front
      

      是的 admin 在另一个文件夹中,所以我可以设置另一个 cookie 来登录管理区域,只有插件和模型是相同的。这个文件夹结构需要在 bootstrap.php 中进行一些配置:

        App::build(array(
            'Plugin' => array(ROOT . DS . 'Plugin' . DS),
            'Model' => array(ROOT . DS . 'Model' . DS),
            'Model/Behavior' => array(ROOT . DS . 'Model' . DS . 'Behavior' . DS),
        ));
      

      也许对你来说很疯狂,但我更喜欢这个使用管理员路由的解决方案。

      【讨论】:

        【解决方案4】:

        试试这个

        你的 config/routes.php

        Router::connect('/', array('controller' => 'users', 'action' => 'dashboard' ));

        应用控制器

        class AppController extends Controller {
        
        public $components = array(
            'Acl',
            'Session',
            'Auth' => array(
                'authenticate' => array(
                    'Form' => array( 
                        'userModel' => 'User',
                         'fields' => array(
                            'username' => 'user_name',
                            'password' => 'password'
                            )
                        )
                    ),
                'loginAction' => array('controller' => 'users', 'action' => 'login'),
                            'loginRedirect' => array('controller' => 'users', 'action' => 'mysettings'),
                'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
                'authError' => 'You don\'t have access here.',
                        /*
                        'loginAction' => array('controller' => 'users', 'action' => 'forgot_password'),
                        'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
                        'logoutRedirect' => array('controller' => 'users', 'action' => 'forgot_password'),
                        'authError' => 'You don\'t have access here.',
                        */
                ),
        
        );
        

        用户控制器

        class UsersController extends AppController {
        
        /**
         * Components
         *
         * @var array
         */ 
        public function beforeFilter() {
            parent::beforeFilter();
            $this->Auth->allow('login','logout');
        }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-23
          • 2013-05-29
          • 1970-01-01
          • 1970-01-01
          • 2011-04-17
          • 1970-01-01
          相关资源
          最近更新 更多