Laravel 将数据从一个视图传递到另一个视图
我正在考虑从主页制作一个登陆页面,将访客引导至注册页面.我想制作两个用于发送数据的表单和其中的两个提交按钮,比如说读写器,根据他们用来进入注册表单页面的按钮,我想传递 profession
字符串从登录页面中的按钮,然后将其放入 /auth/register
中的注册表中.
I am thinking of making a landing page from the home page, which will direct the guest to the register page. I thought of making two forms for sending data and two submit buttons in them, let's say reader and writer and according to the button they use to go to the register form page, I want to pass the profession
string from the button in the landing page and then, place it into the register form in /auth/register
.
{!! Form::open(array('url' => '/auth/register', 'profession' => 'writer')) !!}
{!! Form::submit('Writer', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!}
{!! Form::open(array('url' => '/auth/register', 'profession' => 'reader')) !!}
{!! Form::submit('Reader', array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
它没有将我引导至页面 app.com/auth/register
.但是当我直接输入链接时它会起作用.
It is not directing me to the page app.com/auth/register
. But it works when I directly type the link.
我认为在 /auth/register/
中使用 $profession
并访问该值并将其用作注册表中的隐藏字段.
What I thought was using $profession
in /auth/register/
and access the value and use it as a hidden field in the registeration form.
(使用 laravel 5.1)
(using laravel 5.1)
查看源代码:
<form method="POST" action="http://app.com/auth/register" accept-charset="UTF-8" profession="writer"><input name="_token" type="hidden" value="dZXQsNI1BGQ39JjDLFUEkSQzL5bTNwe8o3rpiSQL">
<input class="btn btn-warning" type="submit" value="Writer">
</form>
<form method="POST" action="http://app.com/auth/register" accept-charset="UTF-8" profession="reader"><input name="_token" type="hidden" value="dZXQsNI1BGQ39JjDLFUEkSQzL5bTNwe8o3rpiSQL">
<input class="btn btn-default" type="submit" value="Reader">
</form>
<小时>
编辑 2:
{!! Form::open(array('url' => '/auth/register', 'profession' => 'writer')) !!}
{!! link_to('/auth/register', 'Writer', array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
我尝试了这个.至少,现在它正在引导页面,但我仍然无法访问profession
I tried this instead. At least, now it is directing the page but I still can't access the data value of profession
编辑 3:
路线:
Route::get('auth/register', 'AuthAuthController@getRegister');
Route::post('auth/register', 'AuthAuthController@postRegister');
Route::get('/', function()
{
return view('pages.home');
});
和 https://app.com/auth/register
正在工作.
推荐答案
以下是有关如何实施它的分步演练.我测试了它.所以它有效.这是给作家"的,但您可以按照原先计划用于其他职业的方式复制它.
Here's a step by step walkthrough on how to implement it. I tested it. So it works. This is for 'writer', but you could replicate it as you had originally planned for other professions.
我假设您已经注册了 Laravel Collective 包,因为您使用的是花括号和感叹号.
I assume you've registered the Laravel Collective package since you're using the curly braces and exclamation points.
第 1 步:
在您的着陆页视图中,您有作家按钮的位置,添加一个带有字符串作家"的隐藏字段.像这样:
In your landing page view, where you have the writer button, add a hidden field with the string 'writer'. Like this:
{!! Form::open(['route' => ['writer_path']]) !!}
{!! Form::hidden('profession', 'writer') !!}
{!! Form::submit('Writer', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!}
并不是说在开放字段中我们使用的是命名路线('writer_path').
Not that in the open field we are using a named route ('writer_path').
第 2 步:
在你的 routes.php 文件中注册一个路由和一个控制器,像这样:
Register a route and a controller on your routes.php file, like this:
Route::post('auth/register', [
'as' => 'writer_path',
'uses' => 'SampleController@displayForm'
]);
第 3 步:
在您的示例控制器中,您定义了 displayForm 方法.在该方法中,您首先获取从着陆页视图传递的值.
In your sample controller, you define the displayForm method. Within that method you first obtain the value you passed from the landing page view.
如果你不知道如何创建一个控制器,你可以做
If you don't know how to create a controler, you can do
php artisan make:controller SampleController
从命令行
因为该值是作为数组到达的,所以您必须从数组中获取字符串writer",然后将其传递给新视图(带有 writer 注册表的视图).
Because the value arrives as an array, you have to obtain the string 'writer' from the array and then pass it to the new view (the view with the registration form for the writer).
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
use AppHttpControllersController;
use IlluminateSupportFacadesInput;
class SampleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function displayForm()
{
$input = Input::get();
$profession = $input['profession'];
return view('writerregistration', ['profession' => $profession]);
}
}
最后一步:
在您将创建为 writerregistration.blade.php 的新视图中,您将显示包含您刚刚传递的字段 ('profession') 的表单,该字段包含字符串 'writer'.像这样:
In the new view which you will create as writerregistration.blade.php, you will display the form with the field you just passed ('profession') which contains the string 'writer'. Like this:
{!! Form::open() !!}
{!! Form::label('username', 'Username:') !!}
{!! Form::text('username', null, ['class' => 'form-control']) !!}
{!! Form::label('profession', 'Profession:') !!}
{!! Form::text('profession', $profession, ['class' => 'form-control']) !!}
{!! Form::label('email', 'Email:') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
{!! Form::label('passowrd', 'Password:') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
{!! Form::label('password_confirmation', 'Password Confirmation:') !!}
{!! Form::password('password_confirmation', ['class' => 'form-control']) !!}
{!! Form::submit('Sign Up', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
Presto,您已经在作者的注册表中填写了该字段,其中包含属于着陆页中作者按钮的隐藏字段的信息.
Presto, you've populated the field in the registration form for the writer with the info on the hidden field that belonged to the writer button in the landing page.
相关文章