umefarooq의 대답은 그러나 희망이 답변은 일관성있는 베스트 -뿐만 아니라 당신의 Laravel 개발에 머리 시작을 얻기에 당신에게 좀 더 통찰력을 제공해야 정확 프로그래밍 스타일을 연습하십시오.
첫째, 클래스 이름은 대문자로 시작해야합니다. 소문자로 시작하는 메소드/함수 이름과 대문자로 시작하는 클래스 이름을 유지하십시오.
두 번째로 BaseController 앞에 \
이 필요하지 않습니다. 컨트롤러의 이름을 지정하는 경우 백 슬래시 만 있으면됩니다. 예 : 컨트롤러가 Admin\TestController.php
폴더에 있고 파일의 시작 부분에 <?php namespace Admin
을 입력하여 Admin
네임 스페이스에 TestController를 넣으면됩니다. 이것은 \BaseController
을 사용해야 할 때입니다. TestController에게 Global Namespace에서 BaseController를 확장하라고 말하기 때문입니다. 또는 클래스를 선언하기 전에 use BaseController;
을 입력 할 수 있으며 매번 \
을 입력 할 필요가 없습니다.
특히 질문 관련
:
당신이 당신의 루트 파일에 리소스 경로를 사용하여, 당신은 컨트롤러가 다음 방법 중 하나 또는 모두 가질 수 Laravel을 말하고있다 : index
, show
, create
, store
을 edit
update
및 destroy
.
마찬가지로, Route::resource('test', 'TestController');
은 컨트롤러 폴더 내의 TestController.php를 가리 킵니다.
귀하의 TestController는 다음과 같이 구성되어야한다, 가장 편안한 컨트롤러는 보일러의 일종으로 아래의 사용 :
<?php
class TestController extends BaseController
{
public function __construct()
{
}
// Typically used for listing all or filtered subset of items
public function index()
{
$tests = Test::all();
return View::make('test.index', compact('tests'));
}
// Typically shows a specific item detail
public function show($id)
{
$test = Test::find($id);
return View::make('test.show', compact('test'));
}
// Typically used to show the form which creates a new resource.
public function create()
{
return View::make('test.create');
}
// Handles the post request from the create form
public function store()
{
$test = new Test;
$test->attribute1 = Input::get('attribute1');
$test->attribute2 = Input::get('attribute2');
$test->attribute3 = Input::get('attribute3');
$test->attribute4 = Input::get('attribute4');
if ($test->save())
{
return Redirect::route('test.show', $test->id);
}
}
// Shows the edit form
public function edit($id)
{
$test = Test::find($id);
return View::make('test.edit', compact('test'));
}
// Handles storing the submitted PUT request from the edit form.
public function update($id)
{
$test = Test::find($id);
$test->attribute1 = Input::get('attribute1');
$test->attribute2 = Input::get('attribute2');
$test->attribute3 = Input::get('attribute3');
$test->attribute4 = Input::get('attribute4');
if ($test->save())
{
return Redirect::route('test.show', [$id]);
}
}
// Used to delete a resource.
public function destroy($id)
{
$test = Test::find($id);
$test->delete();
return Redirect::route('test.index');
}
}
는 또한, 자원 컨트롤러를 사용의 아름다움은 당신이라는 이름의 경로를 활용할 수 있다는 것입니다.
터미널 창에 php artisan routes
을 입력하십시오.
7 개의 경로가 표시됩니다. 만약 당신이 URL을 변경하거나 필요한 경우
{{ Form::open(array('route' => array('test.store')) }}
그 방법 : 대신 대신 명명 된 경로에 URL을 가리킬 수 있습니다
{{ Form::open(array('url'=>'test/loginform')) }}
을하는 양식 이내에
test.index
test.destroy
test.show
test.edit
test.destroy
test.create
test.update
, 양식 게시 URL이 routes 파일 내의 명명 된 경로에 자동으로 바인드되므로 사이트 구조를 이동하는 것이 쉽습니다. URL이 올바른 위치를 가리키고 있는지 확인하기 위해 모든 뷰를 업데이트해야합니다.
마지막으로 출발점으로 JefreyWay/Laravel-4-Generators 패키지를 사용하는 것이 좋습니다. https://github.com/JeffreyWay/Laravel-4-Generators. 이를 사용하여 리소스, 컨트롤러, 뷰 등을 만들고 발전기가 모델, 뷰 및 컨트롤러를 어떻게 활용할 수 있는지 확인하십시오. 여기
당신이 시작하는 데 도움이되는 또 다른 자원이다 :
https://laracasts.com/lessons/understanding-rest
매우 도움이 :) – Kanav
좋아요, 감사합니다. 그리고 정답을 선택하는 것을 잊지 마십시오. :) – Gravy