I'm writing a poll plugin for a websit开发者_StackOverflow中文版e based on CakePHP. The plugin works good if I access it from its own URL (eg. myapp.com/plugin/controller
) but I need to call it from different pages. I would like to include it as a widget in every page.
I'm looking for a method like $myplugin->renderPoll($pollId);
but I really didn't find any information about how to instantiate the Polls class. I tried with App::import
and ClassRegistry::init
with no luck.
Any suggestion ? Thank you
Looks like you are trying to create some sort of Helper
to create poll cross views? I would suggest creating a Helper
for that particular class. Simply create a helper in plugins/plugin_name/views/helpers/foo.php
, and in each controller (or in app_controller.php
) that you need it, include the helpers as $helpers = array("PluginName.Foo");
and inside your view, you should be able to use the methods defined in foo.php
by calling $foo->renderPoll($pollId)
.
//app/plugins/plugin_name/views/helpers/foo.php
class FooHelper extends AppHelper {
var $name = "Foo";
function renderPoll($id=0) {
//...
}
}
Use Elements! They're small blocks of presentation code that need to be repeated from page to page, sometimes in different places in the layout.
Check this link out: http://book.cakephp.org/view/1081/Elements
I guess this link explains everything you need.
精彩评论