Yii-url-routing
Yii-URLルーティング
アプリケーションのデフォルトルートを変更するには、 defaultRoute プロパティを設定する必要があります。
ステップ1 *-以下の方法で *config/web.php ファイルを変更します。
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'defaultRoute' => 'site/contact',
'components' => [
//other code
?>
ステップ2 *- *http://localhost:8080/index.php に移動しました。 デフォルトの contact ページが表示されます。
- アプリケーションを一時的にメンテナンスモードにするには、 *yii \ web \ Application
- $ catchAll* プロパティを設定する必要があります。
ステップ3 *-次の関数を *SiteController に追加します。
public function actionMaintenance() {
echo "<h1>Maintenance</h1>";
}
ステップ4 *-次に、次の方法で *config/web.php ファイルを変更します。
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'catchAll' => ['site/maintenance'],
'components' => [
//OTHER CODE
- ステップ5 *-アプリケーションのURLを入力すると、次のように表示されます。
URLを作成する
- さまざまな種類のURLを作成するには、* yii \ helpers \ Url
- to()*ヘルパーメソッドを使用できます。 次の例では、デフォルトのURL形式が使用されていると想定しています。
ステップ1 *- actionRoutes()メソッドを *SiteController に追加します。
public function actionRoutes() {
return $this->render('routes');
}
このメソッドは、単に routes ビューをレンダリングします。
ステップ2 *-views/siteディレクトリ内に、次のコードで *routes.php というファイルを作成します。
<?php
use yii\helpers\Url;
?>
<h4>
<b>Url::to(['post/index']):</b>
<?php
//creates a URL to a route:/index.php?r = post/index
echo Url::to(['post/index']);
?>
</h4>
<h4>
<b>Url::to(['post/view', 'id' => 100]):</b>
<?php
//creates a URL to a route with parameters:/index.php?r = post/view&id=100
echo Url::to(['post/view', 'id' => 100]);
?>
</h4>
<h4>
<b>Url::to(['post/view', 'id' => 100, '#' => 'content']):</b>
<?php
//creates an anchored URL:/index.php?r = post/view&id=100#content
echo Url::to(['post/view', 'id' => 100, '#' => 'content']);
?>
</h4>
<h4>
<b>Url::to(['post/index'], true):</b>
<?php
//creates an absolute URL: http://www.example.com/index.php?r=post/index
echo Url::to(['post/index'], true);
?>
</h4>
<h4>
<b>Url::to(['post/index'], 'https'):</b>
<?php
//creates an absolute URL using the https scheme: https://www.example.com/index.php?r=post/index
echo Url::to(['post/index'], 'https');
?>
</h4>
ステップ3 *- *http://localhost:8080/index.php?r = site/routes と入力すると、* to()*関数の使用法がいくつか表示されます。
yii \ helpers \ Url :: to()*メソッドに渡されるルートは、次の規則に従って相対または絶対にすることができます-
ルートが空の場合、現在要求されているルートが使用されます。
ルートに先頭のスラッシュがない場合、現在のモジュールに関連するルートと見なされます。
ルートにスラッシュが含まれていない場合、現在のコントローラーのアクションIDと見なされます。
*yii \ helpers \ Url* ヘルパークラスは、いくつかの便利なメソッドも提供します。
ステップ4 *-次のコードに示すように、 *routes ビューを変更します。
<?php
use yii\helpers\Url;
?>
<h4>
<b>Url::home():</b>
<?php
//home page URL:/index.php?r=site/index
echo Url::home();
?>
</h4>
<h4>
<b>Url::base():</b>
<?php
//the base URL, useful if the application is deployed in a sub-folder of the Web root
echo Url::base();
?>
</h4>
<h4>
<b>Url::canonical():</b>
<?php
//the canonical URL of the currently requested URL
//see https://en.wikipedia.org/wiki/Canonical_link_element
echo Url::canonical();
?>
</h4>
<h4>
<b>Url::previous():</b>
<?php
//remember the currently requested URL and retrieve it back in later requests
Url::remember();
echo Url::previous();
?>
</h4>
ステップ5 *-Webブラウザでアドレス *http://localhost:8080/index.php?r = site/routes を入力すると、次のように表示されます。