Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dedadev's avatar

Redirect to the same route but with different parameter values

Hi, in the project that I'm working on I've got some pages with this kind of url, two of them are about and news:

localhost/{param1}/about

localhost/{param2}/about

localhost/{param1}/news

localhost/{param2}/news

Every page has its own route with its own controller.

Is it possibile to make a middleware that if the {paramX} has not a certain value (maybe inside of an array) it will redirect to the same original route BUT with the {paramX}} set to a different fixed value?

With that I can use a single generic function that works with all this kind of pages (and their respective routes/controllers).

I saw that it's possible to get Route::getCurrentRoute()->getActionName() but I don't kown how to use it with return redirect()->route or something similar.

Thank you in advance for your support.

[Edit] given a route extracted from a request, is it possible to do something like this inside the middleware?

public function handle(Request $request, Closure $next)
    {
        $route = $request->route();
      
        if (!in_array($request->param1, ['aaa', 'bbb', 'ccc', true))
        {
            $route->parameters['param1'] = 'aaa';

            return redirect()->$route;
        }

        return $next($request);
    }

Bye

0 likes
7 replies
jlrdw's avatar

Did your IDE show any errors in the code.

dedadev's avatar

@jlrdw Thank you for the answer. Nope, only Ignition threw me an error but having already changed the code I can't paste it here anymore, sorry.

click's avatar

If you name your routes, something like this:

Route::get('{param}/about', YourAboutController::class)->name('about');
Route::get('{param}/news', YourNewsController::class)->name('news');

You can easily create a new route based on the route name and replacing the param variable in the route.

private array $whitelistParams = ['aaa', 'bbb'];
private string $defaultParam = 'aaa';

public function handle(Request $request, Closure $next)
{
    if (!in_array($request->route('param'), $this->whitelistParams)) {
        $newParams = array_merge(
            request()->route()->parameters,
            ['param' => $this->defaultParam]
        );

        $redirectTo = route($request->route()->getName(), $newParams);

        abort(redirect($redirectTo));
    }

    return $next($request);
}

Something like this should do the trick. If the param is not in the whitelist it redirects you to the the same route with the $this->defaultParam in it.

Note that laravel also knows 'missing()' in the routes: https://laravel-news.com/route-missing-method depending on your requirements this could be a more robust solution

dedadev's avatar

@click Thank you for your answer, I will try it. Also the missing() method is cool, I'll keep that in mind, thanks.

Snapey's avatar

With that I can use a single generic function that works with all this kind of pages (and their respective routes/controllers).

Then accept a single parameter and fork it in the single controller based on the passed parameter

dedadev's avatar

@Snapey Thank you for your answer. Can you elaborate a little bit? Because from what I understand this will lead to some duplicated code.

Thank you

Snapey's avatar

@dedadev why would it be duplicated?

I don't really know what you want to do and what these param might be (how many variations)

Please or to participate in this conversation.