Route to match all but a string |
[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 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
[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
[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
[eluser]Colin Williams[/eluser]
have you tried a better regex? like [^upload_image]
[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
[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..
[eluser]narkaT[/eluser]
[quote author="cdsuze" date="1222362547"]Is it that I cannot use $1 when there is a match?[/quote] exactly $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 |
Welcome Guest, Not a member yet? Register Sign In |