Welcome Guest, Not a member yet? Register   Sign In
Routing Issue: Optional ID and Named Routes in CodeIgniter 4
#1

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:
  • When there’s no ID, it handles entity creation.
  • When there is an ID, it handles entity editing.
I’ve seen this approach in various projects and find it quite intuitive.

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');
$routes->match(['GET''POST'], 'edit/(:num)''PostController::edit/$1'); 

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']);
$routes->match(['GET''POST'], 'edit/(:num)''PostController::edit/$1'); 

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');
$routes->match(['GET''POST'], 'edit/(:num)''PostController::edit/$1', ['as' => '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!
Reply
#2

(This post was last modified: 03-26-2025, 07:10 AM by luckmoshy.)

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)
{
    if (
$this->request->is'post')) {
        
// Process form submission
    
}

    if (
$id) {
        
// Load post for editing
    
} else {
        
// Create new post
    
}

    return 
view('post_form', ['id' => $id]);

Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#3

Thank you for the tip!
I’ve implemented something similar to your suggestion for generating URLs:

PHP Code:
public function getUrl(): string
{
    if (empty($this->id)) {
        return url_to('PostController::edit');
    }
    return url_to('PostController::edit'$this->id);


However, my question is more about understanding the strange behavior I encountered, rather than finding a solution Smile

It’s odd that adding a name to the route changes its behavior Undecided
Reply
#4

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.
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#5

(This post was last modified: 03-27-2025, 01:19 PM by Elias.)

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB