I'm working with Laravel 8 and when I write the route to the __invoke
controller like this:
use App\Http\Controllers\PortfolioController;
Route::get('/portfolio', 'PortfolioController')->name('portfolio');
It shows this error:
Invalid route action: [PortfolioController].
PortfolioController
is not invokable
So it only works like this:
Route::get('/portfolio', [PortfolioController::class, '__invoke'])->name('portfolio');;
Which doesn't make sense to me because it should find the __invoke
which is the only one in PortfolioController.php
:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PortfolioController extends Controller
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
$portfolio = [
['title' => 'Project #1'],
['title' => 'Project #2'],
['title' => 'Project #3'],
['title' => 'Project #4'],
];
return view('portfolio',compact('portfolio'));
}
}
Is Laravel 8 ignoring the __invoke
attribute???
TL;DR
Do it like so:
Explanation
Before Laravel 8, routes were namespaced in
RouteServiceProvider.php
:So, when you defined routes, like in your example:
The
PortfolioController
string was namespaced withApp\Http\Controllers
.However, since Laravel 8 this behaviour has been modified. From the v8 release note:
Now, for the particular case you mentioned,
__invoke()
methods, this is how you should handle them according to the docs: