-- Marko Korhonen <[email protected]> wrote
(on Monday, 16 February 2009, 05:04 AM -0800):
> I'm pretty sure that others have done the same as me:
>
> Base Controllers like My_BaseController and used as follows:
> Module_SomeController extends MyLibrary_Controller_Base
>
> Well, if so, you probably have done some methods just to shorten things a
> bit.
>
> I'll share some of my "convienience" methods and I hope you got some
> ideas/feedback/bugs noticed and maybe some great convienience methods to
> share...
>
> I still would like to get Module Base Controller, but it does not autoload
> itself =(
> Module_SomeController extends Module_BaseController
This is what action helpers were designed for -- to allow for common
functionality between action controllers, and also for distributing
functionality to use in different projects. If I may, I'd recommend
rewriting these as action helpers.
One change I'm going to add before 1.8 is the addition of a
"getHelperBroker()" method to both the action controller and abstract
action helper (this latter will proxy to the action controller). This
will allow your action helpers to have access to other action helpers
trivially. For example:
$this->getHelperBroker()->json($data);
This should help with re-use, and further eliminate reasons for "base"
controllers in your projects.
> -------------------------------------------------------------------
>
> // output json without rendering any layouts/templates
> protected function json($data)
> {
> $this->_helper->json($data);
> }
>
> // Is the current action frontpage
> protected function isFrontpage()
> {
> $front = $this->getFrontController();
>
> return (boolean) $this->_module == $front->getDefaultModule() &&
> $this->_controller == $front->getDefaultControllerName() && $this->_action
> == $front->getDefaultAction();
> }
>
> // Disable layout and/or viewRenderer
> protected function disableUI($layout = true, $viewRenderer = true)
> {
> if ($layout) $this->_helper->layout->disableLayout();
>
> if ($viewRenderer) $this->_helper->viewRenderer->setNoRender();
> }
>
> // Set layout
> protected function setLayout($layout)
> {
> $this->_helper->layout->setLayout($layout);
> }
>
> // Set template
> protected function setTemplate($template)
> {
> $this->_helper->viewRenderer($template);
> }
>
> // Change the response segment (default is being $this->layout()->content
> protected function setResponseSegment($segment)
> {
> $this->_helper->viewRenderer->setResponseSegment($segment);
> }
>
> // Set page title
> public function setTitle($title)
> {
> $this->view->headTitle($this->view->page_title);
> }
>
> // Get Url by default route
> public function url($module, $controller, $action, array $urlOptions =
> array(), $reset = false, $encode = true)
> {
> $urlOptions["module"] = $module;
> $urlOptions["controller"] = $controller;
> $urlOptions["action"] = $action;
>
> return $this->view->url($urlOptions, "default", $reset, $encode);
> }
>
> // Get Url by named route
> public function urlByName($name, array $urlOptions = array(), $reset =
> false, $encode = true)
> {
> return $this->view->url($urlOptions, $name, $reset, $encode);
> }
> --
> View this message in context:
> http://www.nabble.com/Base-Controller-convienience-methods-tp22036897p22036897.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
--
Matthew Weier O'Phinney
Software Architect | [email protected]
Zend Framework | http://framework.zend.com/