イントロダクションIntroduction
Laravelはアプリケーションに対するURL生成の手助けとなる、数多くのヘルパを提供しています。これは主にテンプレートやAPIレスポンスでリンクを構築するためにはもちろんのこと、リダイレクトレスポンスの生成や、アプリケーションの他の部分でも役立ちます。Laravel provides several helpers to assist you in generating URLs for your application. Of course, these are mainly helpful when building links in your templates and API responses, or when generating redirect responses to another part of your application.
基礎The Basics
URL生成の基礎Generating Basic URLs
url
ヘルパは、アプリケーションに対する任意のURLを生成するために使用されます。生成されるURLには自動的に、現在のリクエストのスキーム(HTTP/HTTPS)とホストが使用されます。The url
helper may
be used to generate arbitrary URLs for your
application. The generated URL will automatically
use the scheme (HTTP or HTTPS) and host from the
current request:
$post = App\Post::find(1);
echo url("/posts/{$post->id}");
// http://example.com/posts/1
現在のURLへのアクセスAccessing The Current URL
url
ヘルパにパスを指定しないと、Illuminate\Routing\UrlGenerator
インスタンスが返され、現在のURLに関する情報へアクセスできます。If no path is provided to the
url
helper, a
Illuminate\Routing\UrlGenerator
instance is returned, allowing you to access
information about the current URL:
// クエリ文字列を除いた現在のURL
echo url()->current();
// クエリ文字列を含んだ現在のURL
echo url()->full();
// 直前のリクエストの完全なURL
echo url()->previous();
こうしたメソッドには、URL
ファサードを使用してもアクセスできます。Each of these methods may also be
accessed via the URL
facade[/docs/{{version}}/facades]:
use Illuminate\Support\Facades\URL;
echo URL::current();
名前付きルートのURLURLs For Named Routes
route
ヘルパは、名前付きルートへのURLを生成するために使用します。名前付きルートにより、定義したルートの実際のURLを指定せずとも、URLを生成することができます。ですから、ルートのURLを変更しても、route
関数の呼び出しを変更する必要はありません。例として以下のように、アプリケーションが次のルートを持っていると想像してください。The route
helper may
be used to generate URLs to named routes. Named
routes allow you to generate URLs without being
coupled to the actual URL defined on the route.
Therefore, if the route's URL changes, no changes
need to be made to your route
function
calls. For example, imagine your application
contains a route defined like the
following:
Route::get('/post/{post}', function () {
//
})->name('post.show');
このルートへのURLを生成するには、次のようにroute
ヘルパを使用します。To generate a URL to this route,
you may use the route
helper like
so:
echo route('post.show', ['post' => 1]);
// http://example.com/post/1
Eloquentモデルの主キーを使用するURLを生成することもよくあると思います。そのため、Eloquentモデルをパラメータ値として渡すことができます。route
ヘルパは、そのモデルの主キーを自動的に取り出します。You will often be generating URLs
using the primary key of Eloquent
models[/docs/{{version}}/eloquent]. For this
reason, you may pass Eloquent models as parameter
values. The route
helper will
automatically extract the model's primary
key:
echo route('post.show', ['post' => $post]);
コントローラアクションのURLURLs For Controller Actions
action
関数は、指定するコントローラアクションに対するURLを生成します。コントローラの完全な名前空間を渡す必要はありません。代わりに、App\Http\Controllers
名前空間からの相対的なコントローラクラス名を指定してください。The action
function
generates a URL for the given controller action. You
do not need to pass the full namespace of the
controller. Instead, pass the controller class name
relative to the App\Http\Controllers
namespace:
$url = action('HomeController@index');
コントローラメソッドが、ルートパラメータを受け取る場合、この関数の第2引数として渡すことができます。If the controller method accepts route parameters, you may pass them as the second argument to the function:
$url = action('UserController@profile', ['id' => 1]);
デフォルト値Default Values
アプリケーションにより、特定のURLパラメータのデフォルト値をリクエスト全体で指定したい場合もあります。たとえば、多くのルートで{locale}
パラメータを定義していると想像してください。For some applications, you may
wish to specify request-wide default values for
certain URL parameters. For example, imagine many of
your routes define a {locale}
parameter:
Route::get('/{locale}/posts', function () {
//
})->name('post.index');
毎回route
ヘルパを呼び出すごとに、locale
をいつも指定するのは厄介です。そのため、現在のリクエストの間、常に適用されるこのパラメートのデフォルト値は、URL::defaults
メソッドを使用し定義できます。現在のリクエストでアクセスできるように、ルートミドルウェアから、このメソッドを呼び出したいかと思います。It is cumbersome to always pass
the locale
every time you call the
route
helper. So, you may use the
URL::defaults
method to define a
default value for this parameter that will always be
applied during the current request. You may wish to
call this method from a route
middleware[/docs/{{version}}/middleware#assigning-middleware-to-routes]
so that you have access to the current
request:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\URL;
class SetDefaultLocaleForUrls
{
public function handle($request, Closure $next)
{
URL::defaults(['locale' => $request->user()->locale]);
return $next($request);
}
}
一度locale
パラメータに対するデフォルト値をセットしたら、route
ヘルパを使いURLを生成する時に、値を渡す必要はもうありません。Once the default value for the
locale
parameter has been set, you are
no longer required to pass its value when generating
URLs via the route
helper.