2015-07-06 11:13:43 5552次浏览 3条回答 4 悬赏 5 金钱

一般默认首页使用index/index访问的时候,使用Url::to生成的链接地址会出错,而直接使用 / 访问的话,则没有这个问题。

好像Url::to的时候,并没有分析当前路径信息的,而是很傻瓜式的生成了路径信息,有没有办法在什么地方配置这个自动分析当前url来生成正确的地址的?

难道只能用绝对路径来生成url???

最佳答案

  • 胖纸囧 发布于 2015-07-06 16:39 举报

    骚年,这问题真的有点蛋疼。要不是我当初遇到了这个问题我都不知道怎么回答,我自己琢磨了半天才搞定,源码也没写多清楚。
    Url::to()方法里面的路径必须加中括号,如果不加生成的就是最普通的字符串。
    Url::to('index/msg')是非常错误的写法
    Url::to(['index/msg'])这才是正确写法。

    1 条回复
    回复于 2015-07-06 18:21 回复

    谢谢了!
    yii2的文档也真够蛋疼的!!!

    , , , 觉得很赞
  • 回答于 2015-07-06 11:40 举报

    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;
        }
    
  • 回答于 2015-07-06 12:51 举报

    如果是路由,用Url::toRoute,它会根据urlManager里面配置的rules来生成url

您需要登录后才可以回答。登录 | 立即注册
cfanbo
见习主管

cfanbo

注册时间:2015-05-23
最后登录:2015-09-10
在线时长:11小时52分
  • 粉丝2
  • 金钱110
  • 威望0
  • 积分220

热门问题