YII使用url组件美化管理的方法_PHP

php中文网
发布: 2016-05-28 11:49:57
原创
1218人浏览过

本文实例讲述了yii使用url组件美化管理的方法。分享给大家供大家参考,具体如下:

urlManager组件

yii的官方文档对此的解释如下:

代码如下:

'urlSuffix'=>'/',


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

'urlManager'=>array(
  'urlFormat'=>'path',
  'showScriptName'=>false,
  'urlSuffix'=>'.html',
  'rules'=>array(
    'posts'=>'post/list',
    'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),
    'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),
  ),
),

登录后复制

示例一

立即学习PHP免费学习笔记(深入)”;

Rule代码

代码如下:

'posts'=>'post/list',


Action代码

代码如下:

echo $this->createAbsoluteUrl('post/list');

输出

http://localhost/test/index.php/post

示例二

Rule代码

代码如下:

'post/'=>array('post/show','urlSuffix'=>'.html'),


Action代码

代码如下:

echo $this->createAbsoluteUrl('post/show',array('id'=>998, 'name'=>'123'));

输出

http://localhost/test/index.php/post/998.html?name=123

示例三

Rule代码:

代码如下:

'post//'=>array('post/view','urlSuffix'=>'.xml'),

Action代码

代码如下:

echo $this->createAbsoluteUrl('post/view',array('id'=>998, 'mid'=>'tody'));


输出

http://localhost/test/index.php/post/998/tody.xml

Vheer
Vheer

AI图像处理平台

Vheer 125
查看详情 Vheer

示例四

Rule代码

代码如下:

'http://.vt.com/<_c:>'=>array('<_c>/host','urlSuffix'=>'.me'),

Action代码:

echo $this->createAbsoluteUrl('look/host',array('user'=>'boy','mid'=>'ny-01'));
echo '';
echo $this->createAbsoluteUrl('looks/host',array('user'=>'boy','mid'=>'ny-01'));

登录后复制

输出

http://boy.vt.com/look.me?mid=ny-01
http://localhost/test/index.php/looks/host/user/boy/mid/ny-01

1)controller/Update/id/23

public function actionUpdate(){
  $id = Yii::app()->request->getQuery('id') ; 经过处理的$_GET['id']
}
//$id = Yii::app()->request->getPost('id'); 经过处理的$_POST['id']
//$id = Yii::app()->request->getParam('id'); //CHttpRequest更多

登录后复制

2)public function actionUpdate($id)  这种不支持多主键,会检查一下到底GET里面有没有id,没有id就直接不允许访问

'sayhello/<name>' => 'post/hello', name是PostController actionHello($name)的参数
'post/<alias:[-a-z]+>' => 'post/view',  domain/post/e文小写 其中:前面的alias是PostController actionView($alias)的参数
'(posts|archive)/<order:(DESC|ASC)>' => 'post/index', domain/posts/DESC或domain/posts/ASC
'(posts|archive)' => 'post/index', domain/posts或domain/archive
'tos' => array('website/page', 'defaultParams' => array('alias' =>'terms_of_service')),

登录后复制

When the URL is /tos, pass terms_of_service as the alias parameter value.

隐藏 index.php

还有一点,我们可以做进一步清理我们的网址,即在URL中藏匿index.php  入口脚本。这就要求我们配置Web服务器,以及urlManager应用程序元件。

1.add showScriptName=>false

2.add project/.htaccess

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

登录后复制

3.开启rewrite

简单的说,在main.php中简单设置urlManager,然后讲了3条规则,基本都覆盖到了。最后是隐藏index.php,请记住.htaccess位于index.php同级目录 ,而不是protected/目录。其他就简单了。

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

相关标签:
yii
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号