Laravel将数据从一个视图传递到另一个视图 [英] Laravel Passing Data From One View to Another View

查看:111
本文介绍了Laravel将数据从一个视图传递到另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑从主页制作一个登录页面,它将引导访客进入注册页面。我想制作两种用于发送数据和两个提交按钮的表单,让我们说阅读器和书写器,并根据他们用来进入注册表页面的按钮,我想通过专业 code>字符串,然后将它放到 / 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 。但是它直接输入链接。



我认为在<$ c $中使用 $ profession c> / auth / register / 并访问该值并将其用作注册表单中的隐藏字段。



(使用laravel 5.1)




编辑

查看源代码:

 < form method =POSTaction =http://app.com/auth/register accept-charset =UTF-8profession =writer>< input name =_ tokentype =hiddenvalue =dZXQsNI1BGQ39JjDLFUEkSQzL5bTNwe8o3rpiSQL> 

< input class =btn btn-warningtype =submitvalue =Writer>

< / form>


< form method =POSTaction =http://app.com/auth/registeraccept-charset =UTF-8profession =reader> ;< input name =_ tokentype =hiddenvalue =dZXQsNI1BGQ39JjDLFUEkSQzL5bTNwe8o3rpiSQL>

< input class =btn btn-defaulttype =submitvalue =Reader>

< / form>






编辑2:

  {!! Form :: open(array('url'=>'/ auth / register','profession'=>'writer'))!!} 

{!! link_to('/ auth / register','Writer',array('class'=>'btn btn-default'))!!}

{!! Form :: close()!!}

我试过这个。至少,现在它正在引导页面,但我仍然无法访问专业




$的数据值b
$ b

编辑3:

路线:

  Route :: get('auth / register','Auth\AuthController @ getRegister'); 
Route :: post('auth / register','Auth\AuthController @ postRegister');

Route :: get('/',function()
{
return view('pages.home');
});

https://app.com/auth/register 正在工作。

解决方案

以下是一步一步的介绍如何实现它。我测试了它。所以它工作。这是'作家',但你可以复制它,因为你原来计划为其他职业。



我假设你已经注册了Laravel Collective包,因为你是使用大括号和感叹号。



第1步:



在您的着陆页视图中,作家按钮,添加一个字符串'作家'的隐藏字段。像这样:

  {!! Form :: open(['route'=> ['writer_path']])!!} 
{!! Form :: hidden('profession','writer')!!}
{!! Form :: submit('Writer',array('class'=>'btn btn-warning'))!!}
{!! Form :: close()!!}

不是在开放字段中我们使用了一条命名路线('writer_path')。



第2步:在你的routes.php中注册一个路由和一个控制器文件,如下所示:

 路线:: post('auth / register',[
'as'=> gt ;'writer_path',
'uses'=>'SampleController @ displayForm'
]);

第三步:

控制器,您可以定义displayForm方法。
在该方法中,您首先获得您从着陆页视图传递的值。



如果您不知道如何创建控制器,您可以执行

  php artisan make:controller SampleController 

来自命令行

因为该值以数组形式出现,所以必须从数组中获取字符串'writer',然后将它传递给新视图(带有作者注册表单的视图)。

 <?php 

命名空间App \ Http \Controllers;
使用Illuminate \Http\Request;
使用App \Http\Requests;
使用App \Http\Controllers\Controller;
使用Illuminate\Support\Facades\Input;

类SampleController扩展了Controller
{
/ **
*显示资源列表。
*
* @return Response
* /
public function displayForm()
{
$ input = Input :: get();
$ profession = $ input ['profession'];
return view('writerregistration',['profession'=> $ profession]);
}

}

最后一步:



在您将创建为writerregistration.blade.php的新视图中,您将显示包含字符串'writer'的刚刚传递的字段('profession')的表单。像这样:

  {!! 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,您填写了注册表单中的字段作者在着陆页中隐藏字段上的信息,属于作者按钮。


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() !!}

It is not directing me to the page app.com/auth/register. But it works when I directly type the link.

What I thought was using $profession in /auth/register/ and access the value and use it as a hidden field in the registeration form.

(using 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:

    {!! 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


Edit 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.

解决方案

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.

Step 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').

Step 2:

Register a route and a controller on your routes.php file, like this:

Route::post('auth/register', [
'as' => 'writer_path',
'uses' => 'SampleController@displayForm'
]);

Step 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.

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).

<?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:

{!! 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.

这篇关于Laravel将数据从一个视图传递到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆