目标:
(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, // 开启路由转换
);
[/code]
(2)APP_PATH/conf/htmls.php
[code]
return array(
'Index:index' => array('Index/{id}'),
);
[/code]
(3)APP_PATH/conf/routes.php
[code]
return array(
array('a','Index/index','id'),
);
[/code]
(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
[code]
<?php
...
'URL_MODEL' => 2, // 开启rewrite模式
'URL_HTML_SUFFIX' => '.shtml', // 伪静态下的后缀名
...
?>
[/code]
注:写在APP_PATH/Conf/config.php下貌似没起作用
(6)Action模块编写
APP_PATH/Lib/Action/IndexAction.class.php
[code]
<?php
class IndexAction extends Action{
public function index() {
$this->assign('id',$_GET['id']);
$this->display();
}
}
?>
[/code]
访问地址: http://127.0.0.1/a/8888.shtml
静态文件: APP_PATH/Html/Index/8888.html生成
注意:生成静态文件后下次访问会直接访问静态html
(7)在模板里使用U方法,统一url格式
[code]
访问路由修改后的(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 />
[/code]
注意:U方法的路由支持 仅支持简单路由,不支持泛路由和正则路由(来自官方手册,5.2.8 URL生成)
thinkphp 静态 伪静态 路由
最新推荐文章于 2022-03-10 11:09:20 发布