您的位置:首页 > 编程语言 > PHP开发

thinkphp 静态 伪静态 路由

2016-01-23 07:31 435 查看
目标:
(1)url转变 http://127.0.0.1/index.php/Index/index/id/2 转变成 http://127.0.0.1/a/2.shtml (2)生成静态缓存
---------------------------------------------------------------------

环境:
xp 2
apache 2.2
开启mod_rewrite
配置文件 AllowOverride All
php 5.2
mysql 5.0
---------------------------------------------------------------------

说明:
APP_PATH:项目目录
THINK_PATH:框架目录
步骤:
(1)APP_PATH/conf/config.php
[code]
return array(

'HTML_CACHE_ON' => true, // 开启静态缓存

'URL_ROUTER_ON' => true, // 开启路由转换
);


(2)APP_PATH/conf/htmls.php
return array(

'Index:index' => array('Index/{id}'),
);


(3)APP_PATH/conf/routes.php
return array(

array('a','Index/index','id'),
);


(4)APP_PATH/.htaccess(跟入口文件index.php同一级目录)
(1)必须开启mod_rewrite模块(改变配置必须重启apache服务器)
(2)配置文件httpd.conf,该目录内设置 AllowOverride ALL(改变配置必须重启apache服务器)
(3)文件内容(无需重启apache)
[content]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
[/content]

(5)THINK_PATH/Common/convention.php
<?php
...
'URL_MODEL' => 2, // 开启rewrite模式
'URL_HTML_SUFFIX' => '.shtml', // 伪静态下的后缀名
...
?>

注:写在APP_PATH/Conf/config.php下貌似没起作用

(6)Action模块编写
APP_PATH/Lib/Action/IndexAction.class.php
<?php
class IndexAction extends Action{
public function index() {
$this->assign('id',$_GET['id']);
$this->display();
}
}
?>

访问地址: http://127.0.0.1/a/8888.shtml 静态文件: APP_PATH/Html/Index/8888.html生成
注意:生成静态文件后下次访问会直接访问静态html

(7)在模板里使用U方法,统一url格式
访问路由修改后的(http://127.0.0.1/a/111.shtml):<a href="{:U('a/111')}">test1</a><br />
访问不带路由的(http://127.0.0.1/Index/index/111.shtml):<a href="{:U('Index/index/111')}">test1</a><br />

注意:U方法的路由支持 仅支持简单路由,不支持泛路由和正则路由(来自官方手册,5.2.8 URL生成) [/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thinkphp 缓存