使用Url::to('index/msg) 生成路径信息时的问题 [ 2.0 版本 ]
一般默认首页使用index/index访问的时候,使用Url::to生成的链接地址会出错,而直接使用 / 访问的话,则没有这个问题。
好像Url::to的时候,并没有分析当前路径信息的,而是很傻瓜式的生成了路径信息,有没有办法在什么地方配置这个自动分析当前url来生成正确的地址的?
难道只能用绝对路径来生成url???
最佳答案
其他 2 个回答
-
vendor/yiisoft/yii2/helpers/BaseUrl.php
这儿有说明。
例如site/index site是控制器名,index是行为名。/** * Creates a URL based on the given parameters. * * This method is very similar to [[toRoute()]]. The only difference is that this method * requires a route to be specified as an array only. If a string is given, it will be treated as a URL. * In particular, if `$url` is * * - an array: [[toRoute()]] will be called to generate the URL. For example: * `['site/index']`, `['post/index', 'page' => 2]`. Please refer to [[toRoute()]] for more details * on how to specify a route. * - a string with a leading `@`: it is treated as an alias, and the corresponding aliased string * will be returned. * - an empty string: the currently requested URL will be returned; * - a normal string: it will be returned as is. * * When `$scheme` is specified (either a string or true), an absolute URL with host info (obtained from * [[\yii\web\UrlManager::hostInfo]]) will be returned. If `$url` is already an absolute URL, its scheme * will be replaced with the specified one. * * Below are some examples of using this method: * * ```php * // /index.php?r=site/index * echo Url::to(['site/index']); * * // /index.php?r=site/index&src=ref1#name * echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name']); * * // /index.php?r=post/index assume the alias "@posts" is defined as "/post/index" * echo Url::to(['@posts']); * * // the currently requested URL * echo Url::to(); * * // /images/logo.gif * echo Url::to('@web/images/logo.gif'); * * // images/logo.gif * echo Url::to('images/logo.gif'); * * // http://www.example.com/images/logo.gif * echo Url::to('@web/images/logo.gif', true); * * // https://www.example.com/images/logo.gif * echo Url::to('@web/images/logo.gif', 'https'); * ``` * * * @param array|string $url the parameter to be used to generate a valid URL * @param boolean|string $scheme the URI scheme to use in the generated URL: * * - `false` (default): generating a relative URL. * - `true`: returning an absolute base URL whose scheme is the same as that in [[\yii\web\UrlManager::hostInfo]]. * - string: generating an absolute URL with the specified scheme (either `http` or `https`). * * @return string the generated URL * @throws InvalidParamException a relative route is given while there is no active controller */ public static function to($url = '', $scheme = false) { if (is_array($url)) { return static::toRoute($url, $scheme); } $url = Yii::getAlias($url); if ($url === '') { $url = Yii::$app->getRequest()->getUrl(); } if (!$scheme) { return $url; } if (strncmp($url, '//', 2) === 0) { // e.g. //hostname/path/to/resource return is_string($scheme) ? "$scheme:$url" : $url; } if (($pos = strpos($url, ':')) == false || !ctype_alpha(substr($url, 0, $pos))) { // turn relative URL into absolute $url = Yii::$app->getUrlManager()->getHostInfo() . '/' . ltrim($url, '/'); } if (is_string($scheme) && ($pos = strpos($url, ':')) !== false) { // replace the scheme with the specified one $url = $scheme . substr($url, $pos); } return $url; }
cfanbo
注册时间:2015-05-23
最后登录:2015-09-10
在线时长:11小时52分
最后登录:2015-09-10
在线时长:11小时52分
- 粉丝2
- 金钱110
- 威望0
- 积分220