Route with optional param is not working - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Route with optional param is not working (/showthread.php?tid=65389) |
Route with optional param is not working - harpreet - 06-06-2016 hello, So I have routes like PHP Code: $route["admin/product/image/(:num)"]["delete"] = "product/deleteImage/$1"; http://localhost/PROJECT/admin/product/image as a delete request should work. But it leads to 404. I have to pass the last param to make it work. any help is appreciated. RE: Route with optional param is not working - cartalot - 06-07-2016 this doesn't make any sense. are you really saying this is your url? http://localhost/PROJECT/admin/product/image how could that be a delete request when you are not specifying an id (number) to delete? RE: Route with optional param is not working - kilishan - 06-07-2016 In your method definition, provide a null possibility for the argument and it should work for you: Code: function deleteImage($id = null) RE: Route with optional param is not working - harpreet - 06-07-2016 (06-07-2016, 01:33 PM)cartalot Wrote: this doesn't make any sense. are you really saying this is your url? The delete request also takes a URL to delete temp images that don't have ID yet. BTW the solution was PHP Code: $route["admin/product/image/?(:num)?"]["delete"] = "product/deleteImage/$1"; to make parameter optional. even though the functions is defined as PHP Code: public function deleteImage($imageId=0) { RE: Route with optional param is not working - harpreet - 06-07-2016 (06-07-2016, 06:07 PM)kilishan Wrote: In your method definition, provide a null possibility for the argument and it should work for you: I too thought that would work but it does not and am not sure why another example with screenshot. Let me know if i am doing anything wrong. |