The controller contains the functionality of a page or snippet. Pimcore offers an abstract class (Pimcore_Controller_Action_Frontend), which must be implemented by your controllers. The naming of the file and the class is the same as in Zend Framework. Because “website” is configured as the default module in the front controller, you don't have to add a prefix to your controller classnames.
Examples
| Controllername | Filename |
Classname |
|---|---|---|
| content |
ContentController.php |
ContentController |
| news |
NewsController.php |
NewsController |
Put your controllers in the following directory: /website/controllers
There are some helpers defined in Pimcore_Controller_Action_Frontend (the abstract class your controller must extend). But the best way is to use the Website_Controller_Action (/website/lib/Website/Controller/Action.php) which is already shipped with Pimcore and implements the Pimcore_Controller_Action_Frontend and can be modified and extended the way you need.
You can use:
| Method |
Arguments |
Description |
|---|---|---|
| enableLayout |
none |
Register Zend_Layout for you, if called you can use this layout in your views. |
| disableLayout |
none |
The opposite of enableLayout. |
| disableViewAutoRender |
none |
Disables the auto renderer for the current action, for actions which don't need a view. |
| removeViewRenderer |
none |
Removes the view renderer. This is not only for the current action, once the renderer is removed you can't render a view anymore, no matter which scope. |
| forceRender |
none |
Call this method to force rendering, this can be useful when you need the response directly. |
| setDocument |
Document $document |
With this method you can set the default document for the current action which will be available in the view and the action with $this->document. |
If you want to use one of the methods (hooks) below which are offered by ZF you have to call their parent:
- preDispatch
- postDispatch
- init
There are also some properties which can be useful:
| Name |
Type |
Description |
|---|---|---|
| document |
Document |
Reference to the current document |
| editmode |
boolean |
True if you are in editmode (admin) |
Example
... public function init() { parent::init(); // example of properties if ($this->document instanceof Document_Page) { // do something } ... // your custom code ... } public function preDispatch() { parent::preDispatch(); if ($this->editmode) { // do something only in editmode } ... // your custom code ... } ...
Comments (3)
Apr 14, 2011
jason fonseca says:
Is there a tutorial I can follow to create a controller? I'm having trouble. Why...Is there a tutorial I can follow to create a controller? I'm having trouble. Why doesn't this work:
class JasonsController extends Website_Controller_Action {
public function jasonsAction () {
}
}
class JasonsController extends Website_Controller_Action { public function jasonsAction () { }
}
Apr 15, 2011
Thomas Akkermans says:
What doesn't work?What doesn't work?
Apr 16, 2011
jason fonseca says:
I solved it, The problem was that I had to define a static route in order to all...I solved it, The problem was that I had to define a static route in order to allow pimcore to route to it.