Is starting route grouping with namespace() not allowed in Laravel 5.4?

柔情痞子 提交于 2019-12-04 02:41:11

In short, it is a PHP problem, and a not well-documented thing of Laravel (this can only work in PHP 7 but not 5.x). It's not a problem on your side, so relax~


Starting PHP 5.3, namespace is added and hence cannot be used as function name.

According to http://docs.php.net/manual/en/migration53.incompatible.php:

The following keywords are now reserved and may not be used in function, class, etc. names.

  • goto
  • namespace

For more information regarding to namespace keyword in PHP, please take a look at http://php.net/manual/en/language.namespaces.nsconstants.php.

(as for why Route::prefix('')->namespace('Admin') works, it's probably an issue of the PHP parser, yet in general PHP 5.x is designed not to support this sort of method naming)


The code actually runs well since PHP 7. According to http://php.net/manual/en/reserved.keywords.php:

These words have special meaning in PHP. Some of them represent things which look like functions, some look like constants, and so on - but they're not, really: they are language constructs. You cannot use any of the following words as constants, class names, function or method names. Using them as variable names is generally OK, but could lead to confusion.

As of PHP 7.0.0 these keywords are allowed as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name.

namespace is one of those keywords. Starting PHP 7, they could be used as method names. So if you really want to use this method of Laravel, you need to upgrade to PHP 7.

Or, you could use other ways to use this feature without using the namespace method, as mentioned in your question and other answers.

Hope this solves your concerns. ^_^

I think you can try this:

Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'admin'], function () {

});

Hope this work for you !!!

Route::group([ 'prefix' => 'admin','namespace' => 'Admin','middleware' =>'admin'], function () {
// Controllers Within The "App\Http\Controllers\Admin" Namespace
});

actually this name Route::namespace() we are using for this

Ex: when you have controller in Admin folder (App\Http\Controllers\Admin;) you can use like this

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

so if don't use namespace then you have to use like this

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

but make sure in your HomeController in top you have to change namespace like this

namespace App\Http\Controllers; to namespace App\Http\Controllers\Admin;

I have checked with Laravel 5.4.3 Server - XAMPP PHP - 7.0 :)

The issue is that Illuminate\Routing\Router does not have a namespace() function.

To apply namespace to routes, use group():

Route::group(['namespace' => 'Admin'], function() {

  // Other routes under the Admin namespace here...

});

I'm not sure why the docs uses namespace() and group() fluently. But clearly namespace() is not in the code for all I know as of now.

Reference: https://laravel.com/api/5.4/Illuminate/Routing/Router.html.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!