I have some questions regarding the Zend Framework. I am trying to route all static pages through the default controller, now using the default displayAction()
method. The intention is to process the displayAction()
request by displayAction()
at the page
parameter to determine if the script page exists if it renders the view, otherwise throw a 404 page error. In addition, a check is performed to check if a method with the same name as the parameter exists, if so, call this action.
Listed here are the routing settings from application.ini
resources.router.routes.static-pages.route = /:page resources.router.routes.static-pages.defaults.module = default resources.router.routes.static-pages.defaults.controller = index resources.router.routes.static-pages.defaults.action = display
Here are the controller actions:
public function someAction() { // do something } public function displayAction() { // extract page param, eg 'some' $page = $this->getRequest()->getParam('page'); // create zend styled action method, eg 'someAction' $page_action = $page.'Action'; // if a method within this controller exists, call on it if (method_exists($this, $page_action)) { $this->$page_action(); } // if nothing was passed in page param, set to 'home' if (empty($page)) { $page = 'home'; } // if script exists, render, otherwise, throw exception. if (file_exists($this->view->getScriptPath(null)."/".$this->getRequest()->getControllerName()."/$page.".$this->viewSuffix)) { $this->render($page); } else { throw new Zend_Controller_Action_Exception('Page not found', 404); } }
Now here are my questions: is there a better way to do this? I am relatively new to this structure, so are there any best practices that apply? Is there a better way to trigger an action from a controller? I did a lot while looking at the documentation, however, this is a bit contrary to myself.
Update 1:
After I thought and read, I was able to simplify the solution and include several things in it. NOTE. I use PagesController
as my default static content controller.
The routing settings from application.ini are listed here. For calls on the home page, that is, "/", I pass "home" as the action
parameter, for all other requests, the user defined / url link parameter is sent to action
.
resources.router.routes.home.route = "/" resources.router.routes.home.defaults.module = "default" resources.router.routes.home.defaults.controller = "pages" resources.router.routes.home.defaults.action = "home" resources.router.routes.pages.route = "/:action" resources.router.routes.pages.defaults.module = "default" resources.router.routes.pages.defaults.controller = "pages"
Here are the controller actions. If the user defines the parameter as an action, it will be called, otherwise it will fall into the php __call magic function.
public function someAction() { // Do something } public function __call($method, $args) { // extract action param, eg "home" $page = $title = $this->getRequest()->getParam('action'); // test if script exists if (file_exists($this->view->getScriptPath(null) . "/" . $this->getRequest()->getControllerName() . "/$page . " . $this->viewSuffix)) { // pass title to layout $this->view->assign(compact('title')); // render script $this->render($page); } else { throw new Zend_Controller_Action_Exception('Page not found', 404); } }
It works. So, here are my questions: would you consider standardizing the use of this method for managing static content? If not, why not? How would you improve it? Also, given that this is a GET request, would it be wise to switch to using Zend_Filter_input to clear input, or is it just brute force?