Change route group prefix when changed locale - php

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

Related

Laravel - add in core/routes/ if username exist

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.

laravel 8 multiple user redirect to different pages with auth command

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');
});

Laravel Route List Fatal Throwable Error

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.

Is my route group intended for routes that require admin role written correctly?

I'm trying to remove the admin middleware from specific routes and create a Route::group for admin routes only but I'm not sure if I'm doing it correctly.
I'm trying to turn this:
Route::get('/admin', [
'uses' => 'PagesController#admin',
'as' => 'admin',
'middleware' => 'roles',
'roles' => 'Admin'
]);
Into this:
Route::group(['middleware' => ['roles'], 'roles' => ['admin']], function(){
Route::get('/admin', 'pagesController#admin')->name('admin');
});
So this route group is meant to only pass people who have the role of admin to the routes. I believe it is working but I really wanna check if I have made the change correctly.
Middleware:
class CheckRole
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if ($request->user() === null){
return redirect()->route('home');
}
$actions = $request->route()->getAction();
$roles = isset($actions['roles']) ? $actions['roles'] : null;
if ($request->user()->hasAnyRole($roles) || !$roles ) {
return $next($request);
}
return redirect()->route('home');
}
}

How can I add role in the route?

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

Categories