yii2 - (仅php层)YAF如何兼容YII的【驼峰action变成减号】的url路由规则
世界只因有你
世界只因有你 2017-05-19 10:08:38
0
1
575

背景描述:
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层实现

世界只因有你
世界只因有你

répondre à tous(1)
習慣沉默

经过对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)));
}
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!