背景描述:
1.在yii中,有以下Controller
class PayController extends Controller
{
public function actionIosCallback()
{
echo 'hello yii';
}
}
访问www.XXX.com/pay/ios-callback,则页面显示hello yii
2.在yaf中,有以下Controller
class PayController extends Yaf_Controller_Abstract{
public function actionIosCallback()
{
echo 'hello yaf';
}
}
访问www.XXX.com/pay/iosCallback,则页面显示hello yaf
问题描述:
3.问yaf如何兼容yii,实现访问www.XXX.com/pay/ios-callback,则页面显示hello yaf
注:目前个人能想到的方案为在Nginx层改写url可以实现,但觉得不是最好方案,故仅讨论php层实现
習慣沉默2017-05-19 10:10:38
经过对YII源码的研究,终于找到了它改写路由的规则了,方式如下
str_replace(' ', '', ucwords(str_replace('-', ' ', $action)))
实现方法为将此规则引入到yaf的routerShutdown,即可改写路由规则,达到访问www.XXX.com/pay/ios-callback,则页面显示hello yaf的目的
public function routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
$request->controller = str_replace(' ', '', ucwords(str_replace('-', ' ', $request->controller)));
$request->action = str_replace(' ', '', ucwords(str_replace('-', ' ', $request->action)));
}