Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
milewski opened this issue Feb 6, 2016 · 5 comments
Closed

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

milewski opened this issue Feb 6, 2016 · 5 comments

Comments

@milewski
Copy link

milewski commented Feb 6, 2016

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"
]
@milewski milewski changed the title Route with an Array as action being mistaken with closure. [5.2] Route with an Array as action being mistaken with closure. Feb 6, 2016
@jimrubenstein
Copy link
Contributor

I also need this feature for a project I'm working on. I've got a [simple] solution written, and it works with 5.1.

Should I submit a PR against this issue + back to branch 5.1? Or back to Master?

@milewski
Copy link
Author

a simples solution i found out temporarily was creating a helper function

function classie($class, $method)
{
    $namespace = 'App\Http\Controllers\'';
    return str_replace($namespace, "", $class) . '@' . $method;
}

then i would use like this

Router::get('/', classie(HomeController::class, 'index'))->name('home');

@acasar
Copy link
Contributor

acasar commented Feb 17, 2016

Just delete the default namespace group: https://github.com/laravel/laravel/blob/master/app/Providers/RouteServiceProvider.php#L40

Then you can use the ::class syntax normally:

Route::get('/', HomeController::class . '@index');

@barryvdh
Copy link
Contributor

See also #11300

@milewski
Copy link
Author

@acasar cant believe that all i had to do was just add '@' in the method.... it did it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants