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
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.
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.
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With