基本的なコントローラーBasic Controllers
全ルートのロジックをたった一つのroutes.php
ファイルで定義するより、コントローラークラスを使い構造化して利用したいと、考えていることでしょう。コントローラーは関連したルートごとのロジックをクラスにまとめると同時に、自動的な依存の注入など、より高度なフレームワークの機能が使える利点があります。Instead of defining all of your
route-level logic in a single
routes.php
file, you may wish to
organize this behavior using Controller classes.
Controllers can group related route logic into a
class, as well as take advantage of more advanced
framework features such as automatic dependency
injection[/docs/4.2/ioc].
基本的にコントローラーはapp/controllers
ディレクトリーに設置されます。このディレクトリーはデフォルトでcomposer.json
ファイルにclassmap
オプションとして登録されています。しかし、コントローラーは技術的に、どのディレクトリー・サブディレクトリーにも設置可能です。ディスク上のコントローラークラスファイルの位置に、ルートの宣言は依存していません。そのため、Composerがコントローラークラスをオートロードできるのであれば、どこにでもお好きな場所へどうぞ!Controllers are typically stored
in the app/controllers
directory, and
this directory is registered in the
classmap
option of your
composer.json
file by default. However,
controllers can technically live in any directory or
any sub-directory. Route declarations are not
dependent on the location of the controller class
file on disk. So, as long as Composer knows how to
autoload the controller class, it may be placed
anywhere you wish.
コントローラークラスの一例です。Here is an example of a basic controller class:
class UserController extends BaseController {
/**
* 指定されたユーザーのプロファイルを表示する
*/
public function showProfile($id)
{
$user = User::find($id);
return View::make('user.profile', array('user' => $user));
}
}
全てのコントローラーはBaseController
クラスを拡張する必要があります。BaseController
はapp/controllers
ディレクトリーの中にあり、共通のコントローラーロジックを記述するために使用できます。BaseController
はフレームワークのController
クラスを拡張しています。では、このコントローラールートを呼び出してみましょう。All controllers should extend the
BaseController
class. The
BaseController
is also stored in the
app/controllers
directory, and may be
used as a place to put shared controller logic. The
BaseController
extends the framework's
Controller
class. Now, we can route to
this controller action like so:
Route::get('user/{id}', 'UserController@showProfile');
もし、あなたがサブフォルダーでネストし、PHP名前空間で体系付けられたコントローラーを採用しているのなら、簡単に完全なクラス名をルートで使用することができます。If you choose to nest or organize your controller using PHP namespaces, simply use the fully qualified class name when defining the route:
Route::get('foo', 'Namespace\FooController@method');
**注目:**LaravelではPHPクラスのオートローディングにComposerを利用しています。ですからComposerが認識する場所であれば、ファイルシステム上のどこにでもコントローラーを置くことが可能です。コントローラーディレクトリーは、アプリケーションのファイル構造をあなたに強いるものではありません。コントローラーへのルーティングは、ファイルシステムより完全に分離されています。Note: Since we're using Composer[http://getcomposer.org] to auto-load our PHP classes, controllers may live anywhere on the file system, as long as composer knows how to load them. The controller directory does not enforce any folder structure for your application. Routing to controllers is entirely de-coupled from the file system.
コントローラールートに名前を付けることもできます。You may also specify names on controller routes:
Route::get('foo', array('uses' => 'FooController@method',
'as' => 'name'));
コントローラーアクションに対するURLを生成するには、URL::action
メソッドか、action
ヘルパメソッドを使用してください。To generate a URL to a controller
action, you may use the URL::action
method or the action
helper
method:
$url = URL::action('FooController@method');
$url = action('FooController@method');
現在実行中のコントローラーアクションの名前を取得するにはcurrentRouteAction
メソッドを使用します。You may access the name of the
controller action being run using the
currentRouteAction
method:
$action = Route::currentRouteAction();
コントローラーフィルターController Filters
コントローラールートに、「普通」のルートと同じようにフィルターを指定することができます。Filters[/docs/4.2/routing#route-filters] may be specified on controller routes similar to "regular" routes:
Route::get('profile', array('before' => 'auth',
'uses' => 'UserController@showProfile'));
その一方で、コントローラーの内部でフィルターを指定することも可能です。However, you may also specify filters from within your controller:
class UserController extends BaseController {
/**
* 新しいUserControllerインスタンスの生成
*/
public function __construct()
{
$this->beforeFilter('auth', array('except' => 'getLogin'));
$this->beforeFilter('csrf', array('on' => 'post'));
$this->afterFilter('log', array('only' =>
array('fooAction', 'barAction')));
}
}
更に、無名関数を使用しインラインでコントローラーフィルターを指定することもできます。You may also specify controller filters inline using a Closure:
class UserController extends BaseController {
/**
* 新しいUserControllerインスタンスの生成
*/
public function __construct()
{
$this->beforeFilter(function()
{
//
});
}
}
フィルターとしてコントローラのメソッドを使用したい場合は、フィルターの定義に@
記法を使用してください。If you would like to use another
method on the controller as a filter, you may use
@
syntax to define the
filter:
class UserController extends BaseController {
/**
* 新しいUserControllerインスタンスの生成
*/
public function __construct()
{
$this->beforeFilter('@filterRequests');
}
/**
* やってきたリクエストをフィルターにかける
*/
public function filterRequests($route, $request)
{
//
}
}
暗黙的なコントローラーImplicit Controllers
Laravelでは一つのルート定義だけで、コントローラーの全アクションを簡単に処理できます。最初にRoute::controller
メソッドでルートを定義します。Laravel allows you to easily
define a single route to handle every action in a
controller. First, define the route using the
Route::controller
method:
Route::controller('users', 'UserController');
controller
メソッドは2つの引数を取ります。最初はコントローラーが処理するベースのURIで、2つ目はコントローラーのクラス名です。続いて対応するHTTP動詞名をプレフィックスに使用した、コントローラメソッドを付け加えてください。The controller
method accepts two arguments. The first is the base
URI the controller handles, while the second is the
class name of the controller. Next, just add methods
to your controller, prefixed with the HTTP verb they
respond to:
class UserController extends BaseController {
public function getIndex()
{
//
}
public function postProfile()
{
//
}
public function anyLogin()
{
//
}
}
index
メソッドはコントローラーが処理するURIのルートに対応します。この例ではusers
です。The index
methods
will respond to the root URI handled by the
controller, which, in this case, is
users
.
もしコントローラアクションを複数の単語で命名するのでしたら、「ダッシュ」で分離したURIでアクセスできます。例えば下のコントローラーアクション、UserController
は、users/admin-profile
というURIに対応します。If your controller action
contains multiple words, you may access the action
using "dash" syntax in the URI. For
example, the following controller action on our
UserController
would respond to the
users/admin-profile
URI:
public function getAdminProfile() {}
RESTフルリソースコントローラーRESTful Resource Controllers
リソースフルコントローラーはリソースに関するRESTフルコントローラーを簡単にしてくれます。例えば、アプリケーションにより管理されている「写真(phots)」を管理するコントローラーを作成するとしましょう。Artisanコマンドラインでcreate:make
を実行し、Route::resource
メソッドでルートを定義すれば、そのようなコントローラを素早く生成できます。Resource controllers make it
easier to build RESTful controllers around
resources. For example, you may wish to create a
controller that manages "photos" stored by
your application. Using the
controller:make
command via the Artisan
CLI and the Route::resource
method, we
can quickly create such a controller.
コマンドラインでコントローラーを作成するには、以下のコマンドを実行してください。To create the controller via the command line, execute the following command:
php artisan controller:make PhotoController
そのコントローラーをリソースフルなルートとして登録しましょう。Now we can register a resourceful route to the controller:
Route::resource('photo', 'PhotoController');
この一行の宣言だけで写真リソースに対するRESTフルな多彩のアクションを処理する複数のルートを作成できます。更に、生成されたコントローラーには処理する変数とURIが指し示す各アクションのスタブメソッドが予め含まれています。This single route declaration creates multiple routes to handle a variety of RESTful actions on the photo resource. Likewise, the generated controller will already have stubbed methods for each of these actions with notes informing you which URIs and verbs they handle.
リソースフルコントローラーにより処理されるアクションActions Handled By Resource Controller
変数Verb | パスPath | アクションAction | Route NameRoute Name |
---|---|---|---|
GETGET | /リソース名/resource | indexindex | resource.indexresource.index |
GETGET | /リソース名/create/resource/create | createcreate | resource.createresource.create |
POSTPOST | /リソース名/resource | storestore | resource.storeresource.store |
GETGET | /リソース名/{resource}/resource/{resource} | showshow | resource.showresource.show |
GETGET | /リソース名/{resource}/edit/resource/{resource}/edit | editedit | resource.editresource.edit |
PUT/PATCHPUT/PATCH | /リソース名/{resource}/resource/{resource} | updateupdate | resource.updateresource.update |
DELETEDELETE | /リソース名/{resource}/resource/{resource} | destroydestroy | resource.destroyresource.destroy |
リソースアクションの一部のみを取り扱いたい場合もあると思います。Sometimes you may only need to handle a subset of the resource actions:
php artisan controller:make PhotoController --only=index,show
php artisan controller:make PhotoController --except=index
更に、処理するルートに関して、アクションの一部を指定したいこともあるでしょう。And, you may also specify a subset of actions to handle on the route:
Route::resource('photo', 'PhotoController',
array('only' => array('index', 'show')));
Route::resource('photo', 'PhotoController',
array('except' => array('create', 'store', 'update', 'destroy')));
デフォルトで、すべてのリソースコントローラーアクションはルート名を持ちます。しかしオプションにnames
配列を渡すことで、こうした名前をオーバーライドすることもできます。By default, all resource
controller actions have a route name; however, you
can override these names by passing a
names
array with your
options:
Route::resource('photo', 'PhotoController',
array('names' => array('create' => 'photo.build')));
ネストしたリソースコントローラーの処理Handling Nested Resource Controllers
「ネスト」したリソースコントローラーには、ルート宣言で「ドット」記法を使用してください。To "nest" resource controllers, use "dot" notation in your route declaration:
Route::resource('photos.comments', 'PhotoCommentController');
これにより、「ネスト」したリソースが登録され、photos/{photoResource}/comments/{commentResource}
のURLにアクセスできるようになります。This route will register a
"nested" resource that may be accessed
with URLs like the following:
photos/{photoResource}/comments/{commentResource}
.
class PhotoCommentController extends BaseController {
public function show($photoId, $commentId)
{
//
}
}
リソースコントローラーにルートを追加するAdding Additional Routes To Resource Controllers
デフォルトのリソースルート以外のルートをリソースコントローラーへ追加する必要がある場合は、Route::resource
の呼び出しより前に定義する必要があります。If it becomes necessary for you
to add additional routes to a resource controller
beyond the default resource routes, you should
define those routes before your call to
Route::resource
:
Route::get('photos/popular', 'PhotoController@method');
Route::resource('photos', 'PhotoController');
見つからないメソッドの処理Handling Missing Methods
Route::controller
を使用する場合、指定されたコントローラで一致するメソッドが見つからないときに呼び出されるバックアップメソッドを定義することが可能です。メソッド名はmissingMethod
と付け、メソッド名とリクエストのパラメーターを配列で受け取ります。When using
Route::controller
, a catch-all method
may be defined which will be called when no other
matching method is found on a given controller. The
method should be named missingMethod
,
and receives the method and parameter array for the
request:
バックアップメソッドを定義するDefining A Catch-All Method
public function missingMethod($parameters = array())
{
//
}
リソースコントローラーを使用する場合は、見つからないメソッドを処理するため、コントローラーに__call
マジックメソッドを定義します。If you are using resource
controllers, you should define a __call
magic method on the controller to handle any missing
methods.