Yii 各种url地址写法

echo Url::home();

生成入口地址/yii2test/frontend/web/index.php;

echo  Url::base();
生成入口文件夹地址:/yii2test/frontend/web

echo  Url::base(true);

生成带有域名的入口文件夹地址:http://localhost/yii2test/frontend/web

echo Url::to();

生成当前浏览器文档地址(同时携带参数)/yii2test/frontend/web/index.php?r=item%2Fview&id=4

Url::toRoute($route, $scheme = false); $route必须为数组格式;

echo Url::toRoute(['item/view']);

生成item为控制器,view为action,地址为:/yii2test/frontend/web/index.php?r=item%2Fview

还有其它例子:echo Url::toRoute(['site/index', 'src' => 'ref1', '#' => 'name']);

Url::to和toRoute() 非常类似。这两个方法的唯一区别在于,前者要求一个路由必须用数组来指定。 如果传的参数为字符串,它将会被直接当做 URL 。

Url::to() 的第一个参数可以是:

  • 数组:将会调用 toRoute() 来生成URL。比如: ['site/index']['post/index', 'page' => 2] 。 详细用法请参考 toRoute() 。
  • 带前导 @ 的字符串:它将会被当做别名, 对应的别名字符串将会返回。
  • 空的字符串:当前请求的 URL 将会被返回;
  • 普通的字符串:返回本身

以下是一些使用示例:

// /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/edit&id=100 assume the alias "@postEdit" is defined as "post/edit"
echo Url::to(['@postEdit', 'id' => 100]); // 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');

yii\helpers\Url::current() 来创建一个基于当前请求路由和 GET 参数的 URL

// assume $_GET = ['id' => 123, 'src' => 'google'], current route is "post/view"

// /index.php?r=post/view&id=123&src=google
echo Url::current(); // /index.php?r=post/view&id=123
echo Url::current(['src' => null]);
// /index.php?r=post/view&id=100&src=google
echo Url::current(['id' => 100]);

记住 URLs

有时,你需要记住一个 URL 并在后续的请求处理中使用它。 你可以用以下方式达到这个目的:

// Remember current URL
Url::remember(); // Remember URL specified. See Url::to() for argument format.
Url::remember(['product/view', 'id' => 42]); // Remember URL specified with a name given
Url::remember(['product/view', 'id' => 42], 'product');

在后续的请求处理中,可以用如下方式获得记住的 URL:

$url = Url::previous();
$productUrl = Url::previous('product');

检查相对 URLs

你可以用如下代码检测一个 URL 是否是相对的(比如,包含主机信息部分)。

$isRelative = Url::isRelative('test/it');

yii的url写法的更多相关文章

  1. Yii 各种url地址写法

    echo Url::home(); 生成入口地址/yii2test/frontend/web/index.php: echo  Url::base();生成入口文件夹地址:/yii2test/fron ...

  2. jdbc应用程序连接Oracle rac的URL写法:

    1.应用程序连接Oracle rac的URL写法: #Oracle(AMS) jdbc.driverClassName=oracle.jdbc.driver.OracleDriver jdbc.url ...

  3. Yii的URL助手

    Url 帮助类 获得通用 URL 记住 URLs 检查相对 URLs Url 帮助类提供一系列的静态方法来帮助管理 URL. 获得通用 URL 有两种获取通用 URLS 的方法 :当前请求的 home ...

  4. jdbc url写法(集群)

    mysql集群,jdbc url写法:jdbc:mysql://[host:port],[host:port].../[database][?propertyName1][=propertyValue ...

  5. yii create url (二)

    在Yii中经常要生成URL,不管是为了自动跳转还是仅仅是一个链接.下面对Yii中的URL生成做了一个总结.提示:以下controllerX代表控制器X,actionX代表方法X.在Controller ...

  6. yii create url (一)

    1.$this->redirect这里的$this是当前的controller.可能是应用程序的也 可能是模块下的 这里仅将解一下第一个参能是url,当url是一个字符串时,它会自己动跳转 如$ ...

  7. Yii 获取url 的一些方法

    原文出处http://blog.csdn.net/iefreer/article/details/21325371 1. 获取url中的host信息: Yii::app()->request-& ...

  8. Yii 框架 URL路径简化

    Yii 框架的訪问地址若不简化会让人认为非常繁琐.未简化的地址一般格式例如以下: http://localhost:80/test/index.php?r=xxx/xxx/xxx 若是带有參数会更复杂 ...

  9. 网站开发过程中的URL写法

    在开发网页和服务器时发现,在很多地方需要写超链接 那么可以将超链接的使用者分为服务器和浏览器,以区分不同的写法 地址可能使用的情况: 1.跳转 2.转发 3.服务器资源地址 4.浏览器超链接 impo ...

随机推荐

  1. python 读取一个目录下的所有目录和文件

    #!/usr/bin/python # -*- coding:utf8 -*- import os allFileNum = 0 def printPath(level, path): global ...

  2. 《机器学习实践》程序清单3-7 plotTree函数

    这个plotTree函数,比较聪明,比较简化,比较抽象,作者一定是逐步优化和简化到这个程度的.我是花了小两天时间,断断续续看明白的,还是在参考了另一篇文章以后.这里是链接http://www.cnbl ...

  3. Oracle体系结构之密码文件管理

    oracle密码文件主要用来控制sysdba和sysoper用户用于远程登录.通常,oracle用户登录database有两种方式,一种是通过本地操作系统验证登录,一种是通过密码文件验证登录. 操作系 ...

  4. 源码 ServerParameter

    总结 1. 服务器参数类型和设置时刻 https://github.com/mongodb/mongo/blob/master/src/mongo/db/server_parameters.h // ...

  5. kerberos (https://en.wikipedia.org/wiki/Kerberos_(protocol))

    Protocol[edit] Description[edit] The client authenticates itself to the Authentication Server (AS) w ...

  6. Python实现简单HTTP服务器(一)

    一.返回固定内容 # coding:utf-8 import socket from multiprocessing import Process def handle_client(client_s ...

  7. oracle查询表结构语句

    select o.table_name, tmp.comments, o.COLUMN_NAME, t.comments, o.DATA_TYPE || CASE TRIM(o.DATA_TYPE) ...

  8. sql批量修改wordpress文章发布时间

    有时为了体现wordpress文章的更新程度,会在后台那边重新设置发布时间,有没更简单的方法呢?用sql批量修改wordpress文章发布时间怎么操作呢? UPDATE `wp_posts` SET ...

  9. iOS连续上传多张图片

    参考地址:http://www.cocoachina.com/ios/20180730/24366.html 需求是怎样的:for 循环里面.多个网络请求上传图片,每次上传一张,至于为什么每次只上传一 ...

  10. 空类指针为什么可以调用类的成员函数 以及 A(){}和A();

    1. 代码及问题 #include <iostream> using namespace std; class A { public: A() {} //A *p = new A()时:此 ...