Sainesh Mamgain
Sainesh Mamgain

Reputation: 785

Cakephp 3 routing not working

I am creating a route in cakephp 3.2 under the scope '/'. But it is not working.

here is my code.

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

    $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);

    $routes->connect('/user/:user',['controller'=>'Users','action'=>'myAccount'],['user'=>'/^[a-z0-9_-]$/','pass'=>['user']]);

    $routes->fallbacks('DashedRoute');
}

When I am visiting a URL like http://localhost/mysite/user/username then it is throwing an error UserController not found.

What should I do?

Upvotes: 2

Views: 1256

Answers (1)

AD7six
AD7six

Reputation: 66170

$routes->connect(
    '/user/:user',
    ['controller'=>'Users','action'=>'myAccount'],
    ['user'=>'/^[a-z0-9_-]$/','pass'=>['user']]
);

This route element regex, if it worked, is going to restrict the user parameter to be exactly one character.

To match "username", or any string longer than one char it's necessary to modify the regular expression for example:

$routes->connect(
    '/user/:user',
    ['controller'=>'Users','action'=>'myAccount'],
    ['user'=>'[a-z0-9_-]+','pass'=>['user']]
//                      ^             
);

Note that route element regular expressions are not expected to be a complete regex.

Upvotes: 1

Related Questions