11

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.

enter image description here

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
    I just installed a fresh version of Laravel (5.4.32, also downgraded to 5.4.30) and added the first route you described to my routes. `php artisan serve` throws no error, have I missed anything? – cre8 Aug 12 '17 at 09:45
  • @mimo indeed what you said is proven again by one of the answer. I now suspect my php version 5.6.30 because the doc says `>=5.6.4` I have to check this. – Oluwatobi Samuel Omisakin Aug 12 '17 at 20:11
  • 1
    That could be, I used php 7 – cre8 Aug 12 '17 at 20:15
  • Already solved with: `Route::group(['namespace' => 'Admin'])`. In Laravel 5.4, there is no namespace() in Illuminate\Routing\Router. Use group(). – doncadavona Aug 14 '17 at 14:05
  • 1
    @doncadavona There *is* a helper on Route for namespace. It's not explicit, but allowed in the `__call` method of `RoutingRegistrar`, as shown here (`namespace` is in the `allowedAttributes` array): https://github.com/laravel/framework/blob/5.4/src/Illuminate/Routing/RouteRegistrar.php#L163 – Jake Bathman Aug 15 '17 at 13:17

5 Answers5

8

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

adamyi
  • 547
  • 2
  • 15
  • This answer speaks a lot about the issue and I hope Laravel makes it clear in this sense. Even though I have however not been able to test the issue out on PHP 5.6.4 and above (because of some difficulty I would have in doing that) so as to see the practical difference but the answer have all the enough proofs I need to know where the fault comes from, at least. – Oluwatobi Samuel Omisakin Aug 16 '17 at 16:45
  • Also at least it works on php7 with L5 which affirms the correctness of the answer. Got a good lesson to read through the documentation well about the requirements before jumping into any framework. Thanks for the research. – Oluwatobi Samuel Omisakin Aug 16 '17 at 16:47
  • @adamyi Sadly I awarded the bounty to another *answer* by mistake without paying attention to the dialog confirm warning :( – Oluwatobi Samuel Omisakin Aug 16 '17 at 16:51
6

I think you can try this:

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

});

Hope this work for you !!!

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
3
Route::group([ 'prefix' => 'admin','namespace' => 'Admin','middleware' =>'admin'], function () {
// Controllers Within The "App\Http\Controllers\Admin" Namespace
});
Kuldeep Mishra
  • 3,846
  • 1
  • 21
  • 26
3

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

Hamelraj
  • 4,676
  • 4
  • 19
  • 42
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.

doncadavona
  • 7,162
  • 9
  • 41
  • 54
  • Sure you are correct that using it that way will work and indeed works. My issue is just why the doc says it can be used that way, and from the response of some people using the method works (with php 7). So I want to believe its the problem of PHP having those as reserved words just as @adamyi says. I'll have to validate this on another project with 7.0 as well. – Oluwatobi Samuel Omisakin Aug 14 '17 at 14:33