Laravel Routes Group-Prefix using variable - laravel

I'm trying to use a variable (which contains a value stored in the session) as a prefix to all my group routes, so I can make it more readable and clean.
Basically I want to transform this:
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
Route::get('dashboard', 'App\Http\Controllers\RolesController#index')->name('admin.dashboard');
Route::get('profile', 'App\Http\Controllers\RolesController#profile')->name('admin.profile');
Route::get('editRegs', 'App\Http\Controllers\RolesController#editRegs')->name('admin.edit');
Route::get('settings', 'App\Http\Controllers\RolesController#settings')->name('admin.settings');
});
Route::group(['prefix' => 'user', 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
Route::get('dashboard', 'App\Http\Controllers\RolesController#index')->name('user.dashboard');
Route::get('profile', 'App\Http\Controllers\RolesController#profile')->name('user.profile');
Route::get('settings', 'App\Http\Controllers\RolesController#settings')->name('user.settings');
});
Route::group(['prefix' => 'manager', 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
Route::get('dashboard', 'App\Http\Controllers\RolesController#index')->name('manager.dashboard');
Route::get('profile', 'App\Http\Controllers\RolesController#profile')->name('manager.profile');
Route::get('settings', 'App\Http\Controllers\RolesController#settings')->name('manager.settings');
});
Into something like this:
$role = session()->get('role');
Route::group(['prefix' => $role, 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
Route::get('dashboard', 'App\Http\Controllers\RolesController#index')->name($role.'.dashboard');
Route::get('profile', 'App\Http\Controllers\RolesController#profile')->name($role.'.profile');
Route::get('editRegs', 'App\Http\Controllers\RolesController#editRegs')->name('admin.edit');
Route::get('settings', 'App\Http\Controllers\RolesController#settings')->name($role.'.settings');
}
What's the correct way to do this?

Can you try like this. It's worked on me.
Open app/Http/Providers/AppServiceProvider
use Illuminate\Support\Facades\Session;
public function boot()
{
$role = Session::get('role');
app()->bind('role', function() use ($role){
return [
'role' => $role
];
});
}
In routes/web.php
define('role', app()->make('role')['role']);
Route::group(['prefix' => $role, 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
Route::get('dashboard', 'App\Http\Controllers\RolesController#index')->name(role.'.dashboard');
Route::get('profile', 'App\Http\Controllers\RolesController#profile')->name(role.'.profile');
Route::get('editRegs', 'App\Http\Controllers\RolesController#editRegs')->name('admin.edit');
Route::get('settings', 'App\Http\Controllers\RolesController#settings')->name(role.'.settings'); });
In blade;
{{ route(app()->make('role')['role'].'.dashboard') }}
You can try to send "app()->make('role')['role']" as a variable to your blade for more understanding.

Try this;
define('role', session()->get('role'));
While using;
Route::group(['prefix' => $role, 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
Route::get('dashboard', 'App\Http\Controllers\RolesController#index')->name(role.'.dashboard');
Route::get('profile', 'App\Http\Controllers\RolesController#profile')->name(role.'.profile');
Route::get('editRegs', 'App\Http\Controllers\RolesController#editRegs')->name('admin.edit');
Route::get('settings', 'App\Http\Controllers\RolesController#settings')->name(role.'.settings'); });

Related

How fix redirecting with parameter have error

In my Laravel 5.8 app when there are no data in session I need to redirect to some default control.
I do
return redirect()->route('admin.oauthAdminCallback/' . $form_action);
When in routes/web.php defined :
Route::group(['middleware' => ['auth', 'isVerified', 'CheckUserStatus'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::get('oauthAdminCallback/{form_action}', [ 'uses' => 'Admin\EventsController#oauthAdminCallback']);//->name('oauthAdminCallback');
But I got error :
Route [admin.oauthAdminCallback/calendarActionUpdate] not defined.
If in first line $form_action has value : “calendarActionUpdate”.
Which is correct way ?
MODIFIED :
I tried this way
return redirect()->route('admin.oauthAdminCallback',$form_action);
and this way
return redirect()->route('admin.oauthAdminCallback')->with([
'form_action' => $form_action,
]);
But in both cases I do not have amy error but method was not called!
In my routes/web.php :
Route::group(['middleware' => ['auth', 'isVerified', 'CheckUserStatus'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::get('oauthAdminCallback', [ 'as' => 'oauthAdminCallback', 'uses' =>'Admin\EventsController#oauthAdminCallback']);
// The method below is not called!
public function oauthAdminCallback()
{
session_start();
die("-1 XXZ oauthAdminCallback");
return redirect( is ignored and I can not understand why?
So you're calling the route by its name so
try this
return redirect()->route('admin.oauthAdminCallback',$form_action);
Mention your route as
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::get('oauthAdminCallback', [ 'as' => 'oauthAdminCallback', 'uses' => 'Admin\EventsController#oauthAdminCallback']);
});
And your callback as below
return redirect()->route('admin.oauthAdminCallback', $form_action);
Tried and tested.

Route not exit but still getting access?

I have not route /home or home in routes.php but i am still able to access this route. It is creating problem some times during login. Sometimes when i am trying to login, after successful authenticated i am redirected this route but why because this route is not exist in routes.php. It is also accessible as localhost:8000/home. My routes.php is as follow. Any help will be appreciated. Thanks
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
use Entities\Driver;
Route::get('/', 'HomeController#index');
Route::resource('/contact', 'HomeController#contact');
Route::post('mileage', ['as' => 'mileage', 'uses' => 'TripController#getMilesBetween']);
//Route::post('mileage', 'TripController#getMilesBetween');
Route::get('mileage', 'TripController#getMilesNotFound');
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.trip'], function () {
Route::resource('trip', 'TripController');
}
);
Route::get('load-list', 'LoadController#showloadlist');
Route::get('load/{id}/clone', 'LoadController#cloneLoad');
Route::get('load/{id}/clone', 'LoadController#cloneLoad');
Route::get('load/{id}/attachcon', ['as' => 'load.attachcon', 'middleware' => ['auth'], 'can' => 'view.load', 'uses' => 'LoadController#attachRateCon']);
Route::get('load/{id}/attachbol', ['as' => 'load.attachbol', 'middleware' => ['auth'], 'can' => 'view.load', 'uses' => 'LoadController#attachBol']);
Route::get('load/{id}/attachments', ['as' => 'load.attachment', 'middleware' => ['auth'], 'can' => 'view.load', 'uses' => 'LoadController#attachmentList']);
Route::get('expense/{id}/attachreceipt', ['as' => 'expense.attachreceipt', 'middleware' => ['auth'], 'can' => 'view.expense', 'uses' => 'ExpenseController#attachReceipt']);
Route::get('expense/{id}/attachments', ['as' => 'expense.attachment', 'middleware' => ['auth'], 'can' => 'view.expense', 'uses' => 'ExpenseController#attachmentList']);
Route::get('maintenance/{id}/attachreceipt', ['as' => 'maintenance.attachreceipt', 'middleware' => ['auth'], 'can' => 'view.maintenance', 'uses' => 'MaintenanceController#attachReceipt']);
Route::get('maintenance/{id}/attachments', ['as' => 'maintenance.attachment', 'middleware' => ['auth'], 'can' => 'view.maintenance', 'uses' => 'MaintenanceController#attachmentList']);
Route::post('load/store-clone', ['as' => 'load.clone', 'uses' => 'LoadController#cloneStore']);
/*Invoice Routes Start*/
Route::group(['prefix'=>'invoices/'],function(){
Route::get('to_invoice','InvoiceController#LoadToInvoice');
Route::get('create/{customer_id}','InvoiceController#create');
Route::post('/store','InvoiceController#store')->name('invoice.store');
Route::get('/show','InvoiceController#show')->name('invoice.show');
Route::get('/add_payment/{id}','InvoiceController#payments');
Route::post('/store_payment','InvoiceController#store_payment')->name('invoice.payment_store');
Route::get('/pdf/{id}/{type}','InvoiceController#genratePdf')->name('pdf.invoice');
});
/*Invoice Routes end*/
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.load'], function () {
Route::resource('load', 'LoadController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.driver'], function () {
Route::resource('driver', 'DriverController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.contract'], function () {
Route::resource('contract', 'ContractController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.settlement'], function () {
Route::resource('settlement', 'SettlementController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.reexpenses'], function () {
Route::resource('reexpenses', 'RecurringExpensesController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.mplan'], function () {
Route::resource('mplan', 'MaintenancePlanController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.maintenance'], function () {
Route::resource('maintenance', 'MaintenanceLogController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.invoice'], function () {
Route::resource('invoice', 'InvoiceController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.expense'], function () {
Route::resource('expense', 'ExpenseController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.maintenance'], function () {
Route::resource('maintenance', 'MaintenanceController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.entry'], function () {
Route::resource('entry', 'EntryController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.contact'], function () {
Route::resource('contact', 'ContactController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.type'], function () {
Route::resource('type', 'TypeController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.model'], function () {
Route::resource('model', 'ModelController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.company'], function () {
Route::resource('company', 'CompanyController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.vehicle'], function () {
Route::resource('vehicle', 'VehicleController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.attachment'], function () {
Route::resource('attachment', 'AttachmentController');
}
);
Route::group(
[
'middleware' => ['auth',
'acl'],
'can' => 'view.user'], function () {
Route::resource('user', 'UserController');
}
);
Route::get('filesafe', 'AttachmentController#filesafe');
Route::get('plan-edit', 'AttachmentController#editPlan');
Route::get('attachment/download/{id}', 'AttachmentController#download');
Route::get('attachment/destroy/{id}', 'AttachmentController#destroy');
Route::get('expense/{id}/clone', 'ExpenseController#cloneExpense');
Route::post('expense/store-clone', ['as' => 'expense.clone', 'uses' => 'ExpenseController#cloneStore']);
Route::get('expense_category', 'ExpenseController#expenseCatList');
Route::get('expense_category/{id}', 'ExpenseController#expenseCatView');
Route::get('expense_category/destroy/{id}', 'ExpenseController#expenseCatDestroy');
Route::get('expense_category/{id}/edit', 'ExpenseController#expenseCatEdit');
Route::post('expense_category/update', ['as' => 'expense.catupdate', 'uses' => 'ExpenseController#expenseCatUpdate']);
Route::post('contact/mstore', 'ContactController#mstore');
Route::post('contact/mget', 'ContactController#jsonGetContact');
Route::post('type/mstore', 'TypeController#mstore');
Route::get('vehicle/{id}/{dateini}/{dateend}', 'VehicleController#show');
Route::get('trip/destroy/{id}', 'TripController#destroy');
Route::get('load/destroy/{id}', 'LoadController#destroy');
Route::get('part/destroy/{id}', 'PartController#destroy');
Route::get('entry/destroy/{id}', 'EntryController#destroy');
Route::get('contact/destroy/{id}', 'ContactController#destroy');
Route::get('type/destroy/{id}', 'TypeController#destroy');
Route::get('model/destroy/{id}', 'ModelController#destroy');
Route::get('vehicle/destroy/{id}', 'VehicleController#destroy');
Route::get('user/destroy/{id}', 'UserController#destroy');
Route::get('contract/destroy/{id}', 'ContractController#destroy');
Route::get('getModels/{entityKey}/{idType?}', 'ModelController#getModelsByType');
Route::get('getPartsByVehicle/{idVehicle}', 'PartController#getPartsByVehicle');
Route::get('sensor/download/{idPart}', 'TireController#downloadData');
Route::get('profile', 'UserController#showProfile');
Route::put('updateProfile/{id}', 'UserController#updateProfile');
Route::get('invite', 'InviteController#showInvite');
Route::put('invite', 'InviteController#storeInvite');
Route::get('create-account/{token}', 'Auth\AuthController#showCreateAccount');
Route::put('create-account/{token}', 'Auth\AuthController#createAccount');
Route::get('auth/social/{provider}/{token?}', 'SocialLoginController#redirectToProvider');
Route::get('auth/{provider}/callback', 'SocialLoginController#handleProviderCallback');
Route::get('auth/{provider}/callback', 'SocialLoginController#handleProviderCallback');
Route::get('auth/logout', 'SocialLoginController#getLogout');
Route::post('tires/position/swap', 'TireController#positionSwap');
Route::post('tires/position/remove', 'TireController#positionRemove');
Route::post('tires/position/add', 'TireController#positionAdd');
Route::post('tires/details', 'TireController#details');
Route::get('tires/updateStorage/{vehicle_id}', 'TireController#updateStorage');
Route::post('parts/create', 'PartController#store');
Route::post('models/create', 'ModelController#storeByDialog');
Route::post('types/create', 'TypeController#storeByDialog');
Route::post('vehicle/map/updateDetail', 'VehicleController#updateMapDetail');
Route::post('vehicle/dashboard/tires', 'VehicleDashboardController#tires');
Route::post('vehicle/dashboard/localization', 'VehicleDashboardController#localization');
Route::get('vehicle/fleet/dashboard', 'VehicleDashboardController#fleet');
Route::get('vehicle/fleet/dashboard/{updateDatetime}', 'VehicleDashboardController#fleetGpsAndSensorData');
Route::get('vehicle/fleet/dashboard/{updateDatetime}/{vehicleId}', 'VehicleDashboardController#fleetGpsAndSensorData');
Route::bind(
'users', function ($value, $route) {
return App\Entities\User::whereId($value)->first();
}
);
Route::controllers(
[
'auth' => 'Auth\AuthController',
]
);
/* Start Route to Delete*/
Route::get('delete',function(){
$model=Request::get('model');
$condition=Request::get('condition');
$qry=$model::find($condition)->delete();
$ret=1;
$ret2=0;
if($qry){
return $ret;
}
return $ret2;
});
/*End Route to delete*/
//Generate pdf
Route::get('/createAJAX',function(){
$model = Request::get('model');
$data=Input::all();
//unset($data['model']);
$create=$model::create($data)->id;
return $create;
});
If you are using Laravel's authentication, then it will redirect the user to /home after successful login.
When a user is successfully authenticated, they will be redirected to
the /home URI. You can customize the post-authentication redirect
location by defining a redirectTo property on the LoginController,
RegisterController, and ResetPasswordController:
protected $redirectTo = '/';
If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo property:
protected function redirectTo()
{
return '/path';
}
See docs.
You can change the route by updating $redirectTo prop or redirectTo() method in the above mentioned controllers.

Access multiple function of single controller using same router in Laravel

I have a controller That have multiple functions with the same router so I am getting error exception.
Please Guide me for This error
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function()
{
Route::get('/dashboard','DashboardController#chart');
Route::get('/dashboard','DashboardController#index');
});
You can't, the solution is :
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function()
{
Route::get('/chart','DashboardController#chart');
Route::get('/dashboard','DashboardController#index');
});
Or you can call multiple functions on the same url, one with "get" method, and an other with "post" for example :
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function()
{
Route::post('/dashboard','DashboardController#chart');
Route::get('/dashboard','DashboardController#index');
});
But the Route::post() is accessible only after a form submission with method post.

Is it possible use controller with different namespace in route group ?

Is it possible use controller with different namespace in route group ?
Route::group(['prefix' => 'dashboard', 'namespace' => 'admin'], function () {
Route::get('/', ['namespace'=>'Controllers','uses'=>'SiteController#dashobard']);
Route::get('posts', 'PostsController#index');
});
As #TimLewis has mentioned in the comments it is possible.
(Assuming the full namespace for SiteController is App\Http\Controllers) The following should work:
Route::group(['prefix' => 'dashboard', 'namespace' => 'admin'], function () {
Route::get('/', '\App\Http\Controllers\SiteController#dashboard');
Route::get('posts', 'PostsController#index');
});
However, it would make more sense to split the routes up:
Route::group(['prefix' => 'dashboard'], function () {
Route::get('/', 'SiteController#dashboard');
Route::group(['namespace' => 'admin'], function () {
Route::get('posts', 'PostsController#index');
});
});
Hope this helps!

Auth session killed after redirect | laravel 5.2

I made a simple login form. I log my user with :
Auth::loginUsingId($user->id, true);
But when I redirect the user to the ClientController I'm redirect to the login form, the Auth session is not persitent.
return redirect()->action('ClientController#index');
My routes :
Route::group(['middleware' => 'web'], function() {
Route::get('/', 'HomeController#index');
Route::post('/', 'HomeController#auth');
});
Route::group(['prefix' => 'admin', 'middleware' => 'web'], function() {
Route::get('/', 'AdminController#index');
});
Route::group(['prefix' => 'client', 'middleware' => ['auth', 'web']], function() {
Route::get('/', 'ClientController#index');
});
The web middleware needs to kick in before the auth middleware because the web middleware is responsible for booting up your session. Switch the order around like this:
Route::group(['prefix' => 'client', 'middleware' => ['web', 'auth']], function() {
Route::get('/', 'ClientController#index');
});
However, while we're on this subject, you can nest route groups within another route group so to prevent an error like this in the future, I would just recommend nesting everything inside the web middleware like this:
Route::group(['middleware' => 'web'], function() {
Route::get('/', 'HomeController#index');
Route::post('/', 'HomeController#auth');
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'AdminController#index');
});
Route::group(['prefix' => 'client', 'middleware' => 'auth'], function() {
Route::get('/', 'ClientController#index');
});
});

Resources