0
votes

I'm having problems with and invoke type controller. After I create the controller with php artisan make:controller -i and add the route, when go to the route it tells me that the Invoke function doesn't exist.

Here is an image of the error

Here is the route I'm using:

Route::get('/portfolio','PortfolioController');

And here is the code of the controller:

<?php
 namespace App\Http\Controllers;
 use Illuminate\Http\Request;

 class PortfolioController extends Controller
 {
 public function __invoke(Request $request)
 {
    /** @var array $portafolio */
    $portafolio = [
        ["title" => "Proyecto #1"],
        ["title" => "Proyecto #2"],
        ["title" => "Proyecto #3"],
        ["title" => "Proyecto #4"],
    ];

    return view("portfolio", compact("portafolio"));
    }
}

I don't really get why this error occurs, because the invoke function is clearly there, so if anyone knows what could be the problem I will be really grateful. I'm using the last version of Laravel.

1
Is this a new project?apokryfos
Yes it is, I created it today. When I use the Route::view function that also uses an invoke it works, so it's really weird.Pichifino
I just created another project and tried it again, and it still didn't work, so I dont knowPichifino
In the latest version of Laravel for new projects you need to use Route::get('/portfolio',PortfolioController::class); (might need a use somewhere in there too)apokryfos
I tried that but it still doesnt work, thanks for the response. EDIT: I forgot to import the class, now it works! Thank youPichifino

1 Answers

3
votes

You need to use the fully qualified class name as in the documentation:

use App\Http\Controllers\PortfolioController;

Route::get('/portfolio', PortfolioController::class);