"ReflectionException Function () does not exist" when trying to setup authentication in Laravel

Viewed 17263

I'm having some trouble getting authentication working 100% in Laravel (all views seem to work so far except for "home") and was hoping to get some assistance from the Laravel experts out there.


A bit of background info:

  • PHP Version: 7.3.11
  • Laravel Version: 8.13.0
  • Used Composer to build the ui scaffolding (composer require laravel/ui)
  • Used the Bootstrap ui option (php artisan ui bootstrap --auth)

The issue

As mentioned above, I seem to be able to access all of the generated authentication views so far (login, register & the password reset views), however after registering with a dummy account I get the following error when trying to access the "home" view:

ReflectionException

Function () does not exist

The Stack trace is pointing to the following file: "vendor/laravel/framework/src/Illuminate/Routing/RouteSignatureParameters.php:23":

<?php

namespace Illuminate\Routing;

use Illuminate\Support\Reflector;
use Illuminate\Support\Str;
use ReflectionFunction;
use ReflectionMethod;

class RouteSignatureParameters
{
    /**
     * Extract the route action's signature parameters.
     *
     * @param  array  $action
     * @param  string|null  $subClass
     * @return array
     */
    public static function fromAction(array $action, $subClass = null)
    {
        $parameters = is_string($action['uses'])
                        ? static::fromClassMethodString($action['uses'])
                        : (new ReflectionFunction($action['uses']))->getParameters();

        return is_null($subClass) ? $parameters : array_filter($parameters, function ($p) use ($subClass) {
            return Reflector::isParameterSubclassOf($p, $subClass);
        });
    }

    /**
     * Get the parameters for the given class / method by string.
     *
     * @param  string  $uses
     * @return array
     */
    protected static function fromClassMethodString($uses)
    {
        [$class, $method] = Str::parseCallback($uses);

        if (! method_exists($class, $method) && Reflector::isCallable($class, $method)) {
            return [];
        }

        return (new ReflectionMethod($class, $method))->getParameters();
    }
}

With the following line (line 23) being highlighted as the error:

: (new ReflectionFunction($action['uses']))->getParameters();


And here are the routes used in the "web.php" file:

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome', ['pageTitle' => 'Home']);
});

Route::get('/services', function () {
    return view('services', ['pageTitle' => 'Services']);
});

Route::get('/contact', function () {
    return view('contact', ['pageTitle' => 'Contact']);
});

Auth::routes();

Route::get('/home', ['pageTitle' => 'Client Dashboard'], [App\Http\Controllers\HomeController::class, 'index'])->name('home');

After a bit of googling I've learnt that the method I'm trying to use to setup authentication has been deprecated and it is now advised to use Jetstream or Fortify, however I also found a few examples of people still managing to use this old method in their projects:

https://www.youtube.com/watch?v=NuGBzmHlINQ


As this is my first ever Laravel project I was really trying to just stick with the basics and not over complicate things for myself which is why I chose not to use Jetstream or Fortify and tried to stick to this older approach of setting up authentication. However I've been stuck on this for a couple of hours now and have not been able to figure out what's going wrong which is why I'm now seeking some help with it.

Happy to provide extra details/project code if needed - any help I can get with this would be really appreciated.


Btw, this also happens to be my first ever post on StackOverflow so any feedback on my question or advice on how I can improve it would also be greatly appreciated.



Cheers,

adb

5 Answers

Adjust your last route to include some type of 'action', by removing that second argument (as it only takes 2 arguments):

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

I was getting the same error when making a simple route.

Route::get("/test", [ListPagesController::class, "index"]);

This was my code in the web.php file and I was routing my blade file in views via the controller. When I later redirected it directly to web.php this way without using a controller, the problem was resolved.

Route::get("/test", function () {return view("main");});

I don't quite understand why but the problem is solved.

You Should add it in line 2 web.php use App\Http\Controllers\YourControllerName;

For me, I changed:

Route::get('/branch/',  [Controller::class, 'get_branch_list'])->name('branch');

To:

Route::get('/branch/',  'Controller@get_branch_list')->name('branch');

I have changed the web.php file.

I was getting the same error when using the following route:

Route::post('/delete/{id}',[AdminController::class],'destroyProduct');

Changing the route using this code fixed the problem:

Route::post('/delete/{id}','App\Http\Controllers\AdminController@destroyProduct');

Related