Welcome Guest, Not a member yet? Register   Sign In
Route to match all but a string
#1

[eluser]cdsuze[/eluser]
Hi guys, I'm new in CI, now re-coding my old fashioned mysql-php-html-together coding style with a MVC framework Smile

So far very good, but I have the following situation:

My site url's are like this

mysite/products
mysite/products/category_nick
mysite/products/category_nick/subcategory_nick

Each of them routes to a different controller/method (I have categories and products controllers). This is so far working good:

$route['^products/(:any)/(:any)/(:any)'] = "products/detail/$1/$2/$3";
$route['^products/(:any)/(:any)'] = "products/list_products/$1/$2";
$route['^products/(:any)'] = "categories/list_subcategories/$1";
$route['^products/?$'] = "categories";

The problem arises when I have a different method inside the products, for instance:

products/upload_image

It fail because it matches my third route, and forwards to categories controller.

I have tried this as a replacement for my third route with no success (I get 404 error):

$route['^products/(?!upload_image)'] = "categories/list_subcategories/$1";

Do you guys can show a better (and working) way to do this job? Is there a way to debug the routing process so I can understand what's the problem?

Thanks a lot in advance,

César Enique
#2

[eluser]Mirage[/eluser]
César,

Make sure to place the most specific routes closest to the top. So your upload_image rule comes before the (:any) rules.

Secondly, I don't know if this is an option or not, but could you change your url pattern to target the method more specifically? like

mysite/products/view/category_nick
mysite/products/view/category_nick/subcategory_nick

Then you can evaluate the nicks as parameters to the view method and dispatch from there...

HTH,
-m
#3

[eluser]cdsuze[/eluser]
Thanks Mirage for your answer.

Actually I can't change the URL for products because we need to migrate the site using the same as the old ones..of course we can use admin/products/upload_image to solve it, or have a more specific rule that matches exactly /products/upload_image..

So, the point here is to understand why the regular [removed]?!upload_image) doesn't work in this case, and if there is a way to debug the routes..

Thanks again for your advice!

César Enrique
#4

[eluser]Colin Williams[/eluser]
have you tried a better regex? like [^upload_image]
#5

[eluser]narkaT[/eluser]
[quote author="Colin Williams" date="1222238666"]have you tried a better regex? like [^upload_image][/quote]

the []-qoutes are for character classes, so [^upload_image] won't match any chars
that are contained in upload_image.


I'm not shure, but I think "?!"-assertions are non capturing, maybe that can
cause problems.

Greetings
Jan
#6

[eluser]Colin Williams[/eluser]
Assertions are indeed non-capturing.
#7

[eluser]cdsuze[/eluser]
Thanks a lot guys for your answers. I'm not exactly sure about what non-capturing means. Is it that I cannot use $1 when there is a match?

I was trying to do use this example:

http://bizwidgets.biz/blog/posts/codeign...rls-short/

So far I have been using one more specific route and it works, but I have to add one route per each new method in the products controller. Just finding a way to do it better

Thanks again for your help..
#8

[eluser]narkaT[/eluser]
[quote author="cdsuze" date="1222362547"]Is it that I cannot use $1 when there is a match?[/quote]

exactly Wink

$0 only works because the "matches"-array which is returned from preg_match
contains the complete matched string at its first index (0).

Greetings
Jan




Theme © iAndrew 2016 - Forum software by © MyBB