Helpers\URL

Helpers\URL

The URL class is used for having handy methods or redirecting the page and returning the path to the current template.

Redirect - To redirect to another page instead of using a header call the static method redirect:

Url::redirect(‘path/to/go‘);

Previous - To be redirected back to the previous page:

Url::previous();

The redirect method can accept a 2nd option of true is used the path will be used as it is provided.

This is useful to redirect to an external website, by default the redirects are relative to the domain its on.

The url should be the local path excluding the application url for instance a valid case might be:

Url::redirect(‘contacts‘);

The redirect method uses the DIR constant to get the application address.

template_path has been renamed to templatePath

The next method is get_templatePath, this returns the path to the template relative from the templates folder, for instance by default it will return: http://www.example.com/templates/default/ this is useful for using absolute paths in your design files such as including css and js files.

Url::templatePath();

autolink has been renamed to autoLink

Another useful feature is the ability to scan a block of text look for any domain names then convert them into html links. To use the autoLink call url:: followed by the method name and pass in the string to autoLink:

$string = "A random piece of text that contains google.com a domain.";
echo Url::autoLink($string);

The autoLink method also accepts a 2nd parameter that will be used as the click text for instance a in the text above I want the link to say Google and not google.com.

$string = "A random piece of text that contains google.com a domain.";
echo Url::autoLink($string, ‘Google‘);

When run the link word will be Google which will link to http://google.com

时间: 2024-10-18 09:53:50

Helpers\URL的相关文章

Yii的URL助手

Url 帮助类 ? 获得通用 URL 记住 URLs 检查相对 URLs Url 帮助类提供一系列的静态方法来帮助管理 URL. 获得通用 URL ? 有两种获取通用 URLS 的方法 :当前请求的 home URL 和 base URL . 为了获取 home URL ,使用如下代码: $relativeHomeUrl = Url::home(); $absoluteHomeUrl = Url::home(true); $httpsAbsoluteHomeUrl = Url::home('ht

【Ts重构Axios】url处理篇

需求分析 首先,我们可能需要使用我们封装的axios去发送一个下面这样的简单请求 axios({ method: 'get', url: '/api/getInfo', params: { a: 1, b: 2 } }) 最终,我们希望我们发送的请求url是这样的,/api/getInfo?a=1&b=2,这样服务器就可以通过请求的url解析到我们传来的参数了.那么,我们要做的实际上就是把params的key和vaule拼接到url上,当然,params是很负责的,可能会有以下几种情况: 参数为

Yii2 中国省市区三级联动

1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region": "dev-master" 切换到项目目录 composer update; 3.配置 1) 在地区的Model中添加以下方法 public static function getRegion($parentId=0) { $result = static::find()->

Yii2 behavior运用

1 class ReturnDataTypeBehaviors extends Behavior 2 { 3 4 public $type = 'json'; 5 public $pcOrMobile = 'pc'; // or mobile 6 7 //控制器执行之后事件 8 public function events() 9 { 10 return [Controller::EVENT_BEFORE_ACTION => 'beforeType']; 11 } 12 13 /** 14 *

Yii 2.0 文件上传

先创建一个(UploadForm.php)模型层 <?phpnamespace app\models; use yii\base\Model;use yii\web\UploadedFile; /*** UploadForm is the model behind the upload form.*/class UploadForm extends Model{    /**    * @var UploadedFile file attribute    */    public $file;

关于Yii2中CSS,JS文件的引入心得

1.在view中 use yii\helpers\Html; <?=Html::jsFile('@web/***/js/***.js')?>//这里***代表你的目录名或者文件名 <?=Html::cssFile('@web/***/css/***.css')?>//***同上 2.布局 public $layout = 'layout';//在类中定义一个变量,名为$layout的php文件 <?php echo $content; ?> 3.跳转链接 use yii

Yii2.0 实现RESTful风格的简单API

一.创建数据库 首先,在mysql中创建一个名为yii2basic的数据库,并创建一张名为player的表. 二.配置 1.app/config/db.php <?php return [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=yii2basic', 'username' => 'root', 'password' => '', 'charset' => 'utf8',

yii2 RUL的生成

通常情况下,会使用 yii\helpers\Url 使用Url类来生成想要的URL地址(链接) Url提供的方法大都是静态方法,不需要对类进行实例化 1.1 最常用的 Url::to() 1.2. base方法 Url::base()方法生成当前网络的基础URL地址,默认为相对路径  base(true)为全部地址 1.3. canonical方法  返回当前URL的友好转码格式 1.4.1. home() 方法 Url::home() 方法返回网站的首页,也就是/index.php home(

jquery 配合 ajax 完成 在线编辑 你值得拥有

思路分析: 将 table中的表格 改变成为 input表格框获得值 ajax配合修改 删除 <?php use yii\helpers\Url; $web = Url::base(); ?> <style> .btn-save-data { display: none; } .btn-cancel-data { display: none; } </style> <script type="text/javascript"> //添加