laravel too many redirects errors - php

Here is my routes file:
Route::get('/', 'WebController#index');
Route::group(['prefix' => 'cn'], function () {
Route::get('/', 'WebController#index');
Route::group(['namespace' => 'ACL', 'prefix' => 'auth'], function () {
Route::get('login', 'AuthController#login');
Route::post('login', 'AuthController#login');
Route::get('logout', 'AuthController#logout');
});
Route::group(['middleware' => ['auth', 'token']], function () {
Route::get("/intentional", 'IntentionalOrderController#index');
Route::get("/intentional/add", 'IntentionalOrderController#create');
Route::post("/intentional/add", 'IntentionalOrderController#store');
Route::get("/intentional/selfOrder", 'IntentionalOrderController#selfOrder');
Route::post("/intentional/selfOrder", 'IntentionalOrderController#storeSelfOrder');
});
});
Route::group(['prefix' => 'en'], function () {
Route::get('/', 'WebController#index');
Route::group(['namespace' => 'ACL', 'prefix' => 'auth'], function () {
Route::get('login', 'AuthController#login');
Route::post('login', 'AuthController#login');
Route::get('logout', 'AuthController#logout');
});
Route::group(['middleware' => ['auth', 'token']], function () {
Route::get("/intentional", 'IntentionalOrderController#index');
Route::get("/intentional/add", 'IntentionalOrderController#create');
Route::post("/intentional/add", 'IntentionalOrderController#store');
Route::get("/intentional/selfOrder", 'IntentionalOrderController#selfOrder');
Route::post("/intentional/selfOrder", 'IntentionalOrderController#storeSelfOrder');
});
});
The problem is:
When I enter the /en or /cn url, the browser will say too many redirects; but when I enter /en/auth/login or any other url, it will work just fine.
Althought I can fix this by renaming /cn to /cn/index, but still I don't understand why this is not working.
PS:
1. My laravel version is 5.1
2. Debug environment is wamp
3. Browser is chrome
Update:
here is my WebController and BaseController
<?php namespace App\Http\Controllers;
class WebController extends BaseController
{
public function index()
{
return view(config('app.locale') . ".web.index", ["title" => trans('common.smart_fabric'), "active" => "0"]);
}
}
<?php namespace App\Http\Controllers;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Request;
class BaseController extends Controller
{
public function __construct()
{
$locale = Request::segment(1);
if ($locale) {
switch ($locale) {
case 'cn':
App::setLocale('cn');
break;
case 'en':
App::setLocale('en');
break;
default:
App::setLocale('cn');
}
}
}

Related

Laravel Routing "Route [dashboard] not defined?

I'm trying to config an user/admin environment in my laravel page, and whenever I try group the routes, I'll get one of the mentioned error back. What am I doing wrong? I tried both formats, same error.
web.php
//supposed user dashboard
Route::group(['middleware' => ['auth', 'user']], function () {
Route::get('/dashboard', 'DashboardController#index')->name('dashboard');
});
//supposed admin dashboard
Route::group(['middleware' => ['auth', 'admin']], function () {
Route::get('/dashboard', [AdminDashController::class, 'index']);
});
eg AdminDashController:
public function index()
{
return view("admin_dashboard");
}
DashboardController does the same, but returning user view.
I'm new to laravel, I appriciate any help!
Update:
I tried the solution below, my result is that I'm now getting "Route [user.dashboard] not defined." error...
My web.php
Route::group(['middleware' => ['auth', 'user']], function () {
Route::get('/dashboard', [UserDashController::class, 'index'])->name('user.dashboard');
});
// admin dashboard
Route::group(['middleware' => ['auth', 'admin']], function () {
Route::get('/dashboard', [AdminDashController::class, 'index'])->name('admin.dashboard');
});
my AdminDashController and UserDashController:
public function index()
{
return view('user_dashboard');
}
AND
public function index()
{
return view('admin_dashboard');
}
I have a RedirectIfAuthenticated.php
public function handle(Request $request, Closure $next, ...$guards)
{
$guards = empty($guards) ? [null] : $guards;
foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
/** #var User $user */
$user = Auth::guard($guard);
// to admin dashboard
if ($user->hasRole('admin')) {
return redirect()->route('admin.dashboard');
}
// to user dashboard
else if ($user->hasRole('user')) {
return redirect(route('user.dashboard'));
}
}
}
return $next($request);
}
Also having an Admin and User redirect:
AdminAuthenticated.php:
public function handle(Request $request, Closure $next)
{
if( Auth::check() )
{
/** #var User $user */
$user = Auth::user();
// if user is not admin take him to his dashboard
if ( $user->hasRole('user') ) {
return redirect()->route('user.dashboard');
}
// allow admin to proceed with request
else if ( $user->hasRole('admin') ) {
return $next($request);
}
}
abort(403); // permission denied error
}
UserAuthenticated
public function handle(Request $request, Closure $next)
{
if( Auth::check() )
{
/** #var User $user */
$user = Auth::user();
// if user is admin take him to his dashboard
if ( $user->hasRole('admin') ) {
return redirect(route('admin.dashboard'));
}
// allow user to proceed with request
else if ( $user->hasRole('user') ) {
return $next($request);
}
}
abort(403); // permission denied error
}
Update 2:
I replaced the routing in web.php as follows:
Route::middleware(['auth','user'])->group(function () {
Route::prefix('user')->group(function () {
Route::get('/dashboard', [UserDashController::class, 'index'])->name('user.dashboard');
});
});
Route::middleware(['auth','admin'])->group(function () {
Route::prefix('admin')->group(function () {
Route::get('/dashboard', [AdminDashController::class, 'index'])->name('admin.dashboard');
});
});
Still same error: "Route [user.dashboard] not defined."
The problem may be in your route name. One route has a named dashboard another was not. Use the below code hope this will resolve your problem
// user dashboard
Route::group(['middleware' => ['auth', 'user']], function () {
Route::get('/dashboard', 'DashboardController#index')->name('user.dashboard');
});
// admin dashboard
Route::group(['middleware' => ['auth', 'admin']], function () {
Route::get('/dashboard', 'DashboardController#index')->name('admin.dashboard);
})
use the route name in stead of url.
in my case, instead using your code below :
Route::group(['middleware' => ['auth', 'user']], function () {
Route::get('/dashboard', [UserDashController::class, 'index'])->name('user.dashboard');
});
// admin dashboard
Route::group(['middleware' => ['auth', 'admin']], function () {
Route::get('/dashboard', [AdminDashController::class, 'index'])->name('user.dashboard');
});
i use my own code, so define the middleware first and use prefix on it :
Route::middleware(['auth','user'])->group(function () {
Route::prefix('user')->group(function () {
Route::get('/dashboard', [UserDashController::class, 'index'])->name('user.dashboard');
});
});
Route::middleware(['auth','admin'])->group(function () {
Route::prefix('admin')->group(function () {
Route::get('/dashboard', [AdminDashController::class, 'index'])->name('admin.dashboard');
});
});
maybe you should differentiate routing between dashboard for admin and user. you can use like this : /admin/dashboard and /user/dashboard
edit :
i think there's some typo on your code :
// to admin dashboard
if ($user->hasRole('admin')) {
return redirect(route('admin.dashboard'));
}
return redirect route should typed like this : return redirect()->route('admin.dashboard)

LaravelQuestion - Class admin does not exist

i found a issue it say 'Class admin does not exist'. Anything I missing for the issue? Thanks.
Here is my Route.php
Route::group(['prefix' => 'admin', 'middleware'=> ['auth' => 'admin']], function () {
Route::get('/','AdminController#index');
Route::get('profile','AdminController#profile');
Route::get('/addProduct','AdminController#addProduct');
});
Here is my AdminController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function index(){
return view('admin.index');
}
public function profile(){
return view('admin.profile');
}
public function addProduct(){
return view('admin.addProduct');
}
}
Issue with in the route file to assign middlewares to route group
If you have two middlewares then assign like this ["auth", "admin"] instead of ["auth" => "admin"].
Route::group(['prefix' => 'admin', 'middleware'=> ['auth', 'admin']], function () {
Route::get('/','AdminController#index');
Route::get('profile','AdminController#profile');
Route::get('/addProduct','AdminController#addProduct');
});

How to prevent a parameter from getting injected in controller methods in Laravel

I am creating a multilingual website in Laravel. I need urls to be like that:
https://website.com/en/admin/user/1
https://website.com/ar/admin/user/1
so my route file looks like that:
Route::group(['prefix' => '{locale}'], function(){
Route::middleware("locale")->group(function () {
Route::group(['prefix' => 'admin', 'as' => "admin.", "namespace" => "Admin"],function(){
Route::get('login', 'LoginController#index')->name("signin")->middleware("Guest");
Route::post('login', 'LoginController#login')->name('login')->middleware("Guest");
Route::group(["middleware" => "Admin"], function (){
Route::get('/', ["as" => "dashboard", "uses" =>'DashboardController#index']);
Route::get('logout', 'LoginController#logout')->name('logout');
Route::resource('users', 'UsersController');
});
});
});
});
My middleware "locale":
public function handle($request, Closure $next)
{
URL::defaults(['locale' => $request->segment(1)]);
if(in_array($request->segment(1), config('app.locales')))
{
app()->setLocale($request->segment(1));
}
return $next($request);
}
Now the problem is that when I try to call a use a route with parameters I have to add the locale as a first parameter. For example:
this is a method in UsersController:
public function destroy($locale, $id)
{
//Any Code
}
I have to add $locale as a parameter to the method to be able to use the other parameters or the $id will have the value of the $locale which is "en", "ar" instead of the user id. So is there any way I can avoid that so my method will look like:
public function destroy($id)
{
//Any Code
}
You can build your routes for example like this:
Route::group(['prefix' => app()->getLocale()], function(){
so you will get rid of locale route parameter.

How to use role based access in laravel using entrust

I am using Laravel 5.4. I want to assign all authority to 'Admin' role. But Employee cannot delete or edit records. I'm using Entrust Package for Role based permission.
I've written routes for this but unfortunately Its not working for employee.
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function () {
// Route::resource('plotsize', 'plotSizeController');
Route::group(['middleware' => 'role:Employee'], function () {
Route::resource('plotsize', 'plotSizeController', ['except' => 'edit', 'update', 'delete']);
});
Route::group(['middleware' => 'role:Admin'], function () {
Route::resource('plotsize', 'plotSizeController');
}); });
This is another approach that I had tried but unfortunately Its also not working.
protected $user;
public function __construct()
{
$this->middleware(function ($request, $next) {
if(Auth::user()->hasRole('Admin')){
$this->middleware('role:Admin');
return $next($request);
}
if(Auth::user()->hasRole('Employee')){
$this->middleware('role:Employee', ['only' => ['edit', 'update', 'destroy']]);
return $next($request);
}
});
}
If you're giving me suggestion to use Policy in Laravel. I've tried that but I'm not getting what's the issue on this. Can you provide me any example for this? Thanks

Login Redirect to wrong location

When I login instead of being redirected to dashboard I get redirected to / in my auth controller I have commented out the following line however I still get redirected to the wrong location why?
I have cleared the cache php artisan cache:clear with no luck
Line Commented Out:
protected $redirectTo = '/';
Login Function:
public function postLogin(Request $request)
{
$credentials = $this->getLoginCredentials($request);
if(Auth::attempt($credentials))
{
redirect()->intended('dashboard');
}
return redirect()->back();
}
Routes:
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'web'], function() {
/* Admin Auth */
Route::get('login', 'Auth\AuthController#getLogin');
Route::post('login', 'Auth\AuthController#postLogin');
Route::get('register', 'Auth\AuthController#getRegister');
Route::post('register', 'Auth\AuthController#postRegister');
Route::get('logout', 'Auth\AuthController#getLogout');
Route::group(['middleware' => 'auth.admin'], function(){
/*Admin Dashboard Routes */
Route::get('dashboard', 'AdminController#getDashboard');
});
});
You forgot return for redirect():
public function postLogin(Request $request)
{
$credentials = $this->getLoginCredentials($request);
if(Auth::attempt($credentials))
{
return redirect()->intended('dashboard'); // here
}
return redirect()->back();
}

Categories