I can list all my route in console, but when I add this on my route.php, I've got fatal error
Route::group(['middleware' => 'is_admin', 'prefix' => 'admin', 'as'=>'admin.'], function () {
Route::get('/', ['as'=>'dashboard', 'uses'=>'AdminController#dashboard']);
Route::resource('questions','QuestionController');
});
and this is on my Middleware/IsAdmin.php
namespace App\Http\Middleware;
use Closure;
class IsAdmin
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if(auth()->user()->isAdmin()) {
return $next($request);
}
return redirect('home');
}
}
Update 2
Even I only got this, it still error.
Route::group(['middleware' => 'is_admin', 'prefix' => 'admin', 'as'=>'admin.'], function () {
Route::get('/', ['as'=>'dashboard', 'uses'=>'AdminController#dashboard']);
});
Update 3 - AdminController
...
class AdminController extends Controller
{
private $page_name;
public function __construct()
{
$this->middleware('auth');
//when I comment this below, it works.
$this->page_name = ucfirst(substr(\Request::route()->getName(), strpos(\Request::route()->getName(), "/") + 1));
}
...
The problem here is that you have:
$this->page_name = ucfirst(substr(\Request::route()->getName(), strpos(\Request::route()->getName(), "/") + 1));
in controller constructor (you already noticed).
When you are using console, you don't have any route so it won't work.
If you really need this line you can wrap this with additional condition:
if (!app()->runningInConsole())
{
$this->page_name = ucfirst(substr(\Request::route()->getName(), strpos(\Request::route()->getName(), "/") + 1));
}
but you can also extract this line into separate method and use it in other methods when needed.
Related
i have 4 types of user which is 'admin', 'staff', 'lecturer', 'student'. i already made that when user login, it will redirect to the dashboard and i want to make the other 3 users to the booking page. supposedly with a simple if-else statement it can be done i believe. but tried doing it, it puts an error.
here are my AdminMiddleware.php:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AdminMiddleware
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle(Request $request, Closure $next)
{
if(Auth::user()->usertype == 'admin')
{
return $next($request);
}
else
{
return redirect('/booking')->with('status','you are not allowed to enter uitm dashboard');
}
}
}
and in my logincontroller.php, i do it like this:
protected function redirectTo()
{
if(Auth::user()->usertype == 'admin')
{
return 'dashboard';
}
else
{
return 'booking';
}
}
web.php:
Route::group(['middleware' => ['auth', 'admin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
});
The ->name() at the end of the routes in case your redirect needs a name.
And the first line if you don't have it already. It will be the route for staff, lecturers, and students (admin can access too).
Route::get('/booking', /* code */)->name('booking');
Route::group(['middleware' => ['auth', 'admin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
})->name('dashboard');
});
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 a method for checking if a user's role is an admin, if not, redirect them with return redirect('/')->send();. How can I check for user role and redirect the user without displaying the page and waiting for a redirect?
My Controller:
class AdminController extends Controller
{
public function __construct()
{
if (Auth::check())
{
$user = Auth::user();
if ($user->role != 'admin')
{
return redirect('/')->send();
}
}
else
{
return redirect('/')->send();
}
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return View('admin/index');
}
}
Create your own Middleware. Here is an example. In my example, I have several usergroups in a separate model. You have to change the code for your needs.
Create the Middleware via terminal/console:
php artisan make:middleware UserGroupMiddleware
The created middleware class could be find in app/Http/Middleware/UserGroupMiddleware.php
You need the following code in your middleware:
namespace App\Http\Middleware;
use Closure;
use App\User;
use App\Usergroup;
class UserGroupMiddleware
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next, $group)
{
if($request->user() !== NULL){
$userGroupId = $request->user()->group;
$userGroup = Usergroup::find($userGroupId);
if($userGroup->slug === $group){
return $next($request);
}
}
// Redirect the user to the loginpage
return redirect('/login');
}
}
Now you have to register this middleware in app/Http/Kernel.php:
protected $routeMiddleware = [
// other middlewares
// Custom Middleware
'group' => \App\Http\Middleware\UserGroupMiddleware::class
];
Finally you need to attach the middleware to your route:
Route::group(['middleware' => 'group:admin'], function(){
// Routes for admins, e.g.
Route::get('/dashboard', 'SomeController#dashboard');
});
// Or for a single route:
Route::get('/dashboard', ['middleware' => 'group:admin'], function(){
return view('adminbereich.dashboard');
});
Remember, that you could pass in multiple middlewares with:
Route::get('/some/route', ['middleware' => ['group:admin', 'auth']], 'SomeController#methodXYZ');
import redirect by adding this to the above the class
use Illuminate\Support\Facades\Redirect;
And the make your redirect by using
return Redirect::to('login');
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?