Laravel 将数据从一个视图传递到另一个视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31756318/
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
Laravel Passing Data From One View to Another View
提问by senty
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
.
我正在考虑从主页制作一个登陆页面,它将引导客人到注册页面。我想制作两个用于发送数据的表单和其中的两个提交按钮,假设读者和作者,根据他们用来转到注册表单页面profession
的按钮,我想从登陆页面中的按钮传递字符串和然后,将其放入注册表中/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() !!}
It is not directing me to the page app.com/auth/register
. But it works when I directly type the link.
它没有将我定向到该页面app.com/auth/register
。但是当我直接输入链接时它会起作用。
What I thought was using $profession
in /auth/register/
and access the value and use it as a hidden field in the registeration form.
我认为使用$profession
in/auth/register/
并访问该值并将其用作注册表中的隐藏字段。
(using laravel 5.1)
(使用 Laravel 5.1)
Edit:
编辑:
In view source:
在查看源:
<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>
Edit 2:
编辑2:
{!! Form::open(array('url' => '/auth/register', 'profession' => 'writer')) !!}
{!! link_to('/auth/register', 'Writer', array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
I tried this instead. At least, now it is directing the page but I still can't access the data value of profession
我尝试了这个。至少,现在它正在引导页面但我仍然无法访问的数据值profession
Edit 3:
编辑3:
Routes:
路线:
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
Route::get('/', function()
{
return view('pages.home');
});
and https://app.com/auth/register
is working.
并且https://app.com/auth/register
正在工作。
回答by tigerbluejay
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.
这是有关如何实现它的分步演练。我测试了它。所以它有效。这是针对“作家”的,但您可以按照原先计划用于其他职业的方式复制它。
I assume you've registered the Laravel Collective package since you're using the curly braces and exclamation points.
我假设您已经注册了 Laravel Collective 包,因为您使用的是花括号和感叹号。
Step 1:
第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() !!}
Not that in the open field we are using a named route ('writer_path').
并不是在开放领域中我们使用的是命名路线('writer_path')。
Step 2:
第2步:
Register a route and a controller on your routes.php file, like this:
在你的 routes.php 文件中注册一个路由和一个控制器,像这样:
Route::post('auth/register', [
'as' => 'writer_path',
'uses' => 'SampleController@displayForm'
]);
Step 3:
第 3 步:
In your sample controller, you define the displayForm method. Within that method you first obtain the value you passed from the landing page view.
在您的示例控制器中,您定义了 displayForm 方法。在该方法中,您首先获取从登录页面视图传递的值。
If you don't know how to create a controler, you can do
如果你不知道如何创建一个控制器,你可以做
php artisan make:controller SampleController
from the command line
从命令行
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).
因为该值是作为数组到达的,所以您必须从数组中获取字符串“writer”,然后将其传递给新视图(带有 writer 注册表的视图)。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
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]);
}
}
Last Step:
最后一步:
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:
在您将创建为 writerregistration.blade.php 的新视图中,您将显示包含您刚刚传递的字段 ('profession') 的表单,该字段包含字符串 'writer'。像这样:
{!! 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, 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.
Presto,您已经在作者的注册表中填充了该字段,其中包含属于登录页面中作者按钮的隐藏字段的信息。