Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 3.1 admin prefix routing and logging in issue

Tags:

php

cakephp

I'm starting a new app using Cake 3.1 for the first time. I used to use version 2+, but now things have changed and I am encountering some troubles.

I want to have a simple authorization system in my app, so I decide to go with "admin" prefix routing.

I have: UsersController with login action, 'ArticlesController' with namespace App\Controller\Admin in Controller/Admin/ directory.

In routes.php I have:

Router::prefix('admin', function ($routes) {
$routes->fallbacks('DashedRoute');
});

What goes wrong: If I have already logged in everything works fine and I have access to my admin actions. But if I'm not logged in, and will try to access /admin/articles/add I will be redirected to /admin/users/login. And this is totally wrong and I'm obviously get Missing Controller exception.

My question: What I need to do to fix this and get a proper redirect to login action /users/login and not the prefixed version (/admin/users/login).

AppController:

    public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'loginRedirect' => [
            'controller' => 'Pages',
            'action' => 'display',
            'home',
        ],
        'logoutRedirect' => [
            'controller' => 'Pages',
            'action' => 'display',
            'home'
        ]
    ]);
}
    public function beforeFilter(Event $event)
{
    $this->Auth->allow(['index', 'view', 'display']);
}
like image 681
user1327 Avatar asked Jan 31 '26 02:01

user1327


1 Answers

Okay. I found the solution – just need to add

    'loginAction' => [
    'prefix' => false,
        'controller' => 'Users',
        'action' => 'login',
    ]

to auth component configuration

like image 73
user1327 Avatar answered Feb 01 '26 18:02

user1327



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!