I need to have routes group with prefix and middlewares all in one but not sure how to do so? In laravel documents there is no such complex sample.
Here is what I want:
Route::group({Prefix}, {middleware}, function(){...});
I am aware that I can add middlewares at the end of my route groups like:
Route::group({Prefix}, function(){...})->middleware('xxxx');
But I like the shape of first sample (all in one at the top of group).
So anyone can help to figure that out?
Try something like this,
Route::group(['middleware' => 'cors', 'prefix' => '/v1/test'], function () {
Route::post('/', 'Admin\testController#create');
Route::post('/list', 'Admin\testController#list');
Route::post('/view', 'Admin\testController#view');
Route::post('/update', 'Admin\testController#update');
});
Try below code.
if you use multiple middleware.
Route::group(['prefix' => 'admin', 'middleware' => ['auth','admin']], function() {
});
Related
I'm using Laravel 5.5 and wonder if I can not only group routes by name prefixes and route prefixes but also controllers since they all use the same controller. The documentation didn't provide anything the like. In Laravel 4 there were implicit controllers available by using Route::controller() but that's not what I'm looking for since this won't be implicit. To cut a long story short, here's what I currently have:
Route::group([ 'prefix' => 'my-route', 'as' => 'myRoute.' ] , function () {
Route::get('/{viewMode?}', 'MyRouteController#index')->name('index')->where('viewMode', '[a-z]+');
Route::get('/ajax', 'MyRouteController#ajax')->name('ajax');
});
And it should look something like that:
Route::group([ 'prefix' => 'my-route', 'as' => 'myRoute.', 'controller' => 'MyRouteController' ] , function () {
Route::get('/{viewMode?}', 'index')->name('index')->where('viewMode', '[a-z]+');
Route::get('/ajax', 'ajax')->name('ajax');
});
Is there a way to achieve this kind of behavior?
Thanks in advance!
I have group of middleware in which I want to add the route but it does not working, the route group is following
Route::group(
[
'domain' => 'admin.'.env('APP_DOMAIN'),
'as' => 'admin.'
],
function () {
Route::group(['namespace' => 'Admin'], function() {
/* I am trying to add route here */
});
})
I need to add following route
Route::post('/dashboard/tokens-sale-record','Admin\DashboardController#totalSaleForChart')->name('tokensSaleRecords');
When I add this route inside above group then it is not working but when I add outside it is working fine. I am using route in ajax.
Can someone kindly let me know what is the issue. I would like to appreciate.
Thank you so much.
Because you already defined Admin\ namespace path in parent group. That's way, You don't use Admin again namespace path in routes in the group.
Can you try following route define.
Route::post('/dashboard/tokens-sale-record','DashboardController#totalSaleForChart')->name('tokensSaleRecords');
If you using again Admin\Dashboard, Laravel searching it DashboardController as the Admin\Admin\DashboardController.
Route::group(
[
'domain' => 'admin.'.env('APP_DOMAIN'),
'as' => 'admin.'
],
function () {
Route::group(['namespace' => 'Admin'], function() {
Route::post('/dashboard/tokens-sale-record','DashboardController#totalSaleForChart')->name('tokensSaleRecords');
});
});
There is no need to write admin before calling controller. It will check for Admin\Admin\DashboardController.
If you are not able to find right route then use php artisan route:list | grep tokens-sale-record to check for the right route.
I am just getting started with Laravel and find the route setup a little confusing. I am trying to create a few pages, that ultimately should have the struture:
domain.com/onboarding
domain.com/onboarding/skip
domain.com/onboarding/skip/anothersubview
etc.
Right now I have:
// Registered and Activated User Routes
Route::group(['middleware' => ['auth', 'activated', 'activity']], function () {
Route::get('/onboarding', 'UserController#Onboarding')->name('onboarding');
});
Would the solution here (and best practice) be to just add another route inside my Route::group, like:
Route::view('/onboarding/skip', 'onboarding.skip');
Is this the correct way of doing things?
use prefix :
Route::group(['prefix' => 'onboarding','middleware' => ['auth', 'activated', 'activity']], function () {
Route::get('/', 'UserController#Onboarding')->name('onboarding');
Route::get('/skip', 'UserController#OnboardingSkip')->name('onboarding_skip');
Route::get('/skip/anothersubview', 'UserController#OnboardingSkipSubview')->name('onboarding_skipsubview');
});
read more here : https://laravel.com/docs/5.6/routing
The structure I used in a few projects in the past looks like this:
Route::group(['prefix' => 'onboarding'], function(){
Route::group(['prefix' => 'something'], function(){
Route::get('/', function(){}); //onboarding/something
Route::group(['prefix' => 'somethingelse'], function(){
Route::get('/', function(){}); //onboarding/something/somethingelse
Route::get('/{id}', function(){}); //onboarding/something/somethingelse/15
});
});
});
nesting groups can help you make the easier extendable router because when you realize you need to add some URL in the middle of long structure it would be easier to do it with this concept
You have a web.php file in your routes folder, there you need to add:
Route::get('/subpage', 'controllername#function-name-you-want-to-call');
Hope this helps, if it doesn't let me know
/e: to clarify:
the first part
Route::group(['middleware' => ['auth', 'activated', 'activity']], function () {
is the authentication. Depending on who you want to access this page, you might not need it
I'm using this package in my project and there have default package routes.
Like this:
I want use this route in my controller. I'm trying to use with name but it did not work this way.
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
Voyager::routes();
});
And
Route::group(['prefix' => 'admin'], function () {
Voyager::routes();
})->name('admin');
I'm trying to use like this:
I want to give access like this, as if I'm trying to access 'admin' route then I could access all routes under these route group. I don't know how I will do that?
Please help me.
You cannot redirect to route with name admin. because such route doesn't exist.
When you use:
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
Voyager::routes();
});
it means all routes created by Voager::routes() will have name starting with admin. but it doesn't mean admin. route exist.
So I assume you should instead use rather admin.voyager.dashboard instead, so you should rather use:
return redirect()->route('admin.voyager.dashboard');
instead of:
return redirect()->route('admin.');
At the moment I'm working with laravel 5 project, which contains ~100 post and get routes. I'm trying to add middlewares here but logics behind this project is really complicated. Middlewares will do really important role here. Before I was using groups, for example:
Route::group(['middleware' => 'auth'], function(){
//routes
});
But everything became really messy since I had to create groups in group, for example:
Route::group(['middleware' => 'auth'], function(){
Route::group(['middleware' => 'status'], function(){
//routes
});
});
At the moment I have 20 controllers, so each of them contains about 5 routes. Could you suggest me more efficent way to use middlewares in big projects. What way do you use? Thanks in advance!
It all depends on which middlewares you need to have applied to different routes.
If you have groups of routes that share the same middleware set, then the easiest way to do is what you do in the first example:
Route::group(['middleware' => 'auth'], function(){
//routes
});
If you have some roots that share common middlewares, but each of them might have some specific middleware applied, then nesting routes and route groups withing existing group like you do in the second example is a way to go:
Route::group(['middleware' => 'auth'], function(){
Route::group(['middleware' => 'status'], function(){
//routes
});
Route::get('/uri/', ['middleware' => 'some_other_middleware']);
});
Finally, when different routes have different middleware sets and you are not able to group them in any way, you'll need to apply a set of middlewares to each of them:
Route::get('/uri1/', ['middleware' => 'some_middleware']);
Route::get('/uri2/', ['middleware' => 'some_other_middleware']);
Long story short - if you have complex rules on what middleware to apply to what routes, then setting it up in the routes.php file will reflect the complexity.
It might be also true that some of the things you do in the middleware should belong to some other layer in the application and moving the logic there could simplify routes.php, but it all depends on what routes and middlewares you have.