Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Problems using array parameters on Route::group() #33

Open
mvilera opened this issue Aug 29, 2017 · 7 comments
Open

Problems using array parameters on Route::group() #33

mvilera opened this issue Aug 29, 2017 · 7 comments

Comments

@mvilera
Copy link

mvilera commented Aug 29, 2017

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.

@homersimpsons
Copy link

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

@marufmax
Copy link

marufmax commented Oct 3, 2018

I am having the same problem

@rahat1994
Copy link

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.

@AndikanGabriel
Copy link

This, actually worked

@geneowak
Copy link

geneowak commented Mar 4, 2020

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.

@paddelboot
Copy link

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');

});

@geneowak
Copy link

geneowak commented Mar 21, 2020

@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');
});

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants