拉拉维尔(Laravel)提交表单数据

4

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

这是我的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);
+   }
+}

这是我的Routes.php文件。
Route::get('/group/create', 'ForumController@create');
+Route::post('/group/create', 'ForumController@created');

这是我的论坛创建页面。
@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

请问有没有人能够给我指点一下方向呢?

2个回答

4
在您的示例中,失败的关键是当您加载“created”路由时,PHP不会保留“$this->name”。实际上,正如评论者所述,“$this->name”从未设置,因为您在其定义之前有一个返回语句。
在create方法中设置“$this->name”后,当您导航到已创建的路由时,“$this->name”将不再设置,即使您将返回语句移动到名称设置之后也是如此。
您需要将数据持久化存储在数据库/会话/缓存中,或者从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');        
}

1
$this->name 没有被设置。因为在控制器方法中已经返回,所以它是无用代码。 - Matt Burrow

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

或者检查:
echo var_dump($_POST); 

你应该将以下代码放到你要发布的控制器方法中。如果想要保存一次请求的值,则可以将它们保存在会话或闪存数据中,然后丢弃它们。同时,你需要将视图重定向到另一个视图或路由。
参考链接:http://laravel.com/docs/4.2/session#flash-data

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接