如何通过重定向将数据传递到 Laravel 中的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37485527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass data through a redirect to a view in laravel
提问by Andrés Buitrago
How can I pass data from a controller after it performs certain action to a view through a redirect()
if I have a get route for it?
redirect()
如果我有一个 get 路由,我如何在控制器执行某些操作后将数据从控制器传递到视图?
The logic of the app is to redirect with an user_id
to a view where the user will select its username after successfully verified its email.
应用程序的逻辑是重定向user_id
到一个视图,用户将在成功验证其电子邮件后选择其用户名。
public function confirm($confirmationCode){
if(!$confirmationCode){
dd('No se encontró ningún código de verificación en la URL');
}
$user = User::where('confirmation_code', $confirmationCode)->first();
if(!$user){
dd('Lo sentimos. Este código de confirmación ya ha sido usado.');
}
$user->confirmed = 1;
$user->confirmation_code = null;
$user_id = $user->user_id;
$user->save();
return redirect('assign-username')->with(compact('user_id'));
}
The get route:
获取路径:
Route::get('assign-username', 'AuthenticationController@showAssignUsernameForm');
And the code for the post request of the assign-user
form.
以及assign-user
表单发布请求的代码。
public function assignUsername(){
$user_id = request()->input('user_id');
$username = request()->input('username');
if(User::where('username', '=', $username)->exists()){
return redirect()->back()->withInput()->withErrors([
'username' => 'Este usuario ya se encuentra registrado. Intenta nuevamente'
]);
}else{
DB::table('user')->where('user_id', $user_id)->update(['username' => $username]);
}
}
When trying to access to the $user_id
variable it says it is not defined.
当试图访问该$user_id
变量时,它说它没有定义。
The view's code:
视图的代码:
@extends('layouts.master')
@section('content')
<section class="hero">
<h1><span>Ya estás casi listo.</span>Es hora de seleccionar tu nombre de usuario</h1>
<div class="form-group">
<form class="form-group" method="post" action="assign-username">
{!! csrf_field() !!}
@if($errors->has('username'))
<span class="help-block" style="color:red">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
<input type="hidden" name="user_id" value="{{ session('user_id') }}">
<input type="text" name="username" placeholder="Escribe tu nombre de usuario">
<button type="submit" class="btn" name="send">Registrar</button>
</form>
</div>
</section>
@endsection
Laravel Version: 5.2
Laravel 版本:5.2
回答by Rifki
...
...
Update
更新
Storing $user_id
on a hidden input is a bit risky, how if your user know how to change the value on browser such as Chrome developer console and replace it with another user id?
存储$user_id
在隐藏输入上有点冒险,如果您的用户知道如何更改浏览器(例如 Chrome 开发者控制台)上的值并将其替换为另一个用户 ID,该怎么办?
Rather than storing it on hidden input I would store it as session flash data, it could be:
与其将其存储在隐藏输入中,不如将其存储为session flash data,它可能是:
public function confirm($confirmationCode){
....
session()->flash('user_id', $user_id); // Store it as flash data.
return redirect('assign-username');
}
On AuthenticationController@showAssignUsernameForm
tell Laravel to keep your user_id
for next request:
在AuthenticationController@showAssignUsernameForm
告诉Laravel,让您user_id
的下一个请求:
public function showAssignUsernameForm() {
session()->keep(['user_id']);
// or
// session()->reflash();
return view('your-view-template');
}
And on your assign username POST
method you can define the value like this:
在您的分配用户名POST
方法中,您可以像这样定义值:
public function assignUsername(){
$user_id = session()->get('user_id');
$username = request()->input('username');
if(User::where('username', '=', $username)->exists()) {
session()->flash('user_id', $user_id); // Store it again.
return redirect()->back()->withInput()->withErrors([
'username' => 'Este usuario ya se encuentra registrado. Intenta nuevamente'
]);
} else {
DB::table('user')->where('user_id', $user_id)->update(['username' => $username]);
}
}
回答by Alexey Mezenin
This should work:
这应该有效:
public function assignUsername(Request $request)
{
$user_id = $request->user_id;
回答by Jose Rojas
If you're passing data to the view, to get the information after with(compact('user_id'))
you must do through Session
like this:
如果您将数据传递给视图,要在with(compact('user_id'))
您必须通过以下方式获取信息后Session
:
@if (session('user_id'))
<input type="hidden" name="user_id" value="{{ session('user_id') }}">
@endif