2013-05-12 63 views
1

我使用Jeffrey Ways Generator通过脚手架命令生成一些代码。 我是OOP,Frameworks和Laravel的新手。我有一个工作方法,但只是想知道这是否是正确的做法。Laravel 4通过表格搜索模型

所以基本上我想通过输入框搜索我的模型。

在我的index.blade.php模型中,我在页面顶部有这段代码。

{{ Form::open(array('url' => 'tweets/', 'method' => 'get')) }} 
    {{ Form::text('id') }} 
{{ Form::close() }} 

现在在我的微博控制器我有这个

public function index() 
    { 
     if(Input::get('id')) 
     { 
      return Redirect::action('[email protected]', array(Input::get('id'))); 
     } 

     else 
     { 

     $tweets = $this->tweet->all(); 
     // print_r($tweets); 
     return View::make('tweets.index', compact('tweets')); 
     } 
    } 

一切工作,我希望它的工作,但是这是做事的正确方法吗?

回答

1

而不是从index()方法重定向到show()如果输入::获得(“ID”)设置,应直接通过更改URL提交表单到show方法。

{{ Form::open(array('url' => 'tweets/show/', 'method' => 'get')) }} 
    {{ Form::text('id') }} 
{{ Form::close() }} 

确保设立鸣叫/显示路由/太app/routes.php

Route::get('/tweets/show', '[email protected]'); 

可能是一个更好的解决方案:

如果表单只存在显示特定推文的链接(通过ID),最好设置路线和show()方法为:

只是通过使普通链路(ID为在URL)

public function show($id) 
{ 
    // Load the tweet using $id as ID instead of Input::get('id') 
} 

从那时起,你可以链接到它:

Route::get('/tweets/show/{id}', '[email protected]'); 

,然后在TweetsController文件改变show()功能

<a href="{{ URL::to('tweets/show/'.$tweet->id) }}">View tweet</a>