Laravel 发布表单数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29945414/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:28:21  来源:igfitidea点击:

Laravel Post Form data

phplaravel

提问by Ryan hipkiss

I am having trouble taking data from a form in my laravel app, and displaying it using another method in the same controller.

我无法从 laravel 应用程序的表单中获取数据,并在同一控制器中使用另一种方法显示它。

This is my FormController

这是我的 FormController

class ForumController extends Controller {
+
+   private $name,
+           $description;
+
+   public function __construct() {
+       $this->middleware('auth');
+   }
+
+   /**
+    * Show the form for creating a new resource.
+    *
+    * @return Response
+    */
+   public function create()
+   {
+       return view('forum.create');        
+
+       $this->name = Input::post('name');  
+   }
+
+   /**
+    * Show the details of their input once its created
+    */
+   public function created()
+   {   
+       var_dump($this->name);
+   }
+}

This is my Routes.php

这是我的 Routes.php

Route::get('/group/create', 'ForumController@create');
+Route::post('/group/create', 'ForumController@created');

This is my forum create view.

这是我的论坛创建视图。

@extends('app')
+
+@section('content')
+   
+   <div class="container">
+       <h1>Create a Forum</h1>
+       <hr>
+       
+       <div class="panel panel-default">
+           <div class="panel-heading">Create your own forum here!</div>
+           
+           <div class="panel-body">
+               @if (count($errors) > 0)
+                   <div class="alert alert-danger">
+                       <strong>Whoops!</strong>There were some problems with your input.<br><br>
+       <ul>
+           @foreach ($errors->all() as $error)
+               <li>{{ $error }}</li>
+           @endforeach
+       </ul>
+               @endif
+               <form class="form-horizontal" method="POST" action="{{ url('/group/create') }}">
+
+                   <input type="hidden" name="_token" value="{{ csrf_token() }}">  
+                   
+                   <div class="form-group">
+                       <label class="col-md-4 control-label">Name</label>
+                       <div class="col-md-6">
+                           <input type="text" class="form-control" name="name" min="1" max="20">
+                       </div>
+                   </div>
+
+                   <div class="form-group">
+                       <label class="col-md-4 control-label">Description</label>
+                       <div class="col-md-6">
+                           <input type="text" class="form-control" name="description" min="1" max="255">
+                       </div>
+                   </div>
+                   
+                   <div class="form-group">
+                       <div class="col-md-6 col-md-offset-4">
+                           <button type="submit" class="btn btn-primary">
+                               Create
+                           </button>
+                       </div>
+                   </div>
+               </form>
+           </div>
+       </div>  
+   </div>  
+@stop

Could somebody point me in the right direction please?

有人能指出我正确的方向吗?

回答by Matthew Brown

Well, the point of failure on your example is that PHP is not going to persist $this->namewhen you load the createdroute. In fact, as the commenter stated, $this->nameis never set because you have a return statement prior to its definition.

好吧,您示例中的失败点在于,$this->name当您加载created路由时,PHP 不会持续存在。事实上,正如评论者所说,$this->name永远不会设置,因为您在定义之前有一个 return 语句。

After you set $this->namein the create method. When you navigate to the created route, $this->nameis no longer set, even if you were to move your return statement after the setting of name.

$this->name创建方法中设置后。当您导航到创建的路由时,$this->name不再设置,即使您要在 name 设置之后移动 return 语句。

You will need to pesist the data in a database/session/cache or redirect from create method to created method passing the data.

您需要将数据保存在数据库/会话/缓存中,或者从 create 方法重定向到传递数据的 created 方法。

public function create() 
{
    // Handle POST Here
    if (Input::has('name')) {
       $this->name = Input::get('name'); 

        // $this->name is redundant but even still...
        return Redirect::to('created', array('name' => $this->name));
    }

    return view('forum.create');        
}

回答by marko

$name = Input::get('name');

or check:

或检查:

echo var_dump($_POST); 

You should have this code in the controller method you're posting to. And you can save the values in session or flashdata if you want something to save one request and then throw it away. And you need to redirect the view to something also a view or a route.

您应该在要发布到的控制器方法中包含此代码。如果您想要保存一个请求然后将其丢弃,您可以将这些值保存在 session 或 flashdata 中。并且您需要将视图重定向到视图或路线。

http://laravel.com/docs/4.2/session#flash-data

http://laravel.com/docs/4.2/session#flash-data