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

joserick's avatar

I know the problem is from Laravel 8 but already in Laravel 9 you can use:

public function holiday($name) {
    return $name;
}

Route::get('/holiday', [HolidayController::class, 'holiday'])
    ->defaults('name', 'Día de muertos');

Also works for DI (Dependency Injection) Automatic:

public function holiday(Holiday $holiday) {
    return $holiday->name;
}

Route::get('/holiday', [HolidayController::class, 'holiday'])
    ->defaults('holiday', 7941);
dougd_nc's avatar

I am trying to do exactly the same in Laravel 8. I would like to have two different pages that are configured in the routes file to display/act differently based on parameters to the controller function.

The defaults() method is also in Laravel 8, but as a warning to anyone trying to use it, it does not work as expected.

It ignores the parameter name and looks only at the parameter order. So, you have to call defaults() in the order of the function/method parameters. Not sure if this is a bug in Laravel 8. If I am using it wrong, let me know.

For example: I can do this:

    Route::get('/displayone', [WAYFController::class, 'wayf'] )
        ->middleware('not-auth')
  		  //  These must be in order and name of parameter doesn't matter??!!
        ->defaults('tiledUI', true)
        ->defaults('showAffiliateLogin', false)
        ->defaults('type', 'full')
        ->defaults('tiledUIConfigCode', 'online'); // required for Tiled UI

    Route::get('/displaytwo', [WAYFController::class, 'wayf'] )
        ->middleware('not-auth')
  		  //  These must be in order and name of parameter doesn't matter??!!
        ->defaults('tiledUI', false)
        ->defaults('showAffiliateLogin', false)
        ->defaults('type', 'full')
        ->defaults('tiledUIConfigCode', 'none'); // required for Tiled UI

This works if that's the order of the function parameters. However, if I switch the order of the calls to defaults(), it will pass the values to the wrong parameters. The parameter names such as 'tiledUIConfigCode' have no effect.

Snapey's avatar

@dougd_nc Funny that you have exactly the same issue, when one thing this thread did establish was that the OP could not explain why or what he was trying to do.

Floddy's avatar

@dougd_nc you can do

public function wayf(\Illuminate\Http\Request $request)
{
    $titledUI = $request->route('titledUI');
    // etc...
}

to get the values (and defaults) from the route instead of having them as arguments.

I usually use it for routes like /foo/{id}/bar. Less Magic, more solid, IMO. :)

laracoft's avatar

@dougd_nc for what it's worth, i'm sure you can call defaults('data', [...]); once with all your data.

Previous

Please or to participate in this conversation.