4

I want to remove controller name from url. It works for one controller but it does not work for more than one controller. Here is my code in Route.php:

 Router::connect('videos/:action', array('controller' => 'videos'));
 Router::connect('/:action', array('controller' => 'frontends')); 

but when I try to access http://local.tbn24.dev/videos it shows:

Error: The action videos is not defined in controller FrontendsController

which proves the above url refer

Router::connect('/:action', array('controller' => 'frontends'));

I want this url to reach the videos controller index function. How could I use both Route::connect() configuration?

AD7six
  • 63,116
  • 12
  • 91
  • 123
  • how can I refer http://local.tbn24.dev/videos to 'controller' => 'videos' and 'action'=>'index' –  Aug 10 '15 at 10:32

1 Answers1

1

but when I try to access http://local.tbn24.dev/videos

There is no route for that

Of the two routes defined, this one does not match the above url as it is only one path segment:

Router::connect('videos/:action', array('controller' => 'videos'));

Therefore, it'll match the catch all route, with videos being interpretted as the action to look for.

Also note that without a leading slash, the route won't match any request as they will always start with a leading slash.

A route to match only a controller name

To define a route to match /videos - either define a route to match that specific string:

Router::connect('/videos', array('controller' => 'videos', 'action' => 'index'));

Or, define a route with a restrictive pattern:

Router::connect(
    '/:controller', 
    array('action' => 'index'),
    array('controller' => 'videos|stuff'),
);

For more information on routes, check the documentation for the version of CakePHP you are using.

Community
  • 1
  • 1
AD7six
  • 63,116
  • 12
  • 91
  • 123