问题
I tried following code: if the user's info to login is not correct redirect the user to the loin page again with the a message:
if (!$info_is_correct){
$_SESSION['err_msg'] = 'your login info is not correct';
return redirect('http://localhost:8000/user/login');
}
this is what I do in the login page(view) to echo the error msg:
@isset($_SESSION['err_msg'])
<div class="alert alert-danger mt-2" style="font-family:IRANSans;font-size:16px ;">
<p>
<strong>خطا !</strong>
{{$_SESSION['err_msg']}}
<?php unset($_SESSION['err_msg']) ?>
</p>
</div>
@endisset
where is the problem? and What is the best way to send data to view with redirecting?
回答1:
Use the with() method:
return redirect('http://localhost:8000/user/login')->with('err_msg', 'your login info is not correct');
And if a view:
@if (session('err_msg'))
<div>{{ session('err_msg') }}</div>
@endif
回答2:
You can do like this to send your error messages with use inputs
return redirect('http://localhost:8000/user/login')->with('err_msg', 'Your error message')->withInput();
Source : Laravel Redirects, Laravel Validate
Note : You can also work with Error Messages like this
回答3:
Since you are using laravel the validations can be done by making use of validators and simply return
redirect()->back()->withErrors($validator)
and from the view you can have access to an $errors
variable which will hold all the errors,
and you can loop through errors variable and print out the errors
from your login view
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
回答4:
Set session value in laravel with session
method, be sure to use because it is global and set session value for whole site.
Also remove http://localhost:8000
to work this route in both development and production.
if (!$info_is_correct){
session('err_msg', 'your login info is not correct');
return redirect('/user/login');
}
And get value form session as session('key')
{{session('err_msg')}}
来源:https://stackoverflow.com/questions/48879238/redirecting-with-sending-datas-in-laravel