doufutao4428 2013-09-15 23:34
浏览 27
已采纳

将CakePHP路由从插件替换为另一条路由

I am using a CakePHP plugin for user management, which specifies a route for an Access Denied page.

Router::connect('/accessDenied', array('plugin' => 'usermgmt', 'controller' => 'users', 'action' => 'accessDenied'));

In my main Cake app, I'd like to 'override' this route to use my own controller.

I don't want to modify the plugin...as it will cause future code maintenance when the plugin is updated.

Is there a way I can replace a plugin's route with my own?

  • 写回答

2条回答 默认 最新

  • donglaoping9702 2013-09-16 05:22
    关注

    Try Router::promote():

    Router::promote()
    Promote a route (by default, the last one added) to the beginning of the list

    If I understood your question correctly, you wanted to overwrite just one route. In your app/Config/routes.php, add the overridden route and a promote after CakePlugin::routes();

    //.... your routes....
    
    //Here the plugin routes being loaded
    CakePlugin::routes();
    
    //Overwrite route:
    Router::connect('/accessDenied', array('plugin' => 'usermgmt', 'controller' => 'users', 'action' => 'accessDenied'));
    Router::promote(); //and promote it
    

    That should do the trick. Promote does nothing but move the last route to top. In CakePHP, routing works as first-come-first-served basis (if you check the source closely it's an array), so promoting will move your last defined route to top and hence overwrite the route defined in the plugin.

    Edit

    If you do not like promoting, you can also define the route before CakePlugin::routes(). That should do the trick as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化