My named route group is working fine without resource route. But when I am trying to use 'resource route' then getting this error. Would someone help me please, in where I am doing wrong?
My Route Group is -
Route::group(['prefix' => 'admin', 'as' => 'admin.', 'middleware' => 'auth:admin'], function () {
Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'Admin\AdminController#dashboard'));
Route::group(['prefix' => 'student', 'as' => 'student.'], function () {
Route::resource('admission', array('as' => 'admission', 'uses' => 'Admin\StudentController'));
}); });
You need to pass resource controller name as string as the second parameter for Route::resource():
Route::resource('admission', 'Admin\StudentController');
You don't need to specify routes names with 'as' => 'admission' because the Route::resource() will do that automatically.
Related
Which is valid way in laravel 5.7 to run several urls on the same control action, like
Route::get('about', array(
'as' => 'page-about',
'uses' => 'PageController#page_content'
))->AdditiveParameter('about');
Route::get('security', array(
'as' => 'page-security',
'uses' => 'PageController#page_content'
))->AdditiveParameter('security');
and In PageController page_content action have 1 required parameter ?
Thanks!
I am not sure if you are looking for this only, but hope it will give some idea.
Route::group(['prefix' => '{prefix?}', 'where' => ['prefix' => 'content']],function (){
Route::get('{route_name}/{req_param}', array(
'as' => 'page-security',
'uses' => 'PageController#page_content'
))->where('route_name', '(about|security)');
});
And Controller action will be something like this
public function page_content($route_name,$req_param)
{
}
Let me know if this is working for you.
I need to secure my backend section.
Right now i have i like this:
Route::get('/backend',['middleware' => 'auth', 'uses' => 'HomeController#index']);
Route::get('/backend/users',['middleware' => 'auth', 'uses' => 'HomeController#show']);
Route::get('/backend/users/create',['middleware' => 'auth', 'uses' => 'HomeController#create']);
Route::get('/backend/users/edit/{id}',['middleware' => 'auth', 'uses' => 'HomeController#edit']);
do i need to write the middleware=> auth to everyline and everysite i have in my backend?
Is it somehow possible to define that everything that has 'backend/' should be checked if auth or not?
You can use a Route Group to define middleware and a prefix (among other things). So it could be:
Route::group(['prefix' => 'backend', 'middleware' => 'auth'], function () {
Route::get('/', 'HomeController#index');
Route::get('/users', 'HomeController#show');
Route::get('/users/create', 'HomeController#create');
Route::get('/users/edit/{id}', 'HomeController#edit');
)};
I am trying to group all the routes for our admin section to access model resources. So far I've come with this:
Route::group(['middleware' => 'auth', 'prefix' => 'admin', 'as' => 'admin::'], function() {
Route::get('dashboard', ['as' => 'dashboard', function() {
return view('pages.dashboard');
}]);
Route::resource('user', 'UserController', ['as' => 'user']);
Route::resource('plan', 'PlanController', ['as' => 'plan']);
Route::resource('answer', 'AnswerController', ['as' => 'answer']);
Route::resource('question', 'QuestionController', ['as' => 'question']);
Route::resource('retailer', 'RetailerController', ['as' => 'retailer']);
Route::resource('restriction', 'RestrictionController', ['as' => 'restriction']);
});
I want to name these routes to access them in a much easier manner by calling their names. However it breaks and says "Route [admin::user] not defined." I want to use the route naming feature to use route('admin::user'). I am having problem with the resource routes. The dashboard one works fine - route('admin::dashboard')
I take from this post that naming resource routes should work (Laravel named route for resource controller)
Resources are given route names automatically run php artisan route:list to list the routes out:
Route::group(['middleware' => 'auth', 'prefix' => 'admin', 'as' => 'admin::'], function() {
Route::get('dashboard', ['as' => 'dashboard', function() {
return view('pages.dashboard');
}]);
Route::resource('user', 'UserController');
});
Resulting Routes
admin::dashboard
admin::admin.user.store
admin::admin.user.index
admin::admin.user.create
admin::admin.user.destroy
admin::admin.user.show
admin::admin.user.update
admin::admin.user.edit
Would like to check say that I have the following routes
Route::group(['middleware' => 'auth'], function(){
Route::get('/{profile_url?}', array('as' => 'profile', 'uses' => 'ProfileController#getProfile'));
Route::get('/settings/password', array('as' => 'chgPassword', 'uses' => 'ProfileController#updatePassword'));
Route::post('/settings/password', array('as' => 'postChgPassword', 'uses' => 'ProfileController#postUpdatePassword'));
Route::get('/settings/email/request', array('as' => 'chgEmailRequest', 'uses' => 'ProfileController#updateEmailRequest'));
Route::post('/settings/email/request', array('as' => 'postChgEmailRequest', 'uses' => 'ProfileController#postUpdateEmailRequest'));
Route::get('/logout', array('as' => 'logout', 'uses' => 'ProfileController#logout'));
});
Notice that my first route accepts an optional parameter which will then route the user to a specific profile which it works fine, but when ever i have other routes say that /logout, laravel router will also use the /{profile_url?} route instead of the expected logout route. Is there any way that i can specified something like a reserved keyword like
Route::get('/{profile_url?}', array('as' => 'profile', 'uses' => 'ProfileController#getProfile')
->except('settings', 'logout'));
something like that? Ho that someone can enlighten me with this issue.
Because you put a wildcard {profile_url?} at the first place, Laravel will ignore the rest. So be careful when using wildcard routes. you should put the least specific route in the last place, Lavarel will check all of specific routes. If it doesn't match, it will go to the wildcard route. For example :
Route::group(['middleware' => 'auth'], function(){
Route::get('/{profile_url?}',...); // Lavarel do this
Route::get('/logout',...); // ignore this
});
Route::group(['middleware' => 'auth'], function(){
Route::get('/logout',...); // do this if it matches
Route::get('/{profile_url?}',...); // else do this
});
I have the following code in routes.php:
Route::group(['prefix' => 'dev/order'], function() {
Route::get('create', ['as' => 'dev.order.create', 'uses' => 'OrderController#create']);
Route::get('create-pack', ['as' => 'dev.order.create-pack', 'uses' => 'OrderController#createPack']);
}
);
How can I get rid of duplicate action name, 'dev.order' and 'OrderController' substrings in parameters? Route::controller() and Route::resource() creates unnamed routes when viewed via
php artisan routes
While I need a group of named routes to one controller with common prefix.
Unfortunatelly, there is no route name prefix yet in Laravel, so, to remove repetition of strings, you can do things like this:
$prefix = 'dev.order.';
$controller = 'OrderController#';
Route::group(['prefix' => 'dev/order'], function() use ($prefix, $prefix)
{
Route::get('create', ['as' => $prefix.'create', 'uses' => $controller.'create']);
Route::get('create-pack', ['as' => $prefix.'create-pack', 'uses' => $controller.'createPack']);
});