#1 chinakr
启用Apache的.htaccess
==========================
参考资料:Ubuntu下启动Apache对.htaccess文件的支持
$ sudo a2enmod
rewrite
$ gvim /etc/apache2/sites-enabled/000-default &
AllowOverride All
$ sudo service apache2 restart
SpeedPHP的.htaccess文件
============================
参考资料:Apache的UrlRewrite伪静态htaccess设置
$ cd /var/www/ root of SpeedPHP project
$ gvim .htaccess &
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
在SpeedPHP中启用URL Rewrite
===============================
参考资料:SpeedPHP框架的urlrewrite伪静态实现
$ gvim index.php &
$spConfig = array(
‘launch’ => array(
‘router_prefilter’ => array(
array(‘spUrlRewrite’, ‘setReWrite’),
),
‘function_url’ => array(
array(‘spUrlRewrite’, ‘getReWrite’),
),
),
);
测试URL Rewrite的效果
=========================
$ gvim index.php &
$spConfig = array(
‘auto_display’ => TRUE,
$ gvim controller/main.php &
function test_smarty() {
$this->name = ‘chinakr’;
$this->display(‘index.html’);
}
$ gvim tpl/index.html &
Hi, {$name}!Go to Home Page or stay here.
$ lynx localhost/index.php?c=main&a=test_smarty
$ lynx localhost/main-test_smarty.html
也即默认的URL Rewrite,得到的URL的形式是“controller-action.html”。
优化我们的URL
=================
参考资料:SpeedPHP框架的urlrewrite伪静态实现
看起来像HTML页面的形式
————————
$ gvim index.php &
$spConfig = array(
‘ext’ => array(
‘spUrlRewrite’ => array(
‘suffix’ => ”,
‘sep’ => ‘/’,
),
),
$ lynx localhost/index.php?c=main&a=test_smarty
$ lynx localhost/main/test_smarty.html
也就是说,现在的URL的形式是“controller/action.html”,目录层次和逻辑关系更加清晰。
更加简洁的形式
—————–
$ gvim index.php &
$spConfig = array(
‘ext’ => array(
‘spUrlRewrite’ => array(
‘suffix’ => NULL,
‘sep’ => ‘/’,
),
),
$ lynx localhost/index.php?c=main&a=test_smarty
$ lynx localhost/main/test_smarty
也就是说,现在的URL的形式是“controller/action”,更简洁了。
URL中的参数
===============
参考资料:SpeedPHP框架的urlrewrite伪静态实现
$ gvim tpl/index.html &
Sample for URL with arguments.$ lynx localhost/index.php?c=main&a=test_smarty&id=0&from=baidu
$ lynx localhost/main/test_smarty/id/0/from/baidu.html
也就是说,现在的URL的形式是“controller/action/参数1/值1/参数2/值2”,不太美观。
优化我们的URL参数
====================
参考资料:SpeedPHP框架的urlrewrite伪静态实现
$ gvim index.php &
$spConfig = array(
‘ext’ => array(
‘spUrlRewrite’ => array(
‘suffix’ => NULL,
‘sep’ => ‘/’,
‘map’ => array(
‘smarty’ => ‘main@test_smarty’,
),
‘args’ => array(
‘smarty’ => array(‘id’, ‘from’),
),
),
),
$ lynx localhost/index.php?c=main&a=test_smarty&id=0&from=baidu
$ lynx localhost/smarty/0/baidu
也可以这样:
$ gvim index.php &
$spConfig = array(
‘ext’ => array(
‘spUrlRewrite’ => array(
‘suffix’ => NULL,
‘sep’ => ‘/’,
‘map’ => array(
‘smarty’ => ‘main@test_smarty’,
),
‘args’ => array(
‘smarty’ => array(‘from’, ‘baidu’),
),
),
),
$ lynx localhost/index.php?c=main&a=test_smarty&id=0&from=baidu
$ lynx localhost/smarty/baidu/0
在这里,建议把重要的参数放在前面,不重要的放在后面,这样逻辑相对更清晰一些。
能否做得更好?
=================
事实上,如果SpeedPHP的URL能够支持这样的形式可能逻辑关系会更清晰一些:
/controller/action-参数1-值1-参数2-值2.html
或者
/controller/action-值1-值2.html
这样的话,上面的URL就可以写成:
http://localhost/main/test_smarty-id-0-from-baidu.html
或者
http://localhost/main/test_smarty-0-baidu.html
这样是不是会更清楚更好看一些呢?见仁见智,权当抛砖引玉了!
来源:chinakr的博客,http://blog.quickbest.net/archives/php/url-rewrite-speedphp.html
该贴已经同步到 chinakr的微博
2011-12-23 00:04:36