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?

有帮助吗?

解决方案

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.

其他提示

Simply just don't load the plugin with routes and use your own in app/Config/routes.php

CakePlugin::load('UserManagement', array('bootstrap' =>  true, 'routes' => false);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top