Home > Net >  ReflectionException PHP 8.1.2 Laravel 9.24.0 Function () does not exist
ReflectionException PHP 8.1.2 Laravel 9.24.0 Function () does not exist

Time:08-21

I've just started to use __invoke magic method in Laravel 9. Have been following the laravel 9 documentation and doing as what documentation suggests. I got this Function () does not exist error.

I have even created the InvokeServer controller using this artisan command

php artisan make:controller InvokeServer --invokable

The web route file below

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\InvokeServer;

Route::get('/news',[InvokeServer::class]);

The InvokeServer Controller file attached below

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class InvokeServer extends Controller
{ 
    public function __invoke()
    {
        return view('welcome');
    }

}

After go through stackoverflow not having answer, I have changed this

Route::get('/news',[InvokeServer::class])

to as this

Route::get('/news',[InvokeServer::class,'__invoke']);

It did work, but that's not the way __invoke works. could anyone suggest a detailed explanation for this issue.

&

So My query here, Is documentation syntax not working properly? or Am not using it properly if yes, How should I use it?

CodePudding user response:

As stated in the documentation: https://laravel.com/docs/9.x/controllers#single-action-controllers

Your route definition should be like this:

Route::get('/news',InvokeServer::class)

No need for the array.

  • Related