larapi icon indicating copy to clipboard operation
larapi copied to clipboard

Problems using array parameters on Route::group()

Open mvilera opened this issue 8 years ago • 7 comments

Hey there,

I'm having problems using parameters like 'middleware' on Route::group() facade static method,

And using param 'middleware' like this:

Route::group(['middleware' => 'role:admin'], function () {
        Route::post('create','AppointmentController@create');
});

The error message is: Symfony\Component\Debug\Exception\FatalErrorException: Illuminate\Routing\Router::loadRoutes(): Failed opening required 'Array' (include_path='.:/usr/share/php')

I'm aware that we can circumvent this problems by using middleware() on each post/get/method, but i was looking for a solution to avoid code duplication.

mvilera avatar Aug 29 '17 15:08 mvilera

I've got the "same" problem, now what you are looking for seems to work, but I tried to do:

Route::name('admin.')->group(['middleware' => 'role:admin'], function () {
        Route::post('create','AppointmentController@create');
});

and this fails... while it should works I guess. ref

homersimpsons avatar Jul 23 '18 12:07 homersimpsons

I am having the same problem

marufmax avatar Oct 03 '18 04:10 marufmax

Hello, I ran into this problem. Though I could have used middleware for each GET/POST... but to avoid code duplication I refactored my code from

//Client Side of the Application
Route::namespace('Clientside')->group(['middleware' => 'defaultdata'],function () {

    // Controllers Within The "App\Http\Controllers\Backoffice" Namespace
	Route::get('/', 'indexController@index');
	Route::get('/hotel_searches/show', 'SearchController@search_hotel');

});

to

//Client Side of the Application
Route::namespace('Clientside')->group(function () {

	Route::middleware(['defaultdata'])->group(function(){
		// Controllers Within The "App\Http\Controllers\Backoffice" Namespace
		Route::get('/', 'indexController@index');
		Route::get('/hotel_searches/show', 'SearchController@search_hotel');
	});

hope it helps someone.

rahat1994 avatar May 01 '19 13:05 rahat1994

Hello, I ran into this problem. Though I could have used middleware for each GET/POST... but to avoid code duplication I refactored my code from

//Client Side of the Application
Route::namespace('Clientside')->group(['middleware' => 'defaultdata'],function () {

    // Controllers Within The "App\Http\Controllers\Backoffice" Namespace
	Route::get('/', 'indexController@index');
	Route::get('/hotel_searches/show', 'SearchController@search_hotel');

});

to

//Client Side of the Application
Route::namespace('Clientside')->group(function () {

	Route::middleware(['defaultdata'])->group(function(){
		// Controllers Within The "App\Http\Controllers\Backoffice" Namespace
		Route::get('/', 'indexController@index');
		Route::get('/hotel_searches/show', 'SearchController@search_hotel');
	});

hope it helps someone.

What about refactoring it to this:

//Client Side of the Application
Route::group(['namespace'=>'Clientside', 'middleware'=>'defaultdata'], function () {
	// Controllers Within The "App\Http\Controllers\Backoffice" Namespace
	Route::get('/', 'indexController@index');
	Route::get('/hotel_searches/show', 'SearchController@search_hotel');
});

Got that idea from the best answer from the discussion here.

PS: I haven't tested using namespace in a group array out but am guessing it would work since prefix and middleware work.

geneowak avatar Mar 04 '20 08:03 geneowak

I am getting this error when I change

Route::group([

to

Route::namespace( 'Auth' )->group([

even though this is what the documentation suggests.

Full code:

Route::namespace( 'Auth' )->group([

'middleware' => 'api',
'prefix' => 'auth'

], function ($router) {

Route::post('register', 'AuthController@register');
Route::post('login', 'AuthController@login');
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');

});

paddelboot avatar Mar 19 '20 13:03 paddelboot

@paddelboot add the namespace to the group, I think that will solve it...

Try this:

Route::group(['namespace'=>'Auth', 'middleware'=>'api', 'prefix' => 'auth'], function () {
	Route::post('register', 'AuthController@register');
	Route::post('login', 'AuthController@login');
	Route::post('logout', 'AuthController@logout');
	Route::post('refresh', 'AuthController@refresh');
	Route::post('me', 'AuthController@me');
});

geneowak avatar Mar 21 '20 09:03 geneowak