要更改应用程序的默认路由,应配置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。您将看到默认的联系页面。

要将应用程序暂时置于维护模式,应配置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');
}此方法仅呈现路线视图。
步骤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。
该警予\佣工\网址助手类,还提供了一些有用的方法。
步骤4-修改路由视图,如以下代码中所给。
<?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,您将看到以下内容。

作者:terry,如若转载,请注明出处:https://www.web176.com/yii/517.html
支付宝
微信