Here is my current route: (which works as well)
Route::get('/register', ['uses' => 'registerController#form','as'=>'register','middleware' => 'roles', 'roles' => ['admin'] ]);
Now I want to know, how can I use role when I write the middleware like ->middleware('role') ?
Note: This doesn't work:
Route::get('/register', 'registerController#form')->name('register')->middleware('role')->role(['admin']);
Route::get('/register', 'registerController#form')->name('register')->middleware('role:admin');
i guess you wanted this
for multiple
Route::group(['middleware' => ['role:Normal_User,Admin']], function() {
Route::get('/register', 'registerController#form');
});
Middleware parameters may be specified when defining the route by separating the middleware name and parameters with a :. Multiple parameters should be delimited by commas.
You can make your own middleware:
<?php
namespace App\Http\Middleware;
use Closure;
class CheckRole
{
/**
* Handle the incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #param string $role
* #return mixed
*/
public function handle($request, Closure $next, $role)
{
if (! $request->user()->hasRole($role)) {
// Redirect...
}
return $next($request);
}
}
And call it like this:
Route::get('/register', 'registerController#form')->name('register')->middleware('role:editor');
Source: https://laravel.com/docs/5.4/middleware#middleware-parameters
Related
Route::middleware('admin')->group(function () {
Route::get('dashboard', 'AdminController#dashboard')->name('dashboard');
Route::get('profile', 'AdminController#profile')->name('profile');
Route::post('profile', 'AdminController#profileUpdate')->name('profile.update');
Route::get('password', 'AdminController#password')->name('password');
Route::post('password', 'AdminController#passwordUpdate')->name('password.update');
I have this code in routes and I want to add "if username == "staff"
Route::get('dashboard', 'AdminController#dashboard')->name('dashboard');
not others pages.
First create StaffMiddleware.php in following path app/Http/Middleware/StaffMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;
class StaffMiddleware
{
/**
* Handle an incoming request. User must be logged in to do admin check
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (\Auth::user()->username == 'staff')
{
return $next($request);
}
return redirect()->guest('/');
}
}
In app/Http/Kernel.php add the following line
protected $routeMiddleware = [
// your existing code
'staff' => \App\Http\Middleware\StaffMiddleware::class,
];
Rewrite the following route
Route::get('dashboard', 'AdminController#dashboard')->name('dashboard');
TO
Route::get('dashboard', 'AdminController#dashboard')->name('dashboard')->middleware('staff');
Now whenever you want to check this condition you just need to add ->middleware('staff') in route.
I want to change route group prefixes when changed locale.
For example, if the locale is en:
Route::group(['prefix' => 'giveaway'], function () {
});
if the locale is tr:
Route::group(['prefix' => 'cekilis'], function () {
});
How should i make this.
I tried
'prefix'=>__('routes.prefix')
But app can't access current locale in routes.
I recommend you to use middleware to set the locale as dynamic
create a middleware just like below:
namespace App\Http\Middleware;
use Closure;
class Language
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
\App::setLocale($request->locale);
return $next($request);
}
}
And registe this middleware in app\Http\Kernel:
protected $middlewareGroups = [
'web' => [
// ...
\App\Http\Middleware\Language::class,
// ...
]
];
finally you can call your middleware on you route file
Route::middleware('language')->group(function ($locale) {
//You have a condition as you wish
if ($locale == 'en') {
Route::group(['prefix' => 'giveaway'], function () {
.......
});
} elseif ($locale == 'tr') {
Route::group(['prefix' => 'cekilis'], function () {
........
});
}
});
I hope this will solve your problem
I have three roles in my application. I have a condition in which two roles can access same page. For that I write below code.
in below code, sub plan1 and sub plan 2 are roles.
Route::group(['middleware' => ['web', 'auth', 'SubPlan1', 'SubPlan2']], function () {
Route::get('/Parent-1-Info', '\ContactInfoController#Parent1Info'));
});
if sub plan1, tries to access the page, I get 404 error because i mentioned both middleware in same group.
Is there anyway to code such that I can define or condition in middleware?
For role based authentication I'm using this middleware:
namespace App\Http\Middleware;
use Auth;
use Closure;
use App\Role;
use Illuminate\Support\Collection;
class RoleMiddleware
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next, $roles = null, $guard = null)
{
$roles = Role::whereIn('slug', explode('|', $roles))->get();
if (! Auth::guard($guard)->user()->hasRole($roles)) {
return abort(403, 'Forbidden');
}
return $next($request);
}
}
Then register the middleware in Kernel.php
'role' => \App\Http\Middleware\RoleMiddleware::class,
On the user model make sure you have a method to check if user has a set of roles, example:
public function hasRole($role)
{
if (is_int($role)) {
return $this->roles->contains('id', $role);
}
if (is_string($role)) {
return $this->roles->contains('slug', $role);
}
if ($role instanceof Model) {
return $this->roles->contains('id', $role->id);
}
return !! $role->intersect($this->roles)->count();
}
And you can use the middleware like this:
Route::group(['middleware' => ['auth', 'role:admin|staff'], ...);
You can replace admin|staff with your role names, separated by |. If you want to add custom guard then you can pass it as second parameter role:admin|staff,mycustomguard
I keep getting an error every time I use myroute middleware. Below is the middleware class. its currently empty.
<?php
namespace App\Http\Middleware;
use Closure;
class EnforceHttps
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
return $next($request);
}
}
When I attach it to my route like so it gives me an error.
Route::get('/', ['middleware' =>'https',function () {
//Route::get('/', function () {
return view('launch');
}]);
If I uncomment the line below which does not include the middleware instead all is good. Why is this?
Just starting to get into laravel and running into major issues migrating my vanilla php to use it.
I created a middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Session;
class QwickAuthCheck
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if ($request->session()->has('qwick')) {
//
return redirect('home');
}
return $next($request);
}
}
I'm checking to see if a session is set.
home is a route I have in routes.php
Route::get('/', 'WebController#home')->name('home');
I register it in kernel.php like so;
protected $routeMiddleware = [
........
........
'qwickAuth' => \App\Http\Middleware\QwickAuthCheck::class,
];
Now I want to apply the middleware to
Route::get('login', 'WebController#login');
How can I do this?
I have tried;
Route::get('login', ['middleware' => ['qwickAuth'], 'WebController#login');
Laravel has a lot of documentation on their site but for some reason all their code is not giving snippets of how people use this framework. In the middleware documentation all they gave was this;
Route::get('admin/profile', ['middleware' => 'auth', function () {
//
}]);
How do I know how to use it seeing that I don't have a function in my route
While Marcins result works, you can also do it this way
Route::get('login', 'WebController#login')->middleware(['qwickAuth']);
You should use:
Route::get('login', ['middleware' => ['qwickAuth'], 'uses' =>'WebController#login']);
You have example of uses in routing here: https://laravel.com/docs/5.2/routing#named-routes