Readouble

Laravel 4.2 ビューとレスポンス

レスポンスの基本Basic Responses

ルートへ文字列をリターンするReturning Strings From Routes

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

カスタムレスポンスを生成するCreating Custom Responses

ResponseインスタンスはSymfony\Component\HttpFoundation\Responseクラスを継承しています。これによりHTTPレスポンスを組み立てるための数多くのメソッドが提供されています。A Response instance inherits from the Symfony\Component\HttpFoundation\Response class, providing a variety of methods for building HTTP responses.

$response = Response::make($contents, $statusCode);

$response->header('Content-Type', $value);

return $response;

Responseクラスメソッドへアクセスする必要があり、レスポンスのコンテンツにはビューを返したい場合、Response::viewメソッドが便利です。If you need access to the Response class methods, but want to return a view as the response content, you may use the Response::view method for convenience:

return Response::view('hello')->header('Content-Type', $type);

レスポンスにクッキーを付けるAttaching Cookies To Responses

$cookie = Cookie::make('name', 'value');

return Response::make($content)->withCookie($cookie);

リダイレクトRedirects

リダイレクトをリターンするReturning A Redirect

return Redirect::to('user/login');

フラッシュデータと伴にリダイレクトをリターンするReturning A Redirect With Flash Data

return Redirect::to('user/login')->with('message', 'Login Failed');

注目:withメソッドにより、フラッシュデーターがセッションに保存されますので、通常Session::getメソッドで取得できます。Note: Since the with method flashes data to the session, you may retrieve the data using the typical Session::get method.

名前付きルートへリダイレクトするReturning A Redirect To A Named Route

return Redirect::route('login');

名前付きルートへパラメーターを渡し、リダイレクトするReturning A Redirect To A Named Route With Parameters

return Redirect::route('profile', array(1));

名前付きルートへ名前付きパラメータでリダイレクトするReturning A Redirect To A Named Route Using Named Parameters

return Redirect::route('profile', array('user' => 1));

コントローラーアクションへリダイレクトするReturning A Redirect To A Controller Action

return Redirect::action('HomeController@index');

コントローラーアクションへパラメーターを渡し、リダイレクトするReturning A Redirect To A Controller Action With Parameters

return Redirect::action('UserController@profile', array(1));

コントローラーアクションへ名前付きパラメーターを渡し、リダイレクトするReturning A Redirect To A Controller Action Using Named Parameters

return Redirect::action('UserController@profile', array('user' => 1));

ビューViews

典型的なビューはアプリケーションのHTMLで構成されており、コントローラーとドメインロジックをプレゼンテーションロジックから切り離す便利な方法です。ビューはapp/viewsディレクトリーに保存されます。Views typically contain the HTML of your application and provide a convenient way of separating your controller and domain logic from your presentation logic. Views are stored in the app/views directory.

シンブルなビューは以下のような形式でしょう。A simple view could look something like this:

<!-- app/views/greeting.phpに保存されているビュー -->

<html>
	<body>
		<h1>こんにちは、<? echo $name; ?>さん</h1>
	</body>
</html>

このビューをブラウザに送り返すには次のようにします。This view may be returned to the browser like so:

Route::get('/', function()
{
	return View::make('greeting', array('name' => 'Taylor'));
});

View::makeの第二引数にはビューで使用するデーターの配列を渡します。The second argument passed to View::make is an array of data that should be made available to the view.

ビューにデーターを渡すPassing Data To Views

// Using conventional approach
$view = View::make('greeting')->with('name', 'Steve');

// Using Magic Methods
$view = View::make('greeting')->withName('steve');

上の例では$name変数はビューでアクセスされ、Steveの値を取ります。In the example above the variable $name would be accessible from the view, and would contain Steve.

ご希望であれば、データーの配列をmakeメソッドの第2パラメーターとして渡すこともできます。If you wish, you may pass an array of data as the second parameter given to the make method:

$view = View::make('greetings', $data);

さらに全てのビューでアクセス可能なデーターを渡すこともできます。You may also share a piece of data across all views:

View::share('name', 'Steve');

ビューにサブビューを渡すPassing A Sub-View To A View

例えば、app/views/child/view.phpとして保存されているサブビューがある場合、他のビューに以下のように渡せます。Sometimes you may wish to pass a view into another view. For example, given a sub-view stored at app/views/child/view.php, we could pass it to another view like so:

$view = View::make('greeting')->nest('child', 'child.view');

$view = View::make('greeting')->nest('child', 'child.view', $data);

サブビューは親のビューからレンダーリングすることもできます。The sub-view can then be rendered from the parent view:

<html>
	<body>
		<h1>Hello!</h1>
		<?php echo $child; ?>
	</body>
</html>

ビューの存在を確認するDetermining If A View Exists

ビューが存在するかを確認する必要がある場合、View::existsメソッドが使用できます。If you need to check if a view exists, use the View::exists method:

if (View::exists('emails.customer'))
{
	//
}

ビューコンポーサーView Composers

ビューコンポーサーはビューがレンダーされる時に呼び出されるコールバック、もしくはクラスメソッドです。アプリケーション全体でレンダーされる可能性があるビューで、それに結びつけるデータが存在するならば、ビューコンポーサーで一箇所にコードをまとめる事ができます。ですから、ビューコンポーサーは"ビューモデル"とか"プレゼンター"のように機能します。View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want bound to a given view each time that view is rendered throughout your application, a view composer can organize that code into a single location. Therefore, view composers may function like "view models" or "presenters".

ビューコンポーサーを定義するDefining A View Composer

View::composer('profile', function($view)
{
	$view->with('count', User::count());
});

profileビューがレンダーされるたびに、countデーターはビューと結合されます。Now each time the profile view is rendered, the count data will be bound to the view.

ビューコンポーサーを複数のビューに対し、一度に設定することも可能です。You may also attach a view composer to multiple views at once:

View::composer(array('profile','dashboard'), function($view)
{
    $view->with('count', User::count());
});

クラスベースのビューコンポーサーを代わりに使っている場合、便利なのはお望みでしたら、アプリケーションのIoCコンテナを使いインスタンスの解決ができることです。If you would rather use a class based composer, which will provide the benefits of being resolved through the application IoC Container[/docs/4.2/ioc], you may do so:

View::composer('profile', 'ProfileComposer');

ビューコンポーサークラスは次のような形式です。A view composer class should be defined like so:

class ProfileComposer {

	public function compose($view)
	{
		$view->with('count', User::count());
	}

}

複数のコンポーサーを定義するDefining Multiple Composers

composersメソッドで、コンポーサーのグループを一度に登録できます。You may use the composers method to register a group of composers at the same time:

View::composers(array(
	'AdminComposer' => array('admin.index', 'admin.profile'),
	'UserComposer' => 'user',
	'ProductComposer@create' => 'product' 
));

**注目:**コンポーサークラスをどこに保存するかという規則はありません。どこにでも自由に設置し、composer.jsonファイルでディレクティブを用い、オートロードできるように設定して下さい。Note: There is no convention on where composer classes may be stored. You are free to store them anywhere as long as they can be autoloaded using the directives in your composer.json file.

ビュークリエーターView Creators

ビュークリエーターはビューコンポーサーと全く同じように動作します。違いはビューがインスタンス化されたらすぐに実行されることです。ビュークリエーターを使用するには、ただcreatorメソッドを使用するだけです。View creators work almost exactly like view composers; however, they are fired immediately when the view is instantiated. To register a view creator, simply use the creator method:

View::creator('profile', function($view)
{
	$view->with('count', User::count());
});

特別なレスポンスSpecial Responses

JSONレスポンスを生成するCreating A JSON Response

return Response::json(array('name' => 'Steve', 'state' => 'CA'));

JSONレスポンスを生成するCreating A JSONP Response

return Response::json(array('name' => 'Steve', 'state' => 'CA'))->setCallback(Input::get('callback'));

ファイルダウンロードレスポンスを生成するCreating A File Download Response

return Response::download($pathToFile);

return Response::download($pathToFile, $name, $headers);

注目: ファイルのダウンロードを管理するSymfonyのHttpFoundationは、ダウンロードするファイルにASCIIのファイル名をつけることを要求しています。Note: Symfony HttpFoundation, which manages file downloads, requires the file being downloaded to have an ASCII file name.

レスポンスマクロResponse Macros

多くのルートやコントローラの中で再利用できるように、カスタムレスポンスを定義することもできます。Response::macroメソッドを使用して下さい。If you would like to define a custom response that you can re-use in a variety of your routes and controllers, you may use the Response::macro method:

Response::macro('caps', function($value)
{
	return Response::make(strtoupper($value));
});

macro機能は、最初の引数として名前を受けます。2つ目はクロージャーです。マクロのクロージャーはResponseクラスにマクロ名を付け呼び出された場合に実行されます。The macro function accepts a name as its first argument, and a Closure as its second. The macro's Closure will be executed when calling the macro name on the Response class:

return Response::caps('foo');

app/start下のファイルの一つの中で、マクロを定義することができます。もしくは、マクロ定義を独立させたファイルに記述し、startファイルの一つから読み込むこともできます。You may define your macros in one of your app/start files. Alternatively, you may organize your macros into a separate file which is included from one of your start files.

章選択

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に保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作