I want when I type the address http://localhost/agendab/public
or when I open my web application, it will redirect directly to login
, or when I click on disconnect, it will redirect to login
.
web.php
Auth::routes();
Route::resource('events', 'EventController')->middleware('auth');
loginController.php
protected $redirectTo = '/events';
public function login(Request $request)
{
$input = $request->all();
$this->validate($request, [
'name' => 'required',
'password' => 'required',
]);
$fieldType = filter_var($request->name, FILTER_VALIDATE_EMAIL) ? 'email' : 'name';
if(auth()->attempt(array($fieldType => $input['name'], 'password' => $input['password'])))
{
return redirect()->route('events');
}else{
return redirect()->route('login')
->with('error','Email-Address And Password Are Wrong.');
}
}
Http/Middleware/RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/events');
}
return $next($request);
}
CodePudding user response:
When you use Laravel resources, you need pre-defined way to call. Ex for the index
you have to call events.index
, if update
then events.update
...
In case you're calling a custom route in the same
EventsController
the defied it before the resource route.
In RedirectIfAuthenticated.php
if (Auth::guard($guard)->check()) {
return redirect('/events.index');
}
or
return Redirect::route('events.index'); #using facade
return redirect()->route('events.index'); #using helper class
FYK
CodePudding user response:
Redirect proper route name
return redirect()->route('events.index'); // loginController.php