2013-11-01 101 views
0

我真正的新Laravel,所以我有這樣的疑問:Laravel 4設置控制器

如果我在數據庫中有2個表,說:立場和載體,我希望能夠 編輯/添加/更新/刪除它們,我需要什麼樣的模型/控制器/視圖結構?

我是否爲位置和矢量創建控制器? 我應該創建一個設置控制器,並創建位置和矢量模型嗎?

回答

0

這完全取決於你,但是我會爲每一個模型,一個所有邏輯的存儲庫,可能被稱爲SettingsRepository.php,然後在需要使用該代碼的任何位置使用該存儲庫。您還必須修改您的composer.json文件,並確保您要放置存儲庫的文件夾位於autoload部分。

至於你的控制器,你可能只需要一個從倉庫抓取數據並將其插入到視圖中的數據。當我想到設置時,我想到了應用程序的其他區域以及存儲庫中所需的設置,所有這些信息都可以被其他控制器輕鬆訪問。

0

模型處理與數據庫的直接交互。創建一個「位置」模型和一個「矢量」模型。當你這樣做,你會延長雄辯模型,你很快就會有一個知道如何添加,刪除,更新,保存等 http://laravel.com/docs/eloquent

創建您希望用戶看到每個頁面一個視圖模型。 (您最終會創建主模板視圖,但現在不用擔心這一點)。 http://laravel.com/docs/responses#views

控制器處理視圖和模型之間的數據。在Laravel文檔中有一部分內容,但我沒有足夠的代表點來發布超過2個鏈接。

以最有意義的方式對控制器功能進行分組。如果您的所有網站路線都以「/ settings」開頭,那麼您可能只需要一個「設置」控制器就會出現紅旗。

這是一個非常簡單的例子,你可能想要做的事情。有很多不同的方式來完成你想要的東西。這是一個例子。

// View that you see when you go to www.yoursite.com/position/create 
// [stored in the "views" folder, named create-position.php] 
<html><body> 
<form method="post" action="/position/create"> 
    <input type="text" name="x_coord"> 
    <input type="text" name="y_coord"> 
    <input type="submit"> 
</form> 
</body></html> 


// Routing [in your "routes.php" file] 
// register your controller as www.yoursite.com/position 
// any public method will be avaliable at www.yoursite.com/methodname 
Route::controller('position', 'PositionController'); 


// Your Position Controller 
// [stored in the "controllers" folder, in "PositionController.php" file 
// the "get" "post" etc at the beginning of a method name is the HTTP verb used to access that method. 
class PositionController extends BaseController { 

    public function getCreate() 
    { 
     return View::make('create-position'); 
    } 

    public function postCreate() 
    { 
     $newPosition = Position::create(array(
      'x_coord' => Input::get('x_coord'), 
      'y_coord' => Input::get('y_coord') 
     )); 

     return Redirect::to('the route that shows the newly created position'); 
    } 

} 

// Your Position Model 
class Position extends Eloquent { 

    // the base class "Eloquent" does a lot of the heavy lifting here. 

    // just tell it which columns you want to be able to mass-assign 
    protected $fillable = array('x_coord', 'y_coord'); 

    // yes, nothing else required, it already knows how to handle data 

}