如何通过数据重定向到Laravel中的视图 [英] How to pass data through a redirect to a view in laravel
问题描述
如果我有获取路由的方法,如何在控制器执行某些操作后通过redirect()
将数据从视图传递给视图?
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?
应用程序的逻辑是使用user_id
重定向到视图,在此视图中,用户将在成功验证其电子邮件后选择其用户名.
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.
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'));
}
获取路线:
Route::get('assign-username', 'AuthenticationController@showAssignUsernameForm');
以及assign-user
表单的发帖请求代码.
And the code for the post request of the assign-user
form.
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]);
}
}
当尝试访问$user_id
变量时,它表示未定义.
When trying to access to the $user_id
variable it says it is not defined.
视图的代码:
@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版本:5.2
Laravel Version: 5.2
推荐答案
...
更新
在隐藏的输入上存储$user_id
有点冒险,如果您的用户知道如何更改浏览器(例如Chrome开发者控制台)上的值并将其替换为另一个用户ID,怎么办?
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?
与其将其存储在隐藏的输入中,不如将其存储为会话闪存数据,可能是:
Rather than storing it on hidden input I would store it as session flash data, it could be:
public function confirm($confirmationCode){
....
session()->flash('user_id', $user_id); // Store it as flash data.
return redirect('assign-username');
}
在AuthenticationController@showAssignUsernameForm
上,告诉Laravel将您的user_id
保留给下一个请求:
On AuthenticationController@showAssignUsernameForm
tell Laravel to keep your user_id
for next request:
public function showAssignUsernameForm() {
session()->keep(['user_id']);
// or
// session()->reflash();
return view('your-view-template');
}
在分配用户名POST
的方法上,您可以定义如下值:
And on your assign username POST
method you can define the value like this:
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]);
}
}
这篇关于如何通过数据重定向到Laravel中的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!