13

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
5
  • 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? Commented Aug 12, 2017 at 9: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. Commented Aug 12, 2017 at 20:11
  • 1
    That could be, I used php 7 Commented Aug 12, 2017 at 20:15
  • Already solved with: Route::group(['namespace' => 'Admin']). In Laravel 5.4, there is no namespace() in Illuminate\Routing\Router. Use group(). Commented Aug 14, 2017 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): github.com/laravel/framework/blob/5.4/src/Illuminate/Routing/… Commented Aug 15, 2017 at 13:17

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
@adamyi Sadly I awarded the bounty to another answer by mistake without paying attention to the dialog confirm warning :(
6

I think you can try this:

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

});

Hope this work for you !!!

4 Comments

Thanks for your answer, truly I can do it this way (which I know) but specifically using the method in the question. Could you try if you get similar issue on a fresh project?
@OmisakinOluwatobi Glad to help
Of course its not the solution I expect, but yeah thanks for suggesting a solution. As I explained, I need to understand why I get that error when using namespace. I understand I can do it as you suggested but I don't want to do it that way unfortunately. Have you encountered this issue before?
@OmisakinOluwatobi No
3
Route::group([ 'prefix' => 'admin','namespace' => 'Admin','middleware' =>'admin'], function () {
// Controllers Within The "App\Http\Controllers\Admin" Namespace
});

Comments

3
+50

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

3 Comments

Thanks for your answer. In deed I did check this as you stated and found that I did it correctly yet still the same thing. I will update my PHP version in the question.
Could it be an issue with my php version? I checked Laravel doc for 5.4 and saw PHP >= 5.6.4?
there is syntax error in some line so better remove all the routes in your route file and just try only with above route
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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.