Readouble

Laravel 4.2 ルーティング

基本的なルーティングBasic Routing

アプリケーションで使用するほとんどのルートはapp/routes.phpの中で定義されます。LaravelのもっともシンプルなルートはURIとクロージャーで構成されます。Most of the routes for your application will be defined in the app/routes.php file. The simplest Laravel routes consist of a URI and a Closure callback.

基本のGETルートBasic GET Route

Route::get('/', function()
{
	return 'Hello World';
});

基本のPOSTルートBasic POST Route

Route::post('foo/bar', function()
{
	return 'Hello World';
});

複数のHTTP動詞に対応するルートの登録Registering A Route For Multiple Verbs

Route::match(array('GET', 'POST'), '/', function()
{
	return 'Hello World';
});

全てのHTTP動詞に対応するルートの登録Registering A Route Responding To Any HTTP Verb

Route::any('foo', function()
{
	return 'Hello World';
});

HTTPSによるルーティングを強要するForcing A Route To Be Served Over HTTPS

Route::get('foo', array('https', function()
{
	return 'Must be over HTTPS';
}));

ルートに対するURLを生成する必要があることも多いでしょう。URL::toを使用してください。Often, you will need to generate URLs to your routes, you may do so using the URL::to method:

$url = URL::to('foo');

ルートパラメーターRoute Parameters

Route::get('user/{id}', function($id)
{
	return 'User '.$id;
});

オプションのルートパラメーターOptional Route Parameters

Route::get('user/{name?}', function($name = null)
{
	return $name;
});

デフォルト値を指定したオプションのルートパラメーターOptional Route Parameters With Defaults

Route::get('user/{name?}', function($name = 'John')
{
	return $name;
});

正規表現によるルートの束縛Regular Expression Route Constraints

Route::get('user/{name}', function($name)
{
	//
})
->where('name', '[A-Za-z] ');

Route::get('user/{id}', function($id)
{
	//
})
->where('id', '[0-9] ');

Whereに配列で指定するPassing An Array Of Wheres

もちろん、必要に応じ、配列でも束縛を指定できます。Of course, you may pass an array of constraints when necessary:

Route::get('user/{id}/{name}', function($id, $name)
{
	//
})
->where(array('id' => '[0-9] ', 'name' => '[a-z] '))

グローバルパターンを定義するDefining Global Patterns

ルートパラメータを常時束縛したい場合は、patternメソッドで正規表現を指定することができます。If you would like a route parameter to always be constrained by a given regular expression, you may use the pattern method:

Route::pattern('id', '[0-9] ');

Route::get('user/{id}', function($id)
{
	// Only called if {id} is numeric.
});

ルートパラメーター値にアクセスするAccessing A Route Parameter Value

ルートの外でルートパラメータにアクセスする必要があれば、Route::inputメソッドを使用して下さい。If you need to access a route parameter value outside of a route, you may use the Route::input method:

Route::filter('foo', function()
{
	if (Route::input('id') == 1)
	{
		//
	}
});

ルートフィルターRoute Filters

ルートフィルターは指定されたルートへのアクセスを制限するための便利な方法で、例えば認証が必要なサイトの領域を作成するのに便利です。Laravelフレームワークには多くのフィルターが含まれており、その中にはauthフィルター、guestフィルター、csrfフィルターが含まれます。app/filter.phpファイルの中で定義されます。Route filters provide a convenient way of limiting access to a given route, which is useful for creating areas of your site which require authentication. There are several filters included in the Laravel framework, including an auth filter, an auth.basic filter, a guest filter, and a csrf filter. These are located in the app/filters.php file.

注意: アプリケーションの環境がtestingの時、フィルターは無効です。Note: Filters are disabled when the application environment is testing.

ルートフィルターを定義するDefining A Route Filter

Route::filter('old', function()
{
	if (Input::get('age') < 200)
	{
		return Redirect::to('home');
	}
});

フィルターからレスポンスを返す場合、そのレスポンスはリクエストに対するレスポンスと考えられ、そのルートが実行されることはありません。そのルートに指定されているafterフィルターもキャンセルされます。If the filter returns a response, that response is considered the response to the request and the route will not execute. Any after filters on the route are also cancelled.

ルートにフィルターを指定するAttaching A Filter To A Route

Route::get('user', array('before' => 'old', function()
{
	return 'You are over 200 years old!';
}));

コントローラーアクションにフィルターを指定するAttaching A Filter To A Controller Action

Route::get('user', array('before' => 'old', 'uses' => 'UserController@showProfile'));

ルートに複数のフィルターを指定するAttaching Multiple Filters To A Route

Route::get('user', array('before' => 'auth|old', function()
{
	return 'You are authenticated and over 200 years old!';
}));

配列により複数のフィルターを指定するAttaching Multiple Filters Via Array

Route::get('user', array('before' => array('auth', 'old'), function()
{
	return 'You are authenticated and over 200 years old!';
}));

フィルターパラメーターを指定するSpecifying Filter Parameters

Route::filter('age', function($route, $request, $value)
{
	//
});

Route::get('user', array('before' => 'age:200', function()
{
	return 'Hello World';
}));

Afterフィルターは$responseを3番目の引数として受け取ります。After filters receive a $response as the third argument passed to the filter:

Route::filter('log', function($route, $request, $response)
{
	//
});

パターンを元にしたフィルターPattern Based Filters

URIを元にしたルート全体にフィルターを適用することも可能です。You may also specify that a filter applies to an entire set of routes based on their URI.

Route::filter('admin', function()
{
	//
});

Route::when('admin/*', 'admin');

上の例では、adminフィルターがadmin/で始まる全てのルートに適用されます。アスタリスクはワイルドカードとして取り扱われ、全ての文字と結び付けられます。In the example above, the admin filter would be applied to all routes beginning with admin/. The asterisk is used as a wildcard, and will match any combination of characters.

HTTP動詞によりパターンフィルターを制約することもできます。You may also constrain pattern filters by HTTP verbs:

Route::when('admin/*', 'admin', array('post'));

フィルタークラスFilter Classes

より上級のフィルタリングのため、クロージャーの代わりにクラスを使用することもできます。テストを行いやすくするため、これらのフィルターをアプリケーションのIoCコンテナで解決することで、依存性を注入することも可能です。For advanced filtering, you may wish to use a class instead of a Closure. Since filter classes are resolved out of the application IoC Container[/docs/4.2/ioc], you will be able to utilize dependency injection in these filters for greater testability.

クラスベースのフィルターを登録するRegistering A Class Based Filter

Route::filter('foo', 'FooFilter');

デフォルトでは、FooFilterクラスのfilterメソッドが呼び出されます。By default, the filter method on the FooFilter class will be called:

class FooFilter {

	public function filter()
	{
		// フィルターのロジック...
	}

}

fileterメソッドを使用したくないのでしたら、他のメソッドを指定してください。If you do not wish to use the filter method, just specify another method:

Route::filter('foo', 'FooFilter@foo');

名前付きルートNamed Routes

リダイレクトやURLを生成する場合により便利にするために、ルートにつけた名前で、ルートを参照することができます。このようにルートに名前を指定してください。Named routes make referring to routes when generating redirects or URLs more convenient. You may specify a name for a route like so:

Route::get('user/profile', array('as' => 'profile', function()
{
	//
}));

コントローラアクションに対してもルート名を指定できます。You may also specify route names for controller actions:

Route::get('user/profile', array('as' => 'profile', 'uses' => 'UserController@showProfile'));

これで、ルート名をURLやリダイレクトを生成する場合に使用できます。Now, you may use the route's name when generating URLs or redirects:

$url = URL::route('profile');

$redirect = Redirect::route('profile');

実行中のルート名はcurrentRouteNameメソッドでアクセスできます。You may access the name of a route that is running via the currentRouteName method:

$name = Route::currentRouteName();

ルートグループRoute Groups

フィルターをルートのグループに対して使用する必要がある場合もあるでしょう。それぞれのルートにフィルターを個別に指定する代わりに、ルートグループを使用できます。Sometimes you may need to apply filters to a group of routes. Instead of specifying the filter on each route, you may use a route group:

Route::group(array('before' => 'auth'), function()
{
	Route::get('/', function()
	{
		// Authフィルター通過
	});

	Route::get('user/profile', function()
	{
		// Authフィルター通過
	});
});

またgroupの配列へ、namespace引数により、そのグループのコントローラー全部に対し、名前空間を指定することもできます。You may also use the namespace parameter within your group array to specify all controllers within that group as being in a given namespace:

Route::group(array('namespace' => 'Admin'), function()
{
	//
});

サブドメインルーティングSub-Domain Routing

Laravelのルートではサブドメインもワイルドカードで処理でき、ドメイン名のワイルドカードパラメーターをルートに渡すことも可能です。Laravel routes are also able to handle wildcard sub-domains, and will pass your wildcard parameters from the domain:

サブドメインルートの登録Registering Sub-Domain Routes

Route::group(array('domain' => '{account}.myapp.com'), function()
{

	Route::get('user/{id}', function($account, $id)
	{
		//
	});

});

ルートのプリフィックスRoute Prefixing

ルートグループのプレフィックスはグループの属性配列でprefixオプションを使用し指定します。A group of routes may be prefixed by using the prefix option in the attributes array of a group:

Route::group(array('prefix' => 'admin'), function()
{

	Route::get('user', function()
	{
		//
	});

});

ルートとモデルの結合Route Model Binding

モデル結合はルートからモデルのインスタンスを受け取るための便利な手法です。例えば、ユーザーIDを受け取る代わりに、そのIDに合致するUserモデルのインスタンスを受け取ることができます。最初にRoute::modelメソッドで指定したパラメーターで使用するモデルを指定します。Model binding provides a convenient way to inject model instances into your routes. For example, instead of injecting a user's ID, you can inject the entire User model instance that matches the given ID. First, use the Route::model method to specify the model that should be used for a given parameter:

モデルとパラメーターを結合するBinding A Parameter To A Model

Route::model('user', 'User');

次に、{user}パラメーターを含んだルートを定義します。Next, define a route that contains a {user} parameter:

Route::get('profile/{user}', function(User $user)
{
	//
});

{user}パラメーターをUserモデルと結合済みですから、ルートからUserインスタンスが渡されます。例えば、profile/1がリクエストされると、IDが1のUserインスタンスが渡されます。Since we have bound the {user} parameter to the User model, a User instance will be injected into the route. So, for example, a request to profile/1 will inject the User instance which has an ID of 1.

**注目:**もしデータベースにモデルのインスタンスが見つからない場合、404エラーがスローされます。Note: If a matching model instance is not found in the database, a 404 error will be thrown.

もし独自の"not found"動作を指定したい場合は、modelメソッドへクロージャーを3つ目の引数として渡してください。If you wish to specify your own "not found" behavior, you may pass a Closure as the third argument to the model method:

Route::model('user', 'User', function()
{
	throw new NotFoundHttpException;
});

場合によっては自分でルートパラメーターを解決したい場合もあるでしょう。シンプルにRoute::bindメソッドを使用してください。Sometimes you may wish to use your own resolver for route parameters. Simply use the Route::bind method:

Route::bind('user', function($value, $route)
{
	return User::where('name', $value)->first();
});

404エラーのスローThrowing 404 Errors

手動で404エラーをルートから発生させるには2つのやり方があります。最初の方法はApp::abortメソッドを使用するものです。There are two ways to manually trigger a 404 error from a route. First, you may use the App::abort method:

App::abort(404);

2つめの方法はSymfony\Component\HttpKernel\Exception\NotFoundHttpExceptionのインスタンスをスローするやりかたです。Second, you may throw an instance of Symfony\Component\HttpKernel\Exception\NotFoundHttpException.

404例外の取り扱いやこうしたエラーのカスタム処理を使用する詳しい方法はドキュメントのエラーの章をご覧ください。More information on handling 404 exceptions and using custom responses for these errors may be found in the errors[/docs/4.2/errors#handling-404-errors] section of the documentation.

コントローラーへのルーティングRouting To Controllers

Laravelはクロージャーでのルート定義だけではなく、コントローラークラスによる定義、さらにリソースコントローラーの生成も可能です。Laravel allows you to not only route to Closures, but also to controller classes, and even allows the creation of resource controllers[/docs/4.2/controllers#restful-resource-controllers].

詳細はコントローラーをご覧ください。See the documentation on Controllers[/docs/4.2/controllers] for more details.

章選択

Artisan CLI

設定

明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

テストコード表示
両コード表示
Pestのみ表示
PHPUnitのみ表示
和文変換

対象文字列と置換文字列を半角スペースで区切ってください。(最大5組各10文字まで)

本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作