问题
I'm running into a problem related to caching, plugins and duplicate model names in Cake 2.0. My application has several controllers containing only actions for public use (view, index and equivalents). The data is managed by a CMS which is added as a plugin, some names of the plugin's controller are the same.
For example, I have a PostsController in my application and a PostsController for the plugin. The plugin controller extends PluginAppController
and the public controller extends AppController
as per the manual. As soon as caching is kicked in (by setting debug
to 0
) the problems start. Cake tries to access a non-existing add
action in the controller which extends AppController
and the public application tries to access methods from the PluginAppController
.
I don't understand why Cake would do this and it creates all kinds of errors (blank pages, lost sessions) which aren't logged properly as well. Everything was working well while the app still ran on Cake 1.3 and also in 2.0 production mode.
The file cake_core_file_map
in the /tmp/cache/persistent/
directory seems to be causing the issue. As soon as I remove this and reload either of the views, it renders correctly. So the procedure is as follows:
- Load
http://www.example.com/admin/posts
successfully; - Load
http://www.example.com/posts
(it fails to render); - Clear the cache (or just cake_core_file_map);
- Load
http://www.example.com/posts
successfully; - Load
http://www.example.com/admin/posts
(which now fails to load properly).
My guess is Cake fails to save the correct references to the plugin and main application paths in cake_core_file_map
, but I have no clue how to force Cake to behave nicely in that regard.
Does anybody know how to stop Cake from confusing the plugin's controllers with the other ones with duplicate names?
EDIT
This issue might be related to a bug in Cake, as this report mentions similar problems and cake_core_file_map
as well. The fix mentioned here doesn't work unfortunately.
EDIT 2
There is indeed some custom routing going on, which was working normally in Cake 1.3. This is from routes.php
:
Router::connect('/plugin_name', array('plugin' => 'plugin_name', 'controller' => 'users', 'action' => 'login'));
Router::connect('/admin/*', array('plugin' => 'plugin_name', 'controller' => 'posts', 'action' => 'index'));
回答1:
May be it would be helpfull to override Cake' standard cache file names for Plugin to make Cache engine keep it separately. In main bootstrap file while loading plugin:
CakePlugin::loadAll(array('Plugin' => array('bootstrap' => true));
In Plugin direcrory /app/Plugin/Plugin/Config/bootstrap.php
<?php
Cache::config('_cake_core_', array(
'engine' => 'File',
'prefix' => 'cake_core_plugin_',
'path' => CACHE . 'persistent' . DS,
'serialize' => true,
'duration' => '+999 days',
));
?>
回答2:
Are there conflicting routes between the main app and the plugin? This sounds like you may need to create a route for your /posts and another for /admin/posts in your main app. This should override the routes from the plugin causing any conflict. Of course, clear your cache before trying the change.
//main app posts route
Router::connect(
'/posts',
array(
'controller' => 'Posts'
'action' => 'index'
)
);
//plugin posts route
Router::connect(
'/admin/posts',
array(
'controller' => 'Posts'
'action' => 'index',
'plugin' => 'CmsPlugin'
)
);
回答3:
This turns out to be a known issue (which doesn't make it less annoying) and will only be fixed in a next major release, as per this Cake bug report.
PHP 5.2 doesn't have namespace support which is apparently needed for Cake to support duplicate classnames. PHP 5.3 has namespace support and Cake 3.0 will require that version.
来源:https://stackoverflow.com/questions/8647229/caching-plugin-and-normal-controllers-with-duplicate-names