多APP下伪静态实现方法

#1 荆柯要打鱼

在以下的目录结构当中,实现:
网站APP:
1、首页访问地址:http://localhost/index/
2、main控制器下的page方法:http://localhost/index/main/page.html
3、带参数:http://localhost/index/topic/page/id/100.html
会员APP:
1、会员首页访问地址:http://localhost/member/
2、main控制器下的page方法:http://localhost/member/main/page.html
3、带参数:http://localhost/member/topic/page/id/100.html
其它APP,省略。。。。
如下图目录结构:


实现方法,经在本地测试是没有问题,如您在运行时遇到问题,请及时与我联系,大家一起沟通交流。
1、入口文件配置:
// 网站主体模块程序入口文件

// 载入配置与定义文件
require("config.php");

$spConfig['app_name'] = basename(__FILE__,".php");// 当前APP名称
//PATH_INFO路由配置
$spConfig['url'] = array( // URL设置
'url_path_info' => TRUE, // 是否使用path_info方式的URL
'url_path_base' => '', // URL的根目录访问地址,默认为空则是入口文件index.php
);
//伪静态配置
$spConfig['launch'] = array(
'router_prefilter' => array(
array('spUrlRewrite', 'setReWrite'), // 对路由进行挂靠,处理转向地址
),
'function_url' => array(
array("spUrlRewrite", "getReWrite"), // 对spUrl进行挂靠,让spUrl可以进行Url_ReWrite地址的生成
),
);
//Url_ReWrite的设置
$spConfig['ext'] = array(
'spUrlRewrite' => array(
'hide_default' => true, // 隐藏默认的main/index名称,但这前提是需要隐藏的默认动作是无GET参数的
'sep' => '/', // 网址参数分隔符,建议是“-_/”之一
'args_path_info' => true, // 地址参数是否使用path_info的方式,默认否
'suffix' => '.html', // 生成地址的结尾符
),
);
// 模板视图配置
$spConfig['view'] = array(
'enabled' => TRUE, // 开启视图
'config' =>array(
'template_dir' => APP_PATH.'/view', // 模板目录
'themes_dir' => 'themes', // 媒体文件目录名
'compile_dir' => APP_PATH.'/data/', // 编译目录
'cache_dir' => APP_PATH.'/data/', // 缓存目录
'left_delimiter' => '<{', // smarty左限定符
'right_delimiter' => '}>', // smarty右限定符
),
);

$spConfig['controller_dir'] = APP_PATH.'/modules';
// 当前模块附加的配置
$spConfig['controller_path'] = $spConfig['controller_dir'] . '/'.$spConfig['app_name'];

// 载入SpeedPHP框架
require(SP_PATH."/SpeedPHP.php");
spRun(); // SpeedPHP 3新特性

index.php此处的'url_path_base' => '',设置为空,其它APP入口为:'url_path_base' => '/member/member.php',这个地方会影响会setReWrite与getReWrite,也就是设置和获取伪静态的时候,会根据url_path_base来进行,如果换成会员APP,就把这里换成:'url_path_base' => /admin.php/admin,此处设置是为了能够进入默认的控制器和方法,有兴趣的朋友可以试着自己修改默认控制器和动作。

三、.htaccess配置


RewriteEngine On
RewriteBase /
RewriteRule ^admin/$ admin.php?$1 [L]
RewriteRule ^member/$ member.php?$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?$1 [L]


把这个文件放到网站根目录即可,要想让这个文件生效,需要在apache配置文件当中httpd.conf:一是开启伪静态
LoadModule rewrite_module modules/mod_rewrite.so
把前面的去掉;二是将AllowOverride设置为all。
重启apache,试试看。

2010-08-26 21:49:55

#2 jake

楼上对sp的设置挺有研究的。推荐!

但是,这里还是得补充纠正一下,
http://localhost/index.php/topic/page/id/100.html
带有index.php这样文件名模式的URL地址,是称作PATH_INFO,而不是URL_REWRITE(伪静态)。
另外,PATH_INFO的实现,主要是apache内允许使用PATH_INFO即可,无需像URL_REWRITE那样要htaccess规则等设置。

2010-08-27 08:57:27

#3 荆柯要打鱼

jake纠正的对,之前的概念有点模糊,/index.php是PATH_INFO的访问方式,伪静态应该是:/index   其它APP应该是:/member  /admin,以对上面代码进行了修正。

2010-08-28 12:52:26