Search code examples
phpcodeigniter-4

Setting Parameters in CI4s redirecting function


is there any possible way to send parameters within the redirection function from Codeigniter 4? Important, it is a named route:

$routes->get('edit', 'Test_Controller::editTest/$1', ["as" => "editTest", "filter" => 'testFilter']);

And i want to do a redirect like this:

this works fine:

return $this->redirectTo('test/edit/' . $id1);

this would be nice, if it works:

return redirect('editTest', array($passingID));

or

return redirect('editTest')->with($passingID);

Why? I have HMVC and I would like to name every module with the same routes but different functions ofc. And this would help me having same site urls. (edit, add, a.s.o.)


Solution

  • The short answer is no its not possible. Take a look at system/Common.php and you'll see there is only 1 parameter to the redirect() function.

    The longer answer however is take a look at what the redirect function does.

    function redirect(?string $route = null): RedirectResponse
        {
        $response = Services::redirectresponse(null, true);
    
        if (! empty($route)) {
            return $response->route($route);
        }
    
        return $response;
    }
    

    Based on the current (4.1.4) redirect function you'll see it calls route(). It turns out that route() allows a second parameter which are the parameters.

    So the solution would probably be to do your own redirect() function (see https://codeigniter.com/user_guide/extending/common.html?highlight=common) and allow a second parameter.