Dail
Dail

Reputation: 4606

Custom CakePHP route with Regex

I am using cakephp 2.3.8 and I have this custom route.

Router::connect('/:regione/*', array('controller' => 'ads', 'action' => 'region'), array('regione' => 'WHAT HERE??' )); 

Now, I have to execute this route if my :regione IS NOT starting with the string auth.

Example:

example.com/abruzzo   (ok)
example.com/abruzzo/2 (ok)
example.com/abruzzo/3 (ok)
example.com/auth (NO)
example.com/auth/something (NO)

I do not know how to write this regex in this case, because I need to execute the route IF DOES NOT MATCH/START with auth string.

Thank you!

Upvotes: 0

Views: 557

Answers (1)

ndm
ndm

Reputation: 60463

I think using a negative lookahead should do it:

(?!auth).*

This should match everything that doesn't start with auth, which means not even authenticate would match.

The internally compiled route regex will look like this:

#^(?:/(?P<regione>(?!auth).*))(?:/(?P<_args_>.*))?[/]*$#

Upvotes: 1

Related Questions