30.URL 管理

Web应用程序完整的URL管理包括两个方面。首先, 当用户请求约定的URL,应用程序需要解析 
它变成可以理解的参数。第二,应用程序需求提供一种创造URL的方法,以便创建的URL应用程序可以理解的。
对于Yii应用程序,这些通过[CUrlManager]辅助完成。

Creating URLs(创建网址)

虽然URL可被硬编码在控制器的视图(view)文件,但往往可以很灵活地动态创建它们:

[php]
$url=$this->createUrl($route,$params);

$this指的是控制器实例; $route指定请求的route 的要求;$params 列出了附加在网址中的GET参数。

默认情况下,URL以get格式使用[createUrl|CController::createUrl] 创建。例如,
提供$route='post/read'$params=array('id'=>100) ,我们将获得以下网址:
/index.php?r=post/read&id=100

参数以一系列Name=Value通过符号串联起来出现在请求字符串,r参数指的是请求的route 。
这种URL格式用户友好性不是很好,因为它需要一些非字字符。

我们可以使上述网址看起来更简洁,更不言自明,通过采用所谓的'path格式,省去查询字符串和把GET参数加到路径信息,
作为网址的一部分:
/index.php/post/read/id/100

要更改URL格式,我们应该配置[urlManager|CWebApplication::urlManager]应用元件,
以便[createUrl|CController::createUrl]可以自动切换到新格式和应用程序可以正确理解新的网址:

[php]
array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
            'urlFormat'=>'path',
        ),
    ),
);
请注意,我们不需要指定的[urlManager|CWebApplication::urlManager]元件的类,
因为它在[CWebApplication]预声明为[CUrlManager]。

提示:此网址通过[createUrl|CController::createUrl]方法所产生的是一个相对地址。
为了得到一个绝对的URL ,我们可以用前缀Yii::app()->request->hostInfo; ,或调用[createAbsoluteUrl|CController::createAbsoluteUrl] 

User-friendly URLs(用户友好的URL)

当用path格式URL,我们可以指定某些URL规则使我们的网址更用户友好性。例如,我们可以产生一个短短的URL/post/100 ,
而不是冗长/index.php/post/read/id/100。网址创建和解析都是通过[CUrlManager]指定网址规则。

要指定的URL规则,我们必须设定[urlManager|CWebApplication::urlManager] 应用元件的
属性[rules|CUrlManager::rules]:

[php]
array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                'pattern1'=>'route1',
                'pattern2'=>'route2',
                'pattern3'=>'route3',
            ),
        ),
    ),
);

这些规则以一系列的路线格式对数组指定,每对对应于一个单一的规则。路线(route)的格式必须是有效的正则表达式,
没有分隔符和修饰语。它是用于匹配网址的路径信息部分。还有route应指向一个有效的路线控制器。

规则可以绑定少量的GET参数。这些出现在规则格式的GET参数,以一种特殊令牌格式表现如下:
[php]
'pattern1'=>array('route1', 'urlSuffix'=>'.xml', 'caseSensitive'=>false)

这里写图片描述
这里写图片描述

[php]
array(
    'posts'=>'post/list',
    'post/<id:\d+>'=>'post/read',
    'post/<year:\d{4}>/<title>'=>'post/read',
)

这里写图片描述


隐藏 index.php
这里写图片描述

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

这里写图片描述


Faking URL Suffix(伪造URL后缀)
这里写图片描述


使用自定义URL规则设置类
这里写图片描述

[php]
array(
    // 一个标准的URL规则,将 '/' 对应到 'site/index'
    '' => 'site/index',

    // 一个标准的URL规则,将 '/login' 对应到 'site/login', 等等
    '<action:(login|logout|about)>' => 'site/<action>',

    // 一个自定义URL规则,用来处理 '/Manufacturer/Model'
    array(
        'class' => 'application.components.CarUrlRule',
        'connectionID' => 'db',
    ),

    // 一个标准的URL规则,用来处理 'post/update' 等
    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
从以上可以看到,我们自定义了一个URL规则类CarUrlRule来处理类似/Manufacturer/Model这样的URL规则。 这个类可以这么写:

[php]
class CarUrlRule extends CBaseUrlRule
{
    public $connectionID = 'db';

    public function createUrl($manager,$route,$params,$ampersand)
    {
        if ($route==='car/index')
        {
            if (isset($params['manufacturer'], $params['model']))
                return $params['manufacturer'] . '/' . $params['model'];
            else if (isset($params['manufacturer']))
                return $params['manufacturer'];
        }
        return false;  // this rule does not apply
    }

    public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
    {
        if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches))
        {
            // check $matches[1] and $matches[3] to see
            // if they match a manufacturer and a model in the database
            // If so, set $_GET['manufacturer'] and/or $_GET['model']
            // and return 'car/index'
        }
        return false;  // this rule does not apply
    }
}

这里写图片描述


这里写图片描述


YII模块绑定二级域名方法

在配置文件设置
'urlManager' => array(  
'urlFormat' => 'path',  
'showScriptName' => false, //注意false不要用引号括上  
'urlSuffix' => '.html',  
'rules' => array(  
'http://blog.zeeeda.com'=>array('/blog', 'urlSuffix'=>'', 'caseSensitive'=>false),  
'http://blog.zeeeda.com/comment-<id:\w+>'=>array('/blog/comment/', 'urlSuffix'=>'.html', 'caseSensitive'=>false),//blog 为一个模块 ,如果在blog模块下还存在第二个控制器(这里以comment为例),则需要多写一个规则  
),  

Yii的常用URL

当前页面url  Yii::app()->request->url;
跳转前一个页面url $this->redirect(Yii::app()->request->urlReferrer);
根目录URL Yii::app()->baseUrl 或 Yii::app()->request->baseUrl;
自定义URL $this->createUrl('post/read',array('id'=>100))Yii::app()->createUrl();

如果浏览器重定位到登录页面,而且登录成功,我们将重定位浏览器到引起验证失败的页面。我们怎么知道这个值呢?我们可以通过用户部件的returnUrl 属性获得。我们因此可以用如下执行重定向:
Yii::app()->request->redirect(Yii::app()->user->returnUrl);
当前域名  Yii::app()->request->hostInfo;
除域名外的URL  Yii::app()->request->getUrl();
除域名外的首页地址  Yii::app()->user->returnUrl;
除域名外的根目录地址  Yii::app()->homeUrl;


Yii获取IP地址:Yii::app()->request->userHostAddress;
Yii判断提交方式:Yii::app()->request-isPostRequest;
proteced目录的物理路径:Yii::app()->basePath;
获取上一页的url以返回:Yii::app()->request->urlReferrer;
获取当前控制器ID:Yii::app()->getController()->getAction()->id;
项目路径:dirname(Yii::app()->BasePath);
Yii获取ip地址:Yii::app()->request->userHostAddress;
Yii获取get,post过来的数据:Yii::app()->request->getParam('id');


Yii如何设置时区:
可以在config/main.php里'timeZone'=>'Asia/Chongqing',设定时区

Yii如何将表单验证提示弄成中文:
将main.php 里的app配置加上language=>'zh_cn',系统默认的提示就是中文的。

防止重复提交:Ccontroler->refresh();

http://www.yiichina.com/doc/guide/1.1/topics.url

http://blog.csdn.net/xinqingch/article/details/8186716

http://www.cnblogs.com/zhengyanbin2016/p/5392232.html

http://zhangxugg-163-com.iteye.com/blog/1675093

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值