-->

Is starting route grouping with namespace() not al

2020-07-05 06:50发布

问题:

Using Laravel 5.4, indeed in the documentation about Route grouping, and an example as this was given about namespacing:

Route::namespace('Admin')->group(function () {
   // Controllers Within The "App\Http\Controllers\Admin" Namespace
});

This according to the doc is okay, but after installing Laravel 5.4.30 I discovered that doing the above throws the following error:

PHP Parse error:  syntax error, unexpected 'namespace' (T_NAMESPACE) in /Applications/MAMP/htdocs/my_app/routes/web.php on line

Even though I did a workaround by using other route methods before it such as the following:

Route::prefix('')->namespace('Admin')->group(function () {
   // Controllers Within The "App\Http\Controllers\Admin" Namespace
});

Yet, Is this a bug in Laravel or something that I did't suspect is the issue in my code?.

If there is a need to provide more explanations, then I am glad to do that.

Update: As @Adweb suggested, it can be done using group(['namespace' => 'Admin'])... but I am really still keen on what could be the issue based on the error I got.

Here is my PHP version:

PHP 5.6.30 (cli) (built: Mar 11 2017 09:56:27) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

回答1:

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. ^_^



回答2:

I think you can try this:

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

});

Hope this work for you !!!



回答3:

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


回答4:

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 :)



回答5:

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.