2013-11-01 2 views
0

내가 Laravel 정말 새로운, 그래서 나는이 질문이 : 나는 데이터베이스에서 2 개 테이블이있는 경우Laravel 4 설정 컨트롤러

는 말 : 위치와 벡터를 내가 편집 을 할 수 있도록하려면 추가// 업데이트/삭제, 어떤 종류의 모델/컨트롤러/뷰 구조가 필요합니까?

위치 및 벡터에 대한 컨트롤러를 만드는 중입니까? 설정 컨트롤러를 만들고 위치 및 벡터에 대한 모델을 만들어야합니까?

답변

0

이것은 모두 당신에게 달려 있지만, 각 로직에 대한 리포지토리 (아마도 SettingsRepository.php이라고 불리는 모델)를 가지고있을 것입니다. 그런 다음 그 코드를 사용해야하는 곳에는 저장소를 사용하십시오. 또한 composer.json 파일을 수정하고 리포지토리를 넣을 폴더가 autoload 섹션에 있는지 확인해야합니다.

컨트롤러의 경우 리포지토리에서 데이터를 가져 와서보기에 삽입하는 컨트롤러 만 필요할 것입니다. 설정을 생각할 때, 앱의 다른 영역과 저장소에서 필요한 정보를 다른 컨트롤러가 쉽게 액세스 할 수 있다고 생각합니다.

0

모델은 데이터베이스와의 직접적인 상호 작용을 처리합니다. "위치"모델과 "벡터"모델을 만듭니다. 그렇게하면 Eloquent 모델을 확장하고 모델을 추가, 삭제, 업데이트, 저장하는 등의 작업을 빠르게 수행 할 수 있습니다. 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 

}