Optional parameter in the middle of a route
The "correct" answer for this question is; No, you can't and shouldn't use an optional parameter unless it's at the end of the route/url.
But, if you absolutely need to have an optional parameter in the middle of a route, there is a way to achieve that. It's not a solution I would recommend, but here you go:
routes/web.php
:
// Notice the missing slash
Route::get('/test/{id?}entities', 'Pages\TestController@first')
->where('id', '([0-9/]+)?')
->name('first');
Route::get('/test/entities', 'Pages\TestController@second')
->name('second');
app/Http/Controllers/Pages/TestController.php
:
<?php
namespace App\Http\Controllers\Pages;
use App\Http\Controllers\Controller;
class TestController extends Controller
{
public function first($id = null)
{
// If $id is not null, it will have a trailing slash
$id = rtrim($id, '/');
return 'First route with: ' . $id;
}
public function second()
{
return 'Second route';
}
}
resources/views/test.blade.php
:
<!-- Notice the trailing slash on id -->
<!-- Will output http://myapp.test/test/123/entities -->
<a href="{{ route('first', ['id' => '123/']) }}">
First route
</a>
<!-- Will output http://myapp.test/test/entities -->
<a href="{{ route('second') }}">
Second route
</a>
Both links will trigger the first
-method in TestController
. The second
-method will never be triggered.
This solution works in Laravel 6.3, not sure about other versions. And once again, this solution is not good practice.
No. Optional parameters need to go to the end of the route, otherwise Router wouldn't know how to match URLs to routes. What you implemented already is the correct way of doing that:
get('things/entities', 'MyController@doSomething');
get('things/{id}/entities', 'MyController@doSomething');
You could try doing it with one route:
get('things/{id}/entities', 'MyController@doSomething');
and pass * or 0 if you want to fetch entities for all things, but I'd call it a hack.
There are some other hacks that could allow you to use one route for that, but it will increase the complexity of your code and it's really not worth it.
Having the optional parameter in the middle of the route definition like that, will only work when the parameter is present. Consider the following:
- when accessing the path
things/1/entities
, theid
parameter will correctly get the value of1
. - when accessing the path
things/entities
, because Laravel uses regular expressions that match from left to right, theentities
part of the path will be considered to be the value of theid
parameter (so in this case$id = 'entitites';
). This means that the router will not actually be able to match the full route, because theid
was matched and it now expects to have a trailing/entities
string as well (so the route that would match would need to bethings/entities/entities
, which is of course not what we're after).
So you'll have to go with the separate route definition approach.