Make this controller in laravel:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CreateUsersRequest;
use App\Http\Requests\UpdateUsersRequest;
use App\Models\Users;
use App\Models\Roles;
use App\Repositories\UsersRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;
use Illuminate\Foundation\Auth\RegistersUsers;
use Intervention\Image\Facades\Image;
use App\Classes\LockSession;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
class UsersController extends AppBaseController
{
/** @var UsersRepository */
private $usersRepository;
public function __construct(UsersRepository $usersRepo)
{
$this->middleware('lock');
$this->middleware('noGoBack');
$this->usersRepository = $usersRepo;
}
/**
* Display a listing of the Users.
*
* @param Request $request
* @return Response
*/
public function index()
{
$libros = Users::all();
return view('users.index')->with('users', $libros);
}
/**
* Show the form for creating a new Users.
*
* @return Response
*/
public function create()
{
$roles = Roles::all();
return view('users.crear_usuario')->with('roles', $roles);
//return view('users.create');
}
/**
* Store a newly created Users in storage.
*
* @param CreateUsersRequest $request
*
* @return Response
*/
public function store(Request $request)
{
$input = $request->all();
$input['password'] = bcrypt($request->input('password'));
if($request->hasFile($request->input('avatar'))){
$avatar = $request->file('avatar');
$filename = $request->input('name') . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->save(public_path('/uploads/avatars/'. $filename));
$input['avatar'] = $filename;
} else{
$input['avatar'] = 'default.png';
}
$users = $this->usersRepository->create($input);
$users->assign($request->input('rol'));
Flash::success('El usuario se registro exitosamente');
return redirect(route('users.index'));
}
/**
* Display the specified Users.
*
* @param int $id
*
* @return Response
*/
public function show($id)
{
$users = $this->usersRepository->findWithoutFail($id);
if (empty($users)) {
Flash::error('Users not found');
return redirect(route('users.index'));
}
return view('users.show')->with('users', $users);
}
/**
* Show the form for editing the specified Users.
*
* @param int $id
*
* @return Response
*/
public function edit($id)
{
$users = $this->usersRepository->findWithoutFail($id);
if (empty($users)) {
Flash::error('Users not found');
return redirect(route('users.index'));
}
return view('users.edit')->with('users', $users);
}
/**
* Update the specified Users in storage.
*
* @param int $id
* @param UpdateUsersRequest $request
*
* @return Response
*/
public function update($id, UpdateUsersRequest $request)
{
$users = $this->usersRepository->findWithoutFail($id);
if (empty($users)) {
Flash::error('Users not found');
return redirect(route('users.index'));
}
$users = $this->usersRepository->update($request->all(), $id);
Flash::success('Users updated successfully.');
return redirect(route('users.index'));
}
/**
* Remove the specified Users from storage.
*
* @param int $id
*
* @return Response
*/
public function destroy($id)
{
$users = $this->usersRepository->findWithoutFail($id);
if (empty($users)) {
Flash::error('Usuario no encontrado');
return redirect(route('users.index'));
}
$this->usersRepository->delete($id);
Flash::success('Users deleted successfully.');
return redirect(route('users.index'));
}
//Bloquea la session
public function lock(){
$token = Auth::user()->lockToken;
$locked = Cache::remember('session_lock', 1000, function(){
return [];
});
if(array_key_exists($token, $locked) === false){
$locked[$token] = back()->getTargetUrl();
}
Cache::forever('session_lock', $locked);
return redirect()->route('locked');
}
//Muestra la página de bloqueo
public function locked(Request $request){
//Solo puede acceder a esta página se se blokea primero
if(!LockSession::isLocked()){
return back();
}
return view('users.bloqueo');
}
//Verifica si puede desbloquear la sesion con los datos pasados por el request
public function locked_check(Request $request){
$email = Auth::user()->name;
if(LockSession::isLocked() && Auth::attempt([
'name' => $email,
'password' => $request->get('password')
]))
{
$to = LockSession::unlock();
return redirect($to);
} elseif(LockSession::isLocked()){
return redirect()->route('locked')->with('errors', collect(['password' => 'Sus credenciales son incorrectas']));
}
return redirect()->route('locked');
}
}
These are my routes associated with the problem:
Route::post('/bloquea', 'UsersController@lock')->name('auth_lock');
Route::get('/bloqueado', 'UsersController@locked')->name('locked');
Route::post('/chaquear_bloquedo', 'UsersController@locked_check')->name('locked_check');
But the function when it reaches the point of redirection: return redirect()->route('locked');
the browser throws me this error:
Esta página no funciona
La página localhost te ha redirigido demasiadas veces.
Borrar las cookies.
ERR_TOO_MANY_REDIRECTS
Has this problem happened to you? how could they solve it?
From what I can see your redirect problem is that it goes into an infinite loop, since you define the lock middleware for all your actions, so the same login_check action is called continuously and executed on the last line. The 2 possible solutions are to mainly exclude this action from the middleware or remove the middleware line in your controller constructor and include it in your routes file. Here is the first case:
I hope it works !!!