Framework: Missing the namespace

Created on 23 Mar 2019  Â·  3Comments  Â·  Source: laravel/framework

  • Laravel Version: 5.8.4
  • PHP Version: 7.2.10
  • Database Driver & Version:5.5

Description:

Route::namespace('Admin')->get('/','IndexController@index');

I thought it will be hit method index of App\Http\Controllers\Admin\IndexController .But in fact it hit the method index of App\Http\Controllers\IndexController. I read the code and found something wrong Maybe.

Steps To Reproduce:

$route = Route::namespace('Admin')->get('/','IndexController@index');
dd($route);

Route {#261 â–¼
+uri: "/"
+methods: array:2 [â–¶]
+action: array:6 [â–¼
"middleware" => array:1 [â–¶]
"namespace" => "App\Http\Controllers\Admin"
"uses" => "App\Http\Controllers\IndexController@index" // missing namespace
"controller" => "App\Http\Controllers\IndexController@index" // missing namespace
"prefix" => null
"where" => []
]
....
}
The same things happend whe i used

Route::get('/',['namespace '=>'Admin','uses'=>IndexController@index']);

Ok,Let's look at two more examples.
domain.com/admin will hit both below.And that is different from namespaces.

Route::prefix('admin')->get('/',IndexController@index');
Route::get('/',['prefix' => 'admin','uses' => 'IndexController@index');

Suggestion:

// Illuminate\Routing\Router.php line 502
protected function convertToControllerAction($action)
{
    if (is_string($action)) {
        $action = ['uses' => $action];
    }
    // Here we'll merge any group "uses" statement if necessary so that the action
    // has the proper clause for this property. Then we can simply set the name
    // of the controller on the action and return the action array for usage.

    // Here I remove
    // if (! empty($this->groupStack)) {
    //    $action['uses'] = $this->prependGroupNamespace($action['uses']);
    //}

    // And replace it with the codes below . And it works what i expect.
    if (!empty($this->groupStack)) {
        $action['uses'] = isset($action['namespace'])
            ? $this->prependGroupNamespace(trim($action['namespace'], '\\') . '\\' . $action['uses'])
            : $this->prependGroupNamespace($action['uses']);
    }

    // Here we will set this controller name on the action array just so we always
    // have a copy of it for reference if we need it. This can be used while we
    // search for a controller name or do some other type of fetch operation.
    $action['controller'] = $action['uses'];
    return $action;
}

Forgive my English.I believe my Chinese will be better .It took me half an hour to write this.
Because I like this framework so much.So what u guys think.

Most helpful comment

It seems like namespace() is only meant to work with groups:

Route::namespace('Admin')->group(function () {
    Route::get('/','IndexController@index');
});

For a single route, it's easier to specify the namespace directly:

Route::get('/','Admin\IndexController@index');

All 3 comments

It seems like namespace() is only meant to work with groups:

Route::namespace('Admin')->group(function () {
    Route::get('/','IndexController@index');
});

For a single route, it's easier to specify the namespace directly:

Route::get('/','Admin\IndexController@index');

@staudenmeir
Thank you for your reply. But I believe it will make the framework more artistic and easier to understand.

The namespace method is only available for groups.

Was this page helpful?
0 / 5 - 0 ratings