Yii-url-formats
提供:Dev Guides
Yii-URL形式
Yiiアプリケーションが要求されたURLを処理するとき、最初に、URLを解析してルートにします。 次に、要求を処理するために、このルートを使用して、対応するコントローラーアクションをインスタンス化します。 このプロセスは*ルーティング*と呼ばれます。 逆のプロセスはURL作成と呼ばれます。 urlManager アプリケーションコンポーネントは、ルーティングとURLの作成を担当します。 それは2つの方法を提供します-
- * parseRequest()*-要求をルートに解析します。
- * createUrl()*-指定されたルートからURLを作成します。
URLフォーマット
*urlManager* アプリケーションコンポーネントは2つのURL形式をサポートしています-
*デフォルトの形式では、クエリパラメータ_r_を使用してルートを表します。 たとえば、URL*/index.php?r = news/view&id = 5 *は、ルート *news/view* および *id* クエリパラメーター5を表します。
* プリティURL形式は、エントリスクリプト名に追加のパスを使用します。 たとえば、前の例では、プリティ形式は */index.php/news/view/5* になります。 この形式を使用するには、URLルールを設定する必要があります。
プリティURL形式を有効にし、エントリスクリプト名を非表示にするには、次の手順を実行します-
ステップ1 *-以下の方法で *config/web.php ファイルを変更します。
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'request' => [
//!!! insert a secret key in the following (if it is empty) -
//this is required by cookie validation
'cookieValidationKey' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
//send all mails to a file by default. You have to set
//'useFileTransport' to false and configure a transport
//for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true
],
'db' => require(__DIR__ . '/db.php'),
],
'modules' => [
'hello' => [
'class' => 'app\modules\hello\Hello',
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
//configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
?>
- pretty URL形式*を有効にし、エントリスクリプト名を無効にしました。
ステップ2 *-Webブラウザのアドレスバーに *http://localhost:8080/site/about と入力すると、きれいなURLが実際に表示されます。
URLは http://localhost:8080/index.php?r = site/about ではなくなっていることに注意してください。