PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 25, 2022

[FIXED] Cakephp 3 routing not working

 January 25, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

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?


Solution

$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.



Answered By - AD7six
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing