2014-01-13 87 views
3

我目前正在試圖路線如下:Laravel 4路由控制方法

  • 如果用戶得到/account/
    • 如果會議有account_id,用戶登錄;顯示他的帳戶信息
    • 如果不是,用戶未登錄;顯示登錄/創建表單
  • 如果用戶發佈/account/
    • 如果輸入有create,用戶想要創建帳戶;創建它
    • 如果不是,用戶想要登錄;發現他的帳戶,然後再次以/account/

我的路線設置是這樣的:

Route::get('account', function() { 
    if (Session::has('account_id')) 
     return '[email protected]'; 
    else 
     return '[email protected]'; 
}); 

Route::post('account', function() { 
    if (Input::has('create')) { 
     return '[email protected]'; 
    else  
     return '[email protected]'; 
)}; 

這有點我將如何使用Rails做,但我不知道怎麼點到控制器方法。我只是得到返回的字符串。我沒有在Laravel的文檔中找到它(我發現它很差,或者我搜索錯了?),在任何其他Web教程中都沒有找到它。

+0

我沒有試過,但是,這一個可以幫助你:HTTP:// laravel .com/api/source-class-Illuminate.Routing.Controllers.Controller.html#93-127 – alayli

回答

9

嘗試以下操作:

Route::get('account', function() { 
    if (Session::has('account_id')) { 
     $action = 'show'; 
     return App::make('AccountsController')->$action(); 
    } 
    else { 
     $action = 'index'; 
     return App::make('AccountsController')->$action(); 
    } 
}); 

Route::post('account', function() { 
    if (Input::has('create')) { 
     $action = 'create'; 
     return App::make('AccountsController')->$action(); 
    } 
    else { 
     $action = 'login'; 
     return App::make('AccountsController')->$action(); 
    } 
)}; 
+0

使用過濾器實現此目的的最佳方法。我將嘗試使用過濾器來實現一個示例。 – Anam

+0

工作過,謝謝! – ranisalt

0

所以,想要把所有的邏輯控制器。

你會想這樣做

Route::get('account', '[email protected]'); 
Route::get('account', '[email protected]'); 

然後,在相應的控制器,你想使你的測試,然後執行,例如,

if (Session::has('account_id')){ 
    return Redirect::action('[email protected]'); 
} 
else{ 
    return Redirect::action('[email protected]'); 
} 

但是,請確保您定義每個動作的路由,所以你的應用程序可以檢測到一個URL。
例如,如果你想有

Redirect::action('[email protected]') 

您需要定義這個動作,如:

Route::get('/account/show', '[email protected]') 
+0

那麼,因爲用戶無法同時落入2類(如果他獲得或發佈,如果他是否登錄等),我是否真的必須區分這些行爲?我不想有更多的路線/頁面,只是'/ accounts /'對於每一種控制器方法都是足夠的 – ranisalt

+0

抱歉,不確定你到底在問什麼。從技術上講,你可以像以前那樣將關閉放在routes.php文件中,但是最簡單的方式是返回重定向動作。 – elliotanderson

相關問題