laravel/framework

When namespaces begin with `\` they should reset.

AlbertMarashi opened this issue · 11 comments

  • Laravel Version: 5,4

Description:

I'm working with the Laravel Passport.

In my code I have this:

Route::group(['namespace' => 'SomeNamespace'], function(){ //App\Http\Controllers\SomeNamespace
    Route::get('', function(){
        return 'something';
    });
    
    \Laravel\Passport\Passport::routes();
});

I've been getting the error:

[ReflectionException]
Class App\Http\Controllers\SomeNamespace\Laravel\Passport\Http\Controllers\AuthorizationController does not exist

Now I've found the problem, which lies in the Passport.php file

   /**
     * Get a Passport route registrar.
     *
     * @param  array  $options
     * @return RouteRegistrar
     */
    public static function routes($callback = null, array $options = [])
    {
        $callback = $callback ?: function ($router) {
            $router->all();
        };

        $options = array_merge($options, [
            'namespace' => '\Laravel\Passport\Http\Controllers', //Here
        ]);

        Route::group($options, function ($router) use ($callback) {

            $callback(new RouteRegistrar($router));
        });
    }

As you can see at //Here the namespace doesn't reset, like normal php would.

I understand that if it was Laravel\... instead of \Laravel\... it would make sense but this goes against php standards, wouldn't you agree?

I'll try find the problem in the Route files and create a pull request.

Pull Request #18081

Thanks for the PR, closing since it's a Passport issue and should be reported on Passport repo.

@themsaid It's not, check the pull request.

The root problem lays in laravel's RouteGroup.php

Why do you have \Laravel\Passport\Passport::routes(); in a route group? it should be in AuthServiceProvider.

I understand that, However I have multiple API Versions which are under different namespaces.

I understand I could do this in AuthServiceProvider but it's duplicating code:

/**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();
        Route::group(['prefix/api'], function(){
            Route::group(['v1.0'], function(){
                \Laravel\Passport\Passport::routes();
            });
            Route::group(['v1.1'], function(){
                \Laravel\Passport\Passport::routes();
            });
            Route::group(['v1.2'], function(){
                \Laravel\Passport\Passport::routes();
            });
        });
    }

But it seems very impractical and hard to understand to future developers. This pull request would fix it and not cause any problems for others unless they created bad code.

I have other routes, each with their own namespaces and prefixes, I should be able to register my routes there.

I still believe this is a Passport issue

There's no way passport could fix it, because route group namespaces always check for the old namespace even if they begin with \. If you take a moment to read my pull request, you will understand.

Actually I don't think it's an issue, it's just how things are designed to work, it looks to me like some change that you want to apply to support a certain scenario, in this case you're free to open a PR to suggest the change or suggest it on the https://github.com/laravel/internals, it's where we keep such stuff :)

Understandable, however PHP Global spaces work like this, why should laravel work differently, just kinda goes against php practices