If you like to use FormRequest objects in your controllers (and who doesn't?), but you also want them to include the named parameters from your routes, there's an easy way to do that by using a custom service provider.
You'll only need a few lines of code, so if you have an existing custom service provider that you'd like to use, you can put it there.
Using an existing service provider
First, make sure to use
the Illuminate\Foundation\Http\FormRequest
class at the top of the file, underneath the namespace
declaration:
use Illuminate\Foundation\Http\FormRequest;
Then, put the following code inside the boot
method.
public function boot()
{
// automatically add route params to FormRequest objects
$this->app->resolving(FormRequest::class, function ($request, $app) {
$request->merge($request->route()->parameters());
});
}
Using a new service provider
If you don't have an existing service provider yet, or if you'd like to keep this code in its own service provider, first create a new provider using the artisan command:
php artisan make:provider FormRequestProvider
That will create the app/Providers/FormRequestProvider.php
file. After copying the code from above, the entire file should look like this:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Http\FormRequest;
class FormRequestServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
// automatically add route params to FormRequest objects
$this->app->resolving(FormRequest::class, function ($request, $app) {
$request->merge($request->route()->parameters());
});
}
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
}
If you created a new service provider, also make sure to add it to the providers
array in the config/app.php
file. Otherwise, Laravel won't load it! You can put it in the Application Service Providers section.
Inside config/app.php
:
'providers' => [
...
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\FormRequestServiceProvider::class,
App\Providers\RepositoryProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\ValidationServiceProvider::class,
...
],
And that's it! Now, when using your FormRequest objects in your controllers (or anywhere else), you'll have access to named route parameters. You can use them just like any other data in your FormRequest.
Example usage
For example, a named route param like this:
Route::get('user/{id}', 'UserController@getUser');
...will let you do this in the controller:
namespace App\Http\Controllers;
use App\Http\Requests\GetUser;
class UserController extends Controller
{
public function getUser(GetUser $request)
{
$id = $request->id;
}
}
Now you can write validation rules for the named parameters inside your FormRequest
, use them in the authorize
method, and access them in your controller, just as you would for any other field. Convenient!