thinkphp 静态 伪静态 路由

 目标:
(1)url转变
         http://127.0.0.1/index.php/Index/index/id/2
         转变成
         http://127.0.0.1/a/2.shtml2)生成静态缓存
---------------------------------------------------------------------

环境:
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生成)    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值