user4101645
user4101645

Reputation:

Removing controller name from url does not work for more than one controller in cakephp

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?

Upvotes: 4

Views: 313

Answers (1)

AD7six
AD7six

Reputation: 66168

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.

Upvotes: 1

Related Questions