In the routes app/routes/web.php I have a group with a bunch of routes for resources:
Route::group(['middleware' => 'auth'], function()
{
Route::get('/points', 'PagesController#points');
Route::get('/users', 'PagesController#users');
Route::get('/users/groups', 'PagesController#user_groups');
Route::resource('point', 'PointController');
Route::resource('user', 'UserController');
Route::resource('users/group', 'UserGroupController');
});
Auth::routes();
Route::get('/logout', 'Auth\LoginController#logout')->name('logout');
And now I want to distribute routes in this group by user parameter:
use App\UserGroup;
$access = UserGroup::find( auth()->user()->group_id )->access;
Route::group(['middleware' => 'auth'], function()
{
if ($access == 1) {
Route::get('/points', 'PagesController#points');
Route::get('/users', 'PagesController#users');
Route::get('/users/groups', 'PagesController#user_groups');
Route::resource('point', 'PointController');
Route::resource('user', 'UserController');
Route::resource('users/group', 'UserGroupController');
}
});
But I can't get user params right in the Route::group because an error occurs: Trying to get property 'group_id' of non-object
How to get auth()->user()->group_id in app/routes/web.php before middleware?
Use $access param in callback
$access = UserGroup::find( auth()->user()->group_id )->access
Route::group(['middleware' => 'auth'], function() use ($access)
{
if ($access == 1) {
Route::get('/points', 'PagesController#points');
Route::get('/users', 'PagesController#users');
Route::get('/users/groups', 'PagesController#user_groups');
Route::resource('point', 'PointController');
Route::resource('user', 'UserController');
Route::resource('users/group', 'UserGroupController');
}
});
U can create middleware 'GroupAccess'. Then assign it to alias, i.e.
'groupAccess' => \App\Http\Middleware\GroupAccess::class
Finally in it's handle method check your condition
public function handle($request, Closure $next)
{
if (UserGroup::find( auth()->user()->group_id )->access === 1)
return $next($request);
else
return back();
}
Then just specify this middleware in route group
Route::group(['middleware' => ['auth','groupAccess']], function()
{
Route::get('/points', 'PagesController#points');
Route::get('/users', 'PagesController#users');
Route::get('/users/groups', 'PagesController#user_groups');
Route::resource('point', 'PointController');
Route::resource('user', 'UserController');
Route::resource('users/group', 'UserGroupController');
});
Related
I'm a beginner with laravel and trying to figure out the routes. I searched the web but provided solutions don't help, so maybe you guys will be able to shed some light.
So below is my routes file (web.php)
Route::group(['middleware' => 'auth'], function () {
Route::get('/', [HomeController::class, 'home']);
Route::get('dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::get('billing', function () {
return view('billing');
})->name('billing');
Route::get('profile', function () {
return view('profile');
})->name('profile');
Route::get('rtl', function () {
return view('rtl');
})->name('rtl');
Route::get('user-management', function () {
return view('laravel-examples/user-management');
})->name('user-management');
Route::get('tables', function () {
return view('tables');
})->name('tables');
Route::get('virtual-reality', function () {
return view('virtual-reality');
})->name('virtual-reality');
Route::get('static-sign-in', function () {
return view('static-sign-in');
})->name('sign-in');
Route::get('static-sign-up', function () {
return view('static-sign-up');
})->name('sign-up');
Route::get('/logout', [SessionsController::class, 'destroy']);
Route::get('/user-profile', [InfoUserController::class, 'create']);
Route::post('/user-profile', [InfoUserController::class, 'store']);
Route::get('/login', function () {
return view('dashboard');
})->name('sign-up');
Route::get('/register', [RegisterController::class, 'create']);
Route::get('/user-list', [RegisterController::class, 'index'])->name('user-list');
Route::post('/register', [RegisterController::class, 'store']);
//Route::delete('user-destroy', [RegisterController::class, 'destroy'])->name('user-destroy');
Route::resource('users', RegisterController::class);
//Route::get('/viehicle-types', [ViehicleTypeController::class, 'index']);
// Route::resource('viehicle-types', ViehicleTypeController::class);
Route::group(['middleware' => ['role:admin|manager']], function () {
Route::resource('viehicle-types', ViehicleTypeController::class);
Route::resource('addon-types', AddonTypeController::class);
Route::resource('addons', AddonController::class);
Route::resource('job-types', JobTypeController::class);
Route::resource('jobs', JobController::class);
Route::resource('viehicles', ViehicleController::class);
Route::get('/finished-jobs/registerSelectJobType', [FinishedJobController::class, 'registerSelectJobType'])->name('xxxx');
Route::get('/finished-jobs/registerSelectJob/{jobTypeId}', [FinishedJobController::class, 'registerSelectJob'])->name('yyyy');
Route::resource('finished-jobs', FinishedJobController::class);
});
});
My problem is that some of the name routes won't work.
Mainly these 2 don't work and display a 404 Error:
Route::get('/finished-jobs/registerSelectJobType', [FinishedJobController::class, 'registerSelectJobType'])->name('xxxx');
Route::get('/finished-jobs/registerSelectJob/{jobTypeId}', [FinishedJobController::class, 'registerSelectJob'])->name('yyyy');
What is strange that when I check the routes:
$url=route('xxxx');
echo "The url is : " .$url;
I get the proper URL that works when entered in the web browser.
So I can't figure it out if anyone can help I will be greatful.
Cheers
HT
the controller is returning a blank data/view and I think something is wrong with my routes. if I remove {locale}, the data is retrieved.
Can anyone help with returning the data properly while my routes have {locale} in it? Here are my related code:
Web.php
Route::get('{locale}/projects/{id}/billings', 'ProjectController#showbilling')
->name('showbilling');
Route::post('{locale}/projects/{id}', 'ProjectController#addbilling')
->name('addbilling');
ProjectController.php
public function showbilling($id)
{
$billings = Project::find($id);
$locale = app()->getLocale();
return $billings;
//return view('admin.addbillings', compact('billings'));
}
Edit: Here's my full web.php
web.php
Route::get('/', function() {
return redirect(app()->getLocale());
});
Route::group(['prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'setlocale'], function () {
Route::get('/', function () {
return view('welcome');
})->name('main');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
//Customers
Route::get('/customers', 'CustomerController#showcust')->name('customers');
Route::post('/sendcust', 'CustomerController#sendcust')->name('sendcust');
//Items
Route::get('/items', 'ItemController#showitems')->name('items');
Route::post('/senditem', 'ItemController#senditem')->name('senditem');
//Projects
Route::get('/projects', 'ProjectController#showprojects')->name('projects');
Route::post('/sendproj', 'ProjectController#sendproj')->name('sendproj');
//ProjectBillings
Route::get('/projects/{id}/billings', 'ProjectController#showbilling')->name('showbilling');
Route::post('/projects/{id}', 'ProjectController#addbilling')->name('addbilling');
//Invoices
Route::get('/invoices', 'InvoiceController#showinvoice')->name('invoices');
Route::post('/sendinvoitem', 'InvoiceController#sendinvoitem')->name('sendinvoitem');
Route::get('/invoices/{id}/details', 'InvoiceController#showdetails');
Route::post('/updateitem','InvoiceController#updatedetail')->name('updateitem');
Route::get('invoices/{id}/generate', 'InvoiceController#generate');
Route::post('/updatestatus', 'InvoiceController#changestatus')->name('updatestatus');
});
You are passing 2 params in your route but accepting only 1 in the controller. Add locale.
public function showbilling($locale, $id)
in my application there are three routes which can be accessed by admin and superadmin Middleware but these routes only working in one Middleware i.e. superadmin Middleware.
This is my routes file what i am doing wrong here
// Admin Routes
Route::middleware(['admin'])->group(function () {
Route::get('admin', 'AdminController#index');
Route::get('admin/members', 'AdminController#members');
Route::get('admin/members/all', 'AdminController#membersAll');
Route::get('admin/members/unpaid', 'AdminController#membersUnpaid');
Route::post('admin/members/all', 'AdminController#membersAllAjax');
Route::get('admin/member/detail/{id}', 'AdminController#memberDetails')
->name('memberdetails');
Route::get('admin/member/remove/{id}', 'AdminController#memberRemove');
Route::get('admin/member/block/{id}', 'AdminController#memberBlock');
Route::get('admin/member/unblock/{id}', 'AdminController#memberunBlock');
Route::post('admin/member/ajax', 'AdminController#memberunAjax');
Route::get('admin/member/add', 'AdminController#addMember');
Route::post('admin/member/add', 'AdminController#addMemberDB');
Route::post('admin/send/message/all', 'AdminController#sendMessageAll');
Route::post('admin/send/message/single','AdminController#sendMessageSingle');
Route::post('admin/update/invoice', 'AdminController#updateInvoice');
Route::post('admin/user/to/member', 'AdminController#addUserMemberDB');
Route::get('admin/activities', 'AdminController#activities');
Route::post('admin/activities', 'AdminController#activitiesAdd');
Route::get('admin/donation', 'AdminController#donation');
Route::post('admin/edit/user/profile', 'AdminController#editUser');
});
// Admin and SubAdmin Routes
Route::middleware(['superadmin'])->group(function () {
Route::get('admin/members/all', 'AdminController#membersAll');
Route::post('admin/members/all', 'AdminController#membersAllAjax');
Route::get('admin/member/detail/{id}', 'AdminController#memberDetails')
->name('memberdetails');
Route::get('subAdmin', 'SuperAdminController#index');
});
Rewrite your routes using Route::group
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function () {
Route::get('/', 'AdminController#index');
Route::group(['prefix' => 'member'], function () {
Route::get('remove/{id}', 'AdminController#memberRemove');
Route::get('block/{id}', 'AdminController#memberBlock');
Route::get('unblock/{id}', 'AdminController#memberunBlock');
Route::post('admin/member/ajax', 'AdminController#memberunAjax');
Route::get('admin/member/add', 'AdminController#addMember');
Route::post('admin/member/add', 'AdminController#addMemberDB');
Route::group(['middleware' => 'superadmin'], function () {
Route::get('detail/{id}', 'AdminController#memberDetails')
->name('memberdetails');
});
});
Route::group(['prefix' => 'members'], function () {
Route::get('/', 'AdminController#members');
Route::get('unpaid', 'AdminController#membersUnpaid');
Route::get('detail/{id}', 'AdminController#memberDetails')
->name('memberdetails');
Route::group(['middleware' => 'superadmin'], function () {
Route::get('all', 'AdminController#membersAll');
Route::post('all', 'AdminController#membersAllAjax');
});
});
Route::group(['prefix' => 'send/message'], function () {
Route::post('all', 'AdminController#sendMessageAll');
Route::post('single','AdminController#sendMessageSingle');
});
Route::group(['prefix' => 'activities'], function () {
Route::get('/', 'AdminController#activities');
Route::post('/', 'AdminController#activitiesAdd');
});
Route::post('update/invoice', 'AdminController#updateInvoice');
Route::post('user/to/member', 'AdminController#addUserMemberDB');
Route::get('donation', 'AdminController#donation');
Route::post('edit/user/profile', 'AdminController#editUser');
});
the second configuration of the route Route::get('admin/members/all') wich needs to validate the superadmin middleware overwrites the first one wich only need to satisfy adminmiddleware.
if you try to access that route with superadmin priviledge, it will work.
now, if you want both admin && superadmin to access it, put only one configuration in the admin middleware group if the superadmin satisfies it too.
// Admin and SuperAdmin Routes
Route::group(['middleware' => ['admin']], function() {
Route::get('admin', 'AdminController#index');
Route::get('admin/members', 'AdminController#members');
}
//only SuperAdmin Routes
Route::group(['middleware' => ['superadmin']], function() {
Route::get('admin/members/all', 'AdminController#membersAll');
}
otherwise, make another middleware adminAndSuperAdmin for those routes.
// Admin only Routes
Route::group(['middleware' => ['admin']], function() {
Route::get('admin', 'AdminController#index');
}
//superAdmin only Routes
Route::group(['middleware' => ['superadmin']], function() {
Route::get('subAdmin', 'SuperAdminController#index');
});
//Admin and SuperAdmin Routes
Route::group(['middleware' => ['adminAndSuperAdmin']], function() {
Route::get('admin/members', 'AdminController#members');
Route::get('admin/members/all', 'AdminController#membersAll');
}
I need all the route under same prefix manager with one middleware for guest manager_guest and another for logged in user manager_auth.
This code bellow is my route web.php file.
Is there any other way ?
My routes:
Route::prefix('manager')->group(['middleware' => 'manager_guest'], function () {
Route::get('/register', 'Manager\RegisterController#showRegister')->name('manager.register.create');
Route::post('/register', 'Manager\RegisterController#register')->name('manager.register.store');
Route::get('/login', 'Manager\LoginController#showLogin')->name('manager.login.create');
Route::post('/login', 'Manager\LoginController#login')->name('manager.login');
});
Route::prefix('manager')->group(['middleware' => 'manager_auth'], function () {
Route::post('/logout', 'Manager\LoginController#logout')->name('manager.logout');
Route::get('/profile', 'Manager\PageController#profile')->name('manager.profile');
});
Error after executing php artisan route:list
PHP Warning: Uncaught ErrorException: Array to string conversion in E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php:329
Stack trace:
#0 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(329): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Array to string...', 'E:\\laragon\\www\\...', 3
29, Array)
#1 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(329): require()
#2 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(285): Illuminate\Routing\Router->loadRoutes(Array)
#3 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\RouteRegistrar.php(104): Illuminate\Routing\Router->group(Array, Array)
#4 E:\laragon\www\laraveladmin\routes\web.php(30): Illuminate\Routing\RouteRegistrar->group(Array, Object(Closure))
#5 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(329): require('E:\\laragon\\www\\...')
#6 in E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php on line 329
PHP Fatal error: Illuminate\Routing\Router::loadRoutes(): Failed opening required 'Array' (include_path='E:\Developer\Wbserver\php\PEAR') in E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate
\Routing\Router.php on line 329
[Symfony\Component\Debug\Exception\FatalErrorException] Illuminate\Routing\Router::loadRoutes(): Failed opening required 'Array' (include_path='E:\Developer\Wbserver\php\PEAR')
Try this instead
Route::group(['prefix' => 'manager', 'middleware' => 'manager_guest'], function() {
});
You could "factorize" your code like this:
Route::prefix('manager')->group(function () {
Route::middleware(['manager_guest'])->group(function () {
// These will be prefixed with "manager" and assigned the "manager_guest" middleware
});
Route::middleware(['manager_auth'])->group(function () {
// These will be prefixed with "manager" and assigned the "manager_auth" middleware
});
// These will just be prefixed with "manager"
});
I noticed all your controllers live in the sub-namespace Manager. You can chain the methods and make your routes file even cleaner. For instance:
Route::prefix('manager')->namespace('Manager')->group(function () {
Route::middleware(['manager_guest'])->group(function () {
Route::get('register', 'RegisterController#showRegister')->name('mananger.register.create');
});
Route::middleware(['manager_auth'])->group(function () {
Route::get('profile', 'PageController#profile')->name('mananger.profile');
});
});
None of the other answers worked for me as I had a lot of routes to change, and didn't want to change namespaces. The key to making this work is "as". The downside of this being that it changes the path when using "route()", but your use of name on each route here would override that anyway.
Route::group(['prefix' => 'manager', 'middleware' => ['manager_guest'], 'as' => 'manager_guest'], function() {
...
}
Route::group(['prefix' => 'manager', 'middleware' => ['manager_auth'], 'as' => 'manager_auth'], function() {
...
}
Hello i have 2 level users:
1 Super admin who is able to see and do everthing
2 TD which is able to only see stuff and not change anything
I made 2 middleware:
1 auth.superadmin and 1 auth.td
My routes:
Route::group(['middleware' => ['auth.superadmin']], function() {
Route::get('/users/{id}/destroy', 'UsersController#destroy');
Route::get('/searchuser', 'UsersController#searchuser');
Route::get('/users/create-worker', 'UsersController#getcreateworker');
Route::post('/users/post-create-worker', 'UsersController#postcreateworker');
Route::get('/users/create-agent', 'UsersController#getcreateagent');
Route::post('/users/post-create-agent', 'UsersController#postcreateagent');
Route::get('/users-optima', 'UsersController#indexoptima');
Route::resource('/users', 'UsersController');
Route::patch('/retours/{id}/postupdatefill','RetoursController#postupdatefill');
Route::get('/retours/{retourid}/addpart/{partid}','RetoursController#addpart');
Route::get('/retours/{retourid}/remove/{partid}','RetoursController#removepart');
Route::post('/retours/{retourid}/garantie','RetoursController#postonderdeelgarantie');
Route::get('/retours/{id}/updatefill/searchpart', 'RetoursController#searchpart');
Route::get('/searchpart', 'PartsController#searchpart');
Route::resource('/parts', 'PartsController');
});
Route::group(['middleware' => ['auth.td']], function() {
Route::get('/users/{id}/destroy', 'UsersController#destroy');
Route::get('/searchuser', 'UsersController#searchuser');
Route::resource('/users', 'UsersController',
['only' => ['index']]);
Route::patch('/retours/{id}/postupdatefill','RetoursController#postupdatefill');
Route::get('/retours/{retourid}/addpart/{partid}','RetoursController#addpart');
Route::get('/retours/{retourid}/remove/{partid}','RetoursController#removepart');
Route::post('/retours/{retourid}/garantie','RetoursController#postonderdeelgarantie');
Route::get('/retours/{id}/updatefill/searchpart', 'RetoursController#searchpart');
Route::get('/searchpart', 'PartsController#searchpart');
Route::resource('/parts', 'PartsController');
});
My middelware:
superadmin
if (auth()->check() && auth()->user()->level == 1) {
return $next($request);
}
return abort(404, 'no entry to this page');
TD
if (auth()->check() && auth()->user()->level == 2) {
return $next($request);
}
return abort(404, 'no entry to this page');
I tried beginning with /Users.
TD can only see the index at /Users.
When i do it this way the auth.superadmin cannot see index#/users...
Am i doing it wrong?
Any help is appreciated.
You can modify your routes and its groups like this:
Route::group(['middleware' => ['auth.td']], function() {
Route::get('/users/{id}/destroy', 'UsersController#destroy');
Route::get('/searchuser', 'UsersController#searchuser');
Route::resource('/users', 'UsersController',['only' => ['index']]);
Route::patch('/retours/{id}/postupdatefill','RetoursController#postupdatefill');
Route::get('/retours/{retourid}/addpart/{partid}','RetoursController#addpart');
Route::get('/retours/{retourid}/remove/{partid}','RetoursController#removepart');
Route::post('/retours/{retourid}/garantie','RetoursController#postonderdeelgarantie');
Route::get('/retours/{id}/updatefill/searchpart', 'RetoursController#searchpart');
Route::get('/searchpart', 'PartsController#searchpart');
Route::resource('/parts', 'PartsController');
Route::group(['middleware' => ['auth.superadmin']], function() {
Route::get('/users/{id}/destroy', 'UsersController#destroy');
Route::get('/searchuser', 'UsersController#searchuser');
Route::get('/users/create-worker', 'UsersController#getcreateworker');
Route::post('/users/post-create-worker', 'UsersController#postcreateworker');
Route::get('/users/create-agent', 'UsersController#getcreateagent');
Route::post('/users/post-create-agent', 'UsersController#postcreateagent');
Route::get('/users-optima', 'UsersController#indexoptima');
Route::resource('/users', 'UsersController');
Route::patch('/retours/{id}/postupdatefill','RetoursController#postupdatefill');
Route::get('/retours/{retourid}/addpart/{partid}','RetoursController#addpart');
Route::get('/retours/{retourid}/remove/{partid}','RetoursController#removepart');
Route::post('/retours/{retourid}/garantie','RetoursController#postonderdeelgarantie');
Route::get('/retours/{id}/updatefill/searchpart', 'RetoursController#searchpart');
Route::get('/searchpart', 'PartsController#searchpart');
Route::resource('/parts', 'PartsController');
});
});
and your auth:td middleware should be like this:
if (auth()->check() && (auth()->user()->level == 1 || auth()->user()->level == 2)) {
return $next($request);
}
return abort(404, 'no entry to this page');
Just for your knowledge you can either remove the outer middleware (auth:td) as both users can use the routes under it. But I haven't done that because I think you have more users in your system.
Hope this helps!