Updated: Best practices for managing static content in the Zend Framework?

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?

+7
source share
2 answers

Your approach seems reasonable to me. However, perhaps you should use the __call method instead, which will allow you to more easily route your actions ...

Set up your route as follows:

 resources.router.routes.static-pages.route = /:action resources.router.routes.static-pages.defaults.module = default resources.router.routes.static-pages.defaults.controller = index 

And your controller will be like this:

 public function someAction() { //going to URL /some will now go into this method } public function __call($name, $args) { //all URLs which don't have a matching action method will go to this one } 
+1
source

I think you are on the right track, but here are some other ideas.

Partition your routing in INI: i.e. blog router, static page router, forum router, etc. (I think you already do this)

Use different classes of the router to handle routing in each section, and not to send to the controller.

Static http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.static

All: http://framework.zend.com/manual/en/zend.controller.router.html

Some links that may help:

  • codeutopia.net/blog/2007/11/16/routing-and-complex-urls-in-zend-framework/
  • www.vayanis.com/2009/03/20/intro-to-zend-framework-routing/
+1
source

All Articles