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.
Related
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'); });
When I change the file-manager.php to 'routePrefix' => 'file' or 'routePrefix' => 'admin.file' the fm-button page still sends the following request.
Request URL: http://127.0.0.1:8000/file-manager/initialize
I changed the routes.php file as follows and only the routes was modified but the requests within the routes were not modified.
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function () {
Route::group([ 'prefix' => 'file-manager', 'namespace' => 'Alexusmai\LaravelFileManager\Controllers', ], function () {...
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!
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');
});
});
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.