Laravel 5.4中是否不允许使用namespace()开始路由分组? [英] Is starting route grouping with namespace() not allowed in Laravel 5.4?

查看:345
本文介绍了Laravel 5.4中是否不允许使用namespace()开始路由分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确实在有关路由分组的文档中使用了Laravel 5.4,并给出了一个有关命名空间:

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
});

根据文档,这是可以的,但是在安装Laravel 5.4.30之后,我发现执行上述操作会引发以下错误:

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
});

但是,这是Laravel中的错误还是我的代码中没有怀疑的问题?.

如果需要提供更多解释,那么我很乐意这样做.

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

更新:正如@Adweb所建议的那样,可以使用group(['namespace' => 'Admin'])...完成此操作,但我仍然非常热衷于基于我收到的错误可能是什么问题.

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.

这是我的PHP版本:

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

推荐答案

简而言之,这是一个PHP问题,是Laravel文献中没有记载的东西(仅在PHP 7中有效,而在5.x中则无效).在你这边没问题,所以放松一下吧!

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~

从PHP 5.3开始,添加了名称空间,因此不能用作函数名称.

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

根据 http://docs.php.net/manual/zh/migration53.incompatible.php :

以下关键字现已保留,不能在以下关键字中使用 函数,类等名称.

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

  • 转到
  • 命名空间

有关PHP中的名称空间关键字的更多信息,请查看 http ://php.net/manual/en/language.namespaces.nsconstants.php .

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

(关于为什么Route::prefix('')->namespace('Admin')起作用的原因,这可能是PHP解析器的问题,但是一般而言,PHP 5.x被设计为不支持这种方法命名)

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

从PHP 7开始,该代码实际上运行良好.根据 http://php.net /manual/en/reserved.keywords.php :

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

这些词在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.

从PHP 7.0.0开始,这些关键字可以用作property,constant和 类,接口和特征的方法名称,但该类可以 不能用作常量名称.

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.

命名空间是那些关键字之一.从PHP 7开始,它们可用作方法名称.因此,如果您真的想使用Laravel的这种方法,则需要升级到PHP 7.

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

这篇关于Laravel 5.4中是否不允许使用namespace()开始路由分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆