問題描述
我正在考慮從主頁制作一個登陸頁面,將訪客引導至注冊頁面.我想制作兩個用于發送數據的表單和其中的兩個提交按鈕,比如說讀寫器,根據他們用來進入注冊表單頁面的按鈕,我想傳遞 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.
這篇關于Laravel 將數據從一個視圖傳遞到另一個視圖的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!