2013-01-10 57 views
0

我今天從字面上下載了Laravel,並且喜歡事物的外觀,但是我在2件事情上非常努力。Laravel - 使用控制器而不是路由來執行操作

1)我喜歡控制器的行爲分析的網址,而不是使用路線的方法,它似乎更清晰地把一切都在一起,但可以說我想去

/account/account-year/ 

我該怎麼寫行動功能呢?即

function action_account-year()... 

顯然是無效的語法。

2)如果我有

function action_account_year($year, $month) { ... 

,並參觀了

/account/account_year/ 

將顯示一個錯誤失蹤爭論,你怎麼去製作此用戶友好/負載DIFF頁/顯示錯誤??

回答

8

您必須手動路由連字版本,例如,

Route::get('account/account-year', '[email protected]_year'); 

關於參數,它取決於你如何路由。您必須接受路線中的參數。如果您使用完全控制器路由(例如Route::controller('account')),則該方法將自動傳遞參數。

如果手動路由,你必須捕捉參數,可以

Route::get('account/account-year/(:num)/(:num)', '[email protected]_year'); 

所以參觀/account/account-year/1/2會做->account_year(1, 2)

希望這有助於。

+0

我想接受這個答案,但這是我認爲...嗯,我確定我讀了控制器路由可以做一切標準路由可以,似乎沒有。非常感謝你 –

+0

只需數字2出現在沙發上,帶有蘇格蘭威士忌,簡單的PHP語法! ($ name = false,$ place = false){if(...){return View :: make('page.error'); }' –

+0

也查看了laravel/routing/controller.php,方法「response」添加了'$ method = preg_replace(「#\ - +#」,「_」,$ method);'在頂部, 。 Antone知道如何將這個類擴展爲一個bundle /插件? –

0

我想我會添加爲萬一別人的答案是尋找它:

1)

public function action_account_year($name = false, $place = false) { 
    if(...) { 
      return View::make('page.error'); 
    } 
} 

2)

不是固溶體尚未:

laravel/routing/controller.php,方法「響應」

public function response($method, $parameters = array()) 
{ 
    // The developer may mark the controller as being "RESTful" which 
    // indicates that the controller actions are prefixed with the 
    // HTTP verb they respond to rather than the word "action". 

    $method = preg_replace("#\-+#", "_", $method);    

    if ($this->restful) 
    { 
     $action = strtolower(Request::method()).'_'.$method; 
    } 
    else 
    { 
     $action = "action_{$method}"; 
    } 

    $response = call_user_func_array(array($this, $action), $parameters); 

    // If the controller has specified a layout view the response 
    // returned by the controller method will be bound to that 
    // view and the layout will be considered the response. 
    if (is_null($response) and ! is_null($this->layout)) 
    { 
     $response = $this->layout; 
    } 

    return $response; 
} 
4

你可以把下面的可能性以及

class AccountController extends BaseController { 

    public function getIndex() 
    { 
     // 
    } 

    public function getAccountYear() 
    { 
     // 
    } 

} 

現在簡單地定義一個RESTful控制器的路線以下方式

Route::controller('account', 'AccountController'); 

訪問文件'account/account-year'會自動路由到行動getAccountYear

+0

+1不知道爲什麼這不是最佳答案。 –

相關問題