Codeigniter4: redirect route when route is more complex

Created on 5 May 2020  路  4Comments  路  Source: codeigniter4/CodeIgniter4

Hello i am trying to redirect to a reversed route specified in my routes like this and i get this error.
SYSTEMPATH/HTTP/Exceptions/HTTPException.php at line 146

Here is my routes


$routes->group('cms/banners', ['namespace' => 'App\Controllers\cms'], function($routes)
{
    $routes->get('/', 'banners::index');
    $routes->get('add', 'banners::add');
    $routes->get('edit/(:num)', 'banners::edit/$1');
    $routes->post('add', 'banners::submit_add');
});

And here is my code inside my controller
return redirect()->route('cms/banners/edit/'.$id_item);

When i submit the form i want to go to the edit page of the item.
It does not work.

When i go to the edit page of the item manually

https://example.com/cms/banners/edit/36
it works perfectly, the Route is working correctly.

bug

Most helpful comment

Dude, this redirect()->route() is amazing :)

All 4 comments

I have tried also with the route_to function like this and it is still not working.
return redirect()->route(route_to('App\Controllers\cms\banners::edit', $id_item));

the route_to function returning the string '/cms/banners/edit/30'

but the redirect is not working

This is not a bug.
This redirect option is used in the case of named routes.
Maybe another redirect option is right for you:
return redirect()->to('cms/banners/edit/'.$id_item);

I found how it should work with like i want,

i just had to put an alias for the route like this
$routes->get('edit/(:num)', 'banners::edit/$1', ['as' => 'edit_banner']);

and then use this in my controller
return redirect()->route('edit_banner', [$id_item]);

Dude, this redirect()->route() is amazing :)

Was this page helpful?
0 / 5 - 0 ratings