Search code examples
phplaravelpostlaravel-5basic-authentication

Missing argument 1 for MyController in RESTful application


I am developing app in laravel (REST server), using Basic Auth. Using Postman, all GET requests I have implemented seem to work, but unfortunately POST requests not.

routes.php:

Route::post('my/action', 'MyController@postMyAction');

My Controller:

public function __construct()
{
    $this->middleware('auth.basic.once');
}

public function postMyAction($request)
{
    // some logic here
}

The problem is, that this way, after setting credentials and some params in Postman, following exception appears:

Missing argument 1 for App\Http\Controllers\MyController::postMyAction()

Does anybody knows how to put request into post-processing function defined in routes?

Thanks in advance.


Solution

  • Laravel provides dependency injection for controller methods, however you need to typehint exactly what you want so Laravel knows what to inject:

    public function postMyAction(\Illuminate\Http\Request $request) 
    {
        // Now $request is available
    

    Now Laravel knows you want an instance of Illuminate\Http\Request and it will give it to you.

    Of course you can also stick use Illuminate\Http\Request; at the top of your controller then just typehint Request $request as the argument.