Skip to content

[5.2] Route with an Array as action being mistaken with closure. #12187

Closed
@milewski

Description

@milewski

i would like to set up routes like this

$app->get('/', [HomeController::class, 'index']);
//or
$app->get('/')->uses([HomeController::class, 'index']);
//or
$app->get('/', HomeController::class, 'index');

So i would get refactoring on my IDE to work properly

However by inspecting the Laravel source code i found out this line

// If the action is already a Closure instance, we will just set that instance
// as the "uses" property, because there is nothing else we need to do when
// it is available. Otherwise we will need to find it in the action list.
if (is_callable($action)) {
  return ['uses' => $action];
}

On line 536 in Illuminate\Routing\Route@parseAction

As i see on the commentaries it is instead to receive a Closure instance... how ever the function is_callable() also returns true if it receives an array [ValidClassPath::class, ValidMethod] so it causes laravel to mistake the action with a Closure.

I feel it just need a fix on this parser... to check again it the action is really a closure or an array.

underlying the return code of the parser is

array:1 [▼
  "uses" => array:2 [▼
    0 => "App\Http\Controllers\HomeController"
    1 => "index"
  ]
]

instead of

array:2 [▼
  "uses" => "App\Http\Controllers\HomeController@index"
  "controller" => "App\Http\Controllers\HomeController@index"
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions