![]() |
Hi everyone,
I’ve encountered an issue with routing and I’m not sure whether it’s a bug or expected behavior not clearly explained in the documentation. I like to handle form processing in the same method that renders the form’s HTML. Additionally, I prefer using optional IDs in these methods:
Now, I want to create a method for editing a post with an optional ID. From what I understand, the only way to achieve this is by defining two routes: PHP Code: $routes->match(['GET', 'POST'], 'edit', 'PostController::edit'); However, if I want to assign a name to one of these routes, I can only name the route without the ID: PHP Code: $routes->match(['GET', 'POST'], 'edit', 'PostController::edit', ['as' => 'edit']); If I try to name the route with the ID instead, navigating to it returns a 404 error: PHP Code: $routes->match(['GET', 'POST'], 'edit', 'PostController::edit'); For example, navigating to /edit/12 results in a 404. Is this behavior intentional? Or am I missing something in the way named routes and optional parameters are meant to work in CodeIgniter 4? Any guidance would be appreciated. Thanks in advance!
how doyou call your form to route maybe this can at least give an good eye
Code: action="url_to('edit', $product['id'])" Code: action="route_to('edit', $product['id'])" Code: action="base_url('edit/'.product[id]) fetch Code: edit/productId and your contr shoul be PHP Code: public function edit($id = null) Codeigniter First, Codeigniter Then You!!
yekrinaDigitals
Thank you for the tip!
I’ve implemented something similar to your suggestion for generating URLs: PHP Code: public function getUrl(): string However, my question is more about understanding the strange behavior I encountered, rather than finding a solution ![]() It’s odd that adding a name to the route changes its behavior ![]()
https://github.com/codeigniter4/CodeIgni...n.php#L166
There is a list for named routes. I assume that it is a sequence of unnamed and named routes. I didn't study it exactly.
Yeah! I found it! It seems like a bug. There is a conflict of custom names and URI paths.
I have URI "edit" and named URI "edit/(:num)" as "edit". So, name "edit" and the path "edit" intersects. These names and paths are keys of array of all routes so keys must be unique but its don't! File: system/Router/Router.php Method: checkRoutes https://github.com/codeigniter4/CodeIgni...r.php#L407 I've created a pull request to try and fix this https://github.com/codeigniter4/CodeIgniter4/pull/9499 |
Welcome Guest, Not a member yet? Register Sign In |