urlmanager组件
yii的官方文档对此的解释如下:
代码如下:
'urlsuffix'=>'/',
要更改url格式,我们应该配置urlmanager应用元件,以便createurl可以自动切换到新格式和应用程序可以正确理解新的网址:'urlmanager'=>array( 'urlformat'=>'path', 'showscriptname'=>false, 'urlsuffix'=>'.html', 'rules'=>array( 'posts'=>'post/list', 'post/'=>array('post/show','urlsuffix'=>'.html'), 'post//'=>array('post/view','urlsuffix'=>'.xml'), ),),
示例一
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
示例四
rule代码
代码如下:
'http://.vt.com/'=>array('/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/' => 'post/hello', name是postcontroller actionhello($name)的参数'post/' => 'post/view', domain/post/e文小写 其中:前面的alias是postcontroller actionview($alias)的参数'(posts|archive)/' => '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 directlyrewritecond %{request_filename} !-frewritecond %{request_filename} !-d# otherwise forward it to index.phprewriterule . index.php
3.开启rewrite
简单的说,在main.php中简单设置urlmanager,然后讲了3条规则,基本都覆盖到了。最后是隐藏index.php,请记住.htaccess位于index.php同级目录 ,而不是protected/目录。其他就简单了。
希望本文所述对大家基于yii框架的php程序设计有所帮助。