2

I found no error on my router. but when running an error occurs. what's wrong with my router?

MY router

<?php

use App\Http\Controllers\{HomeController, DashboardController};
use Illuminate\Support\Facades\{Route, Auth};


Auth::routes();

Route::get( uri: '/', action: HomeController::class)->name(name: 'home');

Route::middleware( middleware: 'auth')->group( callback: function () {

    Route::get( uri: 'dashboard', action: DashboardController::class)->name(name: 'dashboard');

});

the error message is like this. It's the first time I've come across an error like this, and I looked it up but I couldn't find the same article.

   ErrorException 

  Undefined array key 0

  at vendor/laravel/framework/src/Illuminate/Routing/Router.php:1325
    1321▕             return $this->macroCall($method, $parameters);
    1322▕         }
    1323▕ 
    1324▕         if ($method === 'middleware') {
  ➜ 1325▕             return (new RouteRegistrar($this))->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters);
    1326▕         }
    1327▕ 
    1328▕         return (new RouteRegistrar($this))->attribute($method, $parameters[0]);
    1329▕     }

      +2 vendor frames 
  3   routes/web.php:11
      Illuminate\Support\Facades\Facade::__callStatic()

      +4 vendor frames 
  8   app/Providers/RouteServiceProvider.php:48
      Illuminate\Routing\RouteRegistrar::group()
1
  • .i have updated post .why it thrown error.Its just for your reference Commented Jun 14, 2021 at 15:32

1 Answer 1

2

The error is in Route::middleware( middleware: 'auth') it should be like below.

Route::middleware('auth')->group( callback: function () {
   Route::get( uri: 'dashboard', action: DashboardController::class)->name(name: 'dashboard');

});

middleware() not accepting named parameter because its not a real function.it is implemented via magic __call method so it does not support named variables.

/**
     * Dynamically handle calls into the router instance.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        if (static::hasMacro($method)) {
            return $this->macroCall($method, $parameters);
        }

        if ($method === 'middleware') {
            return (new RouteRegistrar($this))->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters);
        }

        return (new RouteRegistrar($this))->attribute($method, $parameters[0]);
    } }

Ref:https://github.com/laravel/framework/blob/8.x/src/Illuminate/Routing/Router.php#L1318

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.