Wednesday, March 9, 2022

[FIXED] CakePHP3 Auth redirectURL route broken

Issue

I have a controller with a particular method to login:

public function login() {

    if ($this->request->is('post')){
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }

    // not logged
    $this->Flash->error('Your username or password is incorrect');

    }
}

and default route looks like

Router::scope('/', function (RouteBuilder $routes) {
    $routes->fallbacks(DashedRoute::class);
});

after user is logged in CakePHP throws an error

Error: A route matching "/" could not be found.

None of the currently connected routes match the provided parameters. Add a matching route to config/routes.php

when IMO it should to redirect to the page (based on a related controller) from where login method was executed.

Login code is based on that tutorial.

Any thoughts?


Solution

To solve this issue:

Please update the below lines in routes.php file

Router::defaultRouteClass('DashedRoute');

Router::scope('/', function (RouteBuilder $routes) {

$routes->connect('/', ['controller' => 'users', 'action' => 'index']);

$routes->fallbacks('DashedRoute');

}); Plugin::routes();

Please do create index() in users controller. Let me know if any issue.



Answered By - Archit Patel

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.