I'm trying to cut down on the size of my routes file and re-use named routes. I have two separate areas that are authenticated and have their own specialized routes, however, both of them share a LOT of other routes in common.
Route::group(['middleware' => 'web'], function () {
/**
* Author routes.
*/
Route::group(['prefix' => 'author', 'middleware' => 'auth'], function () {
Route::get('/', ['as' => 'dashboard', 'uses' => 'Controller#showHome']);
// ...various routes unique to authors...
Route::any('posts/data', ['as' => 'posts.data'])->uses('PostsController#data');
Route::get('posts/{account?}', ['as' => 'posts.show'])->uses('PostsController#index');
Route::get('posts/{post}/delete', ['as' => 'posts.delete'])->uses('PostsController#destroy');
Route::resource('posts', 'PostsController', ['parameters' => 'singular']);
// ...lots more routes like the above shared with reviewers...
});
/**
* Reviewer routes.
*/
Route::group(['prefix' => 'reviewer', 'middleware' => 'auth'], function () {
Route::get('/', ['as' => 'dashboard', 'uses' => 'Controller#showHome']);
// ...various routes unique to reviewers...
Route::any('posts/data', ['as' => 'posts.data'])->uses('PostsController#data');
Route::get('posts/{account?}', ['as' => 'posts.show'])->uses('PostsController#index');
Route::get('posts/{post}/delete', ['as' => 'posts.delete'])->uses('PostsController#destroy');
Route::resource('posts', 'PostsController', ['parameters' => 'singular']);
// ...lots more routes like the above shared with authors...
});
});
I still need a reviewer to go to example.com/reviewer/posts to do all post related activities and authors to go to example.com/author/posts.
How can I make this a lot less verbose?
Create a separate route file e.g. post_routes.php and put all your shared Post route in there.
Include the route file
Route::group(['prefix' => 'author', 'middleware' => 'auth'], function () {
require app_path('Http/post_routes.php');
});
/**
* Reviewer routes.
*/
Route::group(['prefix' => 'reviewer', 'middleware' => 'auth'], function () {
require app_path('Http/post_routes.php');
});
Related
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 creating internet-marketn on Laravel 5.2
Need to do something like a breadcrumps in url with categories slug,
like so
/catalog/{category_1}/{category_2}/{category_n}
but get only last parameter and bind it into Model
moreover last category can has product slug and route can be like
/catalog/{category_1}/{category_2}/{category_n}/product/{product_slug}
my routing
Route::group(['prefix' => 'catalog'], function () {
Route::get('/', ['as' => 'shop_catalog', 'uses' => 'CatalogController#catalog']);
Route::group(['prefix' => '{category}'], function () {
Route::get('/', ['as' => 'shop_show_category', 'uses' => 'CategoryController#category']);
Route::group(['prefix' => 'product/{product}'], function () {
Route::get('/', ['as' => 'shop_show_product', 'uses' => 'ProductController#product']);
});
Try write pattern in RouteServiceProvider like
$route->pattern('category', '.*');
Then explode by '/' and get last element for binding. But can't get product parameter.
How can I do this logic?
The ordering of your routes is really important.
As the shop_show_category route is listed first, the router never gets to check if the url more specifically matches your shop_show_product route.
Route::group(['prefix' => '{category}'], function () {
Route::group(['prefix' => 'product/{product}'], function () {
Route::get('/', ['as' => 'shop_show_product', 'uses' => 'ProductController#product']);
});
Route::get('/', ['as' => 'shop_show_category', 'uses' => 'CategoryController#category']);
});
By rearranging so the more specific route is first, you know the router will check the shop_show_product route for a match first. Failing it seeing product/xxx at the end of the url it will try to match the shop_show_category route.
so I have to make this system for transport management. The user can log in create/update/edit all his trips. But the admin can do the same for all users. I have divided user and admin in to route prefixes:
Route::group(['prefix' => 'admin/', 'middleware' => ['auth','admin']], function(){
Route::resource('trips', 'TripsController',
array('except' => array('show')));}
Route::group(['prefix' => 'user/', 'middleware' => ['auth', 'user']], function(){
Route::resource('trips', 'TripsController',
array('except' => array('show')));
}
The problem is in every method of the TripController I have to pass route variable with the correct url (the admin request will have a 'admin' prefix, and the users will have 'user' prefix)
return View('trips.index', compact('users', 'route'));
The question is there a way to do this nicely or should I just pull the trips Route::resource out of the both groups so that it wouldn't have any groups? What is the correct approach here?
I use this approach:
Route::group(['namespace' => 'Admin', 'as' => 'admin::', 'prefix' => 'admin'], function() {
// For Other middlewares
Route::group(['middleware' => 'IsNotAuthenticated'], function(){
// "admin::login"
// http://localhost:8000/admin/login
Route::get('login', ['as' => 'login', 'uses' => 'AdminController#index']);
});
// For admin middlewares
Route::group(['middleware' => 'admin'], function(){
// "admin::admin.area.index"
// http://localhost:8000/admin/area/{area}
Route::resource('Area', 'AreaController');
// "admin::dashboard"
// http://localhost:8000/admin/
Route::get('/', ['as' => 'dashboard', 'uses' => 'AdminController#dashboard']);
});
});
Whenever I need to access url in blade templates I simply use route helper method.
// For resourceful routes
{{ route('admin::admin.city.index') }}
or
//For regular get/post routes
{{ route('admin::dashboard') }}
Or simply run artisan command to list route names.
php artisan route:list
I did it with this:
//Admin Group&NameSpace
Route::namespace('Admin')->prefix('admin')->group(function () {
Route::get('/dashboard', 'DashboardController#index')->name('dashboard')->middleware('auth');
});
Even you can customize the ->middleware('auth'); with a custom middleware role based.
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
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']);
});