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

MUM's avatar
Level 4

Limit route prefix to specific values

Maybe it's really simple (like many things in laravel), but I do not see right way:

I want group some controller functions. Now, there can be different group names but the controllers are always the same. In my example groups can be "project" or "documentation" with the controllers for "event" and "details". Any other non existing group name should throw an error for a non existing route. A working code is that:

Route::get('{projectType}/event', 'MyEventController@index')->where('projectType', 'project|documentation');
Route::get('{projectType}/details', 'MyDetailsController@index')->where('projectType', 'project|documentation');

Now, that code isn't really pretty and when the controller lists grows, it is harder to maintain and understand. So i try:

Route::prefix('{projectType}')->group(function () {
    Route::get('event', 'MyEventController@index');
    Route::get('details', 'MyDetailsController@index');
})->where('projectType', 'project|documentation');

but this throws only an error "Call to a member function where() on null". It looks like prefix doesn't support what i want?

How to solve?

0 likes
5 replies
lostdreamer_nl's avatar

What do your controller methods look like?

Because this should work (as long as you have all the parameters in the controller methods setup correctly)

MUM's avatar
Level 4

The question is, it does really matter at this point how the controller is defined? The error is thrown by the Route::prefix which indicated, that the where method does not exist. Question is, maybe the where must be placed on another place to get it working?

lostdreamer_nl's avatar
Level 53

Aah, I figured it might have been from the controller part..... Here's a working example, just switch the group and the where() around

Route::prefix('{projectType}')
    ->where(['projectType' => 'project|documentation'])->group(function () {
        Route::get('event', function($projectType) {
            dd($projectType);
        });
        Route::get('details', function($projectType) {
            dd($projectType);
        });
    });

// so in your case:

Route::prefix('{projectType}')
    ->where(['projectType' => 'project|documentation'])->group(function () {
        Route::get('event', 'MyEventController@index');
        Route::get('details', 'MyDetailsController@index');
    });

And make sure both Controllers have the $projectType as their first parameter and make sure the where() method gets an array, not 2 strings.

1 like
MUM's avatar
Level 4

@LOSTDREAMER_NL - It works, very cool! Thank your for very fast response! :-) Maybe it should be added to laravel documentation (can't find it there)

lostdreamer_nl's avatar

Agreed, the point is though, a lot of things are possible, and many are not really documented (going into the API helps a lot).

Best thing is using an IDE and clicking through the classes and read the doc blocks

Please or to participate in this conversation.