Search code examples
phplaravellaravel-routinglaravel-5web-frameworks

Laravel. Routes do not match


At first it seems that the same routes. But in first route not working middleware that I ordered in the constructor.

How to fix that?

Route::get('/cars.get', function() {

return App::make('App\Http\Controllers\CarsController')->{'get'}();

});

Route::get('/cars.get', 'CarsController@get');

sorry for my English =)


Solution

  • Edit

    I was wrong about callAction() it does nothing else than call the method. Unfortunately there doesn't seem to be a simple API to call middleware manually. A solution to this would just be to define the middleware on the route:

    Route::get('/cars.get', ['middleware' => 'auth', function() {
        return App::make('App\Http\Controllers\CarsController')->{'get'}();
    }]);
    

    original answer:

    By directly calling the get() method you skip middleware defined in the controller. You should use callAction() instead:

    return App::make('App\Http\Controllers\CarsController')->callAction('get');
    

    Also note that you can use app() as a shortcut for App::make():

    return app('App\Http\Controllers\CarsController')->callAction('get');