Varinder Singh
Varinder Singh

Reputation: 1590

Cakephp route matches everything

I have following route added to routes.php in the end.

Router::connect('/:sellername/:itemtitle',
                 array('controller' => 'items', 'action' => 'view_item_detail'),
                 array(
                      'pass' => array('sellername','itemtitle'),
                      'sellername' => '[a-zA-Z0-9_-]+',
                      'itemtitle' => '[a-zA-Z0-9_-]+',
                  )
               );

So this matches the dynamic urls like http://example.com/john/title-of-an-item Problem is this also matches every other url like http://example.com/members/signin even though there's a MembersController controller and signin action in it. I can fix it using following route entry.

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

But it's very tedious to add every route like above. Doesn't existing matching controller names are prioritized while making a match? Do order of routes in routes.php matter?

Upvotes: 1

Views: 523

Answers (1)

Dinkar Thakur
Dinkar Thakur

Reputation: 3103

Custom Route classes is to help you

Custom route classes allow you to extend and change how individual routes parse requests and handle reverse routing. A route class should extend CakeRoute and implement one or both of match() and/or parse(). parse() is used to parse requests and match() is used to handle reverse routing.

You can use a custom route class when making a route by using the routeClass option, and loading the file containing your route before trying to use it:

Router::connect(
     '/:slug',
      array('controller' => 'posts', 'action' => 'view'),
      array('routeClass' => 'SlugRoute')
);

This route would create an instance of SlugRoute and allow you to implement custom parameter handling.

custome routing class let you impliment anything

But personal opinion is to user a static and meaning full text in the url that diffrenciate it from the rest.

Upvotes: 3

Related Questions