Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is starting route grouping with namespace() not allowed in 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
});

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
like image 608
Oluwatobi Samuel Omisakin Avatar asked Aug 09 '17 13:08

Oluwatobi Samuel Omisakin


People also ask

What does a namespace Route Group allow?

Route groups allow you to share route attributes, such as middleware or namespaces, across a large number of routes without needing to define those attributes on each individual route. Shared attributes are specified in an array format as the first parameter to the Route::group method.

What is namespace in Laravel route?

The namespace on the route, is to define where the controller is for that route in Laravel. You don't have to use ->namespace() , you can do Route::get('something', 'MyNamespace\SomeController@method'); 0.

What is namespace routing?

In its simplest form, a Namespace Routing Language (NRL) schema consists of a mapping from namespace URIs to schema URIs. An NRL schema is written in XML. DSDL Part 4 (ISO/IEC 19757-4), NVDL is based on NRL.


1 Answers

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

like image 73
Hamelraj Avatar answered Oct 08 '22 17:10

Hamelraj