Laravel admin/user route prefix approach - laravel

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.

Related

Laravel Subdomain routing, routes undefined inside Route Domain Group

I am trying to move all my routes inside a subdomain routes group. When I put routes inside subdomain group, issues arise. Routes become undefined. This way works.
Route::group(['middleware' => 'auth'], function () {
Route::get('dashboard', 'HomeController#dashboard')->name('dashboard');
Route::get('profile', ['as' => 'profile.edit', 'uses' => 'ProfileController#edit']);
Route::put('profile', ['as' => 'profile.update', 'uses' => 'ProfileController#update']);
});
This way does not work. Links generated with route helper generates undefined errror, href="{{ route('profile.edit') }}"
Route::group(['middleware' => 'auth'], function () {
Route::domain('{username}.'.env('SESSION_DOMAIN'))->group(function () {
Route::get('dashboard', 'HomeController#dashboard')->name('dashboard');
Route::get('profile', ['as' => 'profile.edit', 'uses' => 'ProfileController#edit']);
Route::put('profile', ['as' => 'profile.update', 'uses' => 'ProfileController#update']);
});
});
I thought I came up with a solution when I used url helper, href="{{ url('profile.edit') }}" , but a new problem started. Links to other pages were fine, but form submission actions generated a error
Missing required parameters
What am I doing wrong?
your route requires the subdomain parameter. {username} needs to be passed with your route but you are not passing anything. thus the missing required parameters error comes. you have to call a route like
href="{{ route('profile.edit', 'subdomain_name') }}"
or if you have a fixed subdomain then just remove {username} from your route definition and use the exact sub domain.
Route::group(['middleware' => 'auth'], function () {
Route::domain('subdomain_name.'.env('SESSION_DOMAIN'))->group(function () {
Route::get('dashboard', 'HomeController#dashboard')->name('dashboard');
Route::get('profile', ['as' => 'profile.edit', 'uses' => 'ProfileController#edit']);
Route::put('profile', ['as' => 'profile.update', 'uses' => 'ProfileController#update']);
});
});

Sharing routes between multiple prefixes

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

Laravel multi-nested same route parameters

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.

Resource route within a group route

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

Laravel - using route name in another route

I am trying to use named route in another route like this, but it doesnt work.
Route::get('/home', ['as' => 'home', 'uses' => 'HomeController#index']);
Route::post(route('home'), '.....');
You can use route('home', [], false) in this case. Third parameter false here is to use relative path instead of absolute (default set to true):
public function route($name, $parameters = array(), $absolute = true);
Laravel could do this for you by using a RESTful Resource Controller and allow GET and STORE (to match your example with GET and POST ->http://laravel.com/docs/5.0/controllers)
Route::resource('photo', 'PhotoController', ['only' => ['index', 'store']]);
Run php artisan route:list to see the names of the routes. Or a group of routes (http://laravel.com/docs/5.0/routing):
Route::group(['namespace' => 'admin'], function()
{
Route::get('home', ['as' => 'admin.home.index', 'uses' => 'HomeController#index']);
Route::get('castle', ['as' => 'admin.castle', 'uses' => 'HomeController#castle']);
});

Resources