身份验证组件问题

发布于 2024-10-12 14:42:57 字数 1180 浏览 1 评论 0原文

我试图让我的管理路由与身份验证组件一起使用。我希望以下路由能够工作。

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

但是当我输入 /admin 时,它会重定向到 /admin/users/login 并显示此错误。

Create UsersController::admin_login() in file: cms.local/controllers/users_controller.php

这是我的 app_controller 代码。

class AppController extends Controller {

    var $components = array('DebugKit.Toolbar','Auth','Session');

    function beforeFilter(){

        //Set up Auth Component
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'admin_index');
        $this->Auth->allow('display');

    }

}

users_controller

<?php
class UsersController extends AppController {

    var $name = 'Users';

    function login(){

    }

    function admin_logout(){
        $this->Session->destroy();
        $this->redirect($this->Auth->logout());
    }

}
?>

如果您需要更多信息,请告诉我。

谢谢

Im trying to get my admin routing working with the auth component. I want the following routing to work.

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

but when i type in /admin it redirects to /admin/users/login and display this error.

Create UsersController::admin_login() in file: cms.local/controllers/users_controller.php

here is my app_controller code.

class AppController extends Controller {

    var $components = array('DebugKit.Toolbar','Auth','Session');

    function beforeFilter(){

        //Set up Auth Component
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'admin_index');
        $this->Auth->allow('display');

    }

}

users_controller

<?php
class UsersController extends AppController {

    var $name = 'Users';

    function login(){

    }

    function admin_logout(){
        $this->Session->destroy();
        $this->redirect($this->Auth->logout());
    }

}
?>

If you require more information let me know.

Thanks

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

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

发布评论

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

评论(3

腹黑女流氓 2024-10-19 14:42:57

别担心,一切正常:)

在 AppController::beforeFilter() 中,您声明为了登录,

当您使用 admin 转到 Pages/index 时 ,loginAction 是 Users/login (Users/admin_login 如果 admin=>true) =>true 并且您尚未登录,您将被重定向到 Users/admin_login,如下所示:

但是当我输入 /admin 时,它会重定向到 /admin/users/login 并显示此错误。

在文件:cms.local/controllers/users_controller.php 中创建 UsersController::admin_login()

未找到 UsersController::admin_login() 方法,您应该创建它。

Dont worry, all works ok :)

In AppController::beforeFilter() you stated that in order to log in, the loginAction is Users/login (Users/admin_login if admin=>true)

when you go to Pages/index with admin=>true and you are not logged in, you are being redirected to Users/admin_login, and as this tells:

but when i type in /admin it redirects to /admin/users/login and display this error.

Create UsersController::admin_login() in file: cms.local/controllers/users_controller.php

The method UsersController::admin_login() is not found and you should create it.

独夜无伴 2024-10-19 14:42:57

这对我来说很好:-

function beforeFilter() {
    if (isset($this - > params['admin'])) {
        $loggedAdminId = $this - > Session - > read("adminid");
        if (!$loggedAdminId && $this - > params['action'] != "admin_login") {
            $this - > redirect("/admin/admins/login");
            $this - > Session - > setFlash('The URL you followed requires you login.');
        } else {
            $this - > Auth - > allow('*');
        }
    } else {
        $this - > Auth - > loginAction = array('controller' = > 'users', 'action' = > 'login', 'admin' = > false);
        $this - > Auth - > loginRedirect = array('controller' = > 'users', 'action' = > 'admin_index');
    }
}

This worked for me fine:-

function beforeFilter() {
    if (isset($this - > params['admin'])) {
        $loggedAdminId = $this - > Session - > read("adminid");
        if (!$loggedAdminId && $this - > params['action'] != "admin_login") {
            $this - > redirect("/admin/admins/login");
            $this - > Session - > setFlash('The URL you followed requires you login.');
        } else {
            $this - > Auth - > allow('*');
        }
    } else {
        $this - > Auth - > loginAction = array('controller' = > 'users', 'action' = > 'login', 'admin' = > false);
        $this - > Auth - > loginRedirect = array('controller' = > 'users', 'action' = > 'admin_index');
    }
}
唠甜嗑 2024-10-19 14:42:57

您可以通过指定 'admin' => 强制 Auth 使用无前缀登录操作。 false 在你的loginAction中。这样,所有需要身份验证的操作都将使用无前缀登录操作。所以你的 beforeFilter 看起来像这样:

function beforeFilter(){

    //Set up Auth Component
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false);
    $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'admin_index');
    $this->Auth->allow('display');

}

同样,你可以采取另一种方式并指定 'admin' => true 表示仅使用 admin_login

You can force Auth to use the non-prefixed login action by specifying 'admin' => false in your loginAction. This way, all actions that require authentication will use the non-prefixed login action. So your beforeFilter would look something like this:

function beforeFilter(){

    //Set up Auth Component
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false);
    $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'admin_index');
    $this->Auth->allow('display');

}

Similarly, you could go the other way and specify 'admin' => true to only use admin_login.

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