Choosing Zend Framework URL Parameters

I am building my first Zend Framework application, and I want to know how best to select user parameters from a URL.

I have some controllers that have index , add , edit and delete methods. The index action can take the page parameter, and the edit and delete actions can take the id parameter.

 Examples http://example.com/somecontroller/index/page/1 http://example.com/someController/edit/id/1 http://example.com/otherController/delete/id/1 

So far, I have used these parameters in action methods like this:

 class somecontroller extends Zend_Controller_Action { public function indexAction() { $page = $this->getRequest->getParam('page'); } } 

However, a colleague told me about a more elegant solution using Zend_Controller_Router_Rewrite as follows:

 $router = Zend_Controller_Front::getInstance()->getRouter(); $route = new Zend_Controller_Router_Route( 'somecontroller/index/:page', array( 'controller' => 'somecontroller', 'action' => 'index' ), array( 'page' => '\d+' ) ); $router->addRoute($route); 

This would mean that for each controller I would need to add at least three routes:

  • one for the "index" action with parameter: page
  • one for the change action with parameter: id
  • one for the delete action with parameter: id

See the code below. These are routes for only 3 basic methods of action of one controller, imagine that you have 10 or more controllers ... I can’t imagine that this would be the best solution. The only thing I see is that the parameter keys are named and therefore can be omitted from the URL ( somecontroller/index/page/1 becomes somecontroller/index/1 )

 // Route for somecontroller::indexAction() $route = new Zend_Controller_Router_Route( 'somecontroller/index/:page', array( 'controller' => 'somecontroller', 'action' => 'index' ), array( 'page' => '\d+' ) ); $router->addRoute($route); // Route for somecontroller::editAction() $route = new Zend_Controller_Router_Route( 'somecontroller/edit/:id', array( 'controller' => 'somecontroller', 'action' => 'edit' ), array( 'id' => '\d+' ) $router->addRoute($route); // Route for somecontroller::deleteAction() $route = new Zend_Controller_Router_Route( 'somecontroller/delete/:id', array( 'controller' => 'somecontroller', 'action' => 'delete' ), array( 'id' => '\d+' ) $router->addRoute($route); 
+4
source share
3 answers

I usually look at it like this:

  • Define processing requirements.

    What does each "action" need? The edit action and the delete action probably require: id param. The add action and the list action are probably not. These controllers / actions then consume the parameters and execute the processing.

    Note. You can write these comtrollers / actions without linking to the URLs that bring visitors. Actions simply expect their parameters to be passed to them.

  • Decide (!) Which URL you want.

    In general, I find that part of the text (/:module/):controller/:action basically works fine (except for relatively static top-level pages like /about , where I often put actions in IndexController (or StaticController) and resent to include the /index prefix in the URL.

    So, for message processing you may need the URL:

    • /post - list all posts, possibly with some swap
    • /post/:id - display a specific message
    • /post/:id/edit - edit a specific post
    • /post/:id/delete - delete a specific message
    • /post/add - add a message

    In addition, you can:

    • /post/list - a list of all messages, possibly with some paging
    • /post/display/:id - display a specific message
    • /post/edit/:id - edit a specific post
    • /post/delete/:id - delete a specific message
    • /post/add - add a message

    Or any other URL scheme. The fact is that you decide the URL you want to open.

  • Create routes ...

    ... that display these URLs for controllers / actions. [And make sure that whenever you render them, you use a url() view helper with the name of the route, so changing the route does not require changes in your downstream code in your actions or views.

Do you end up writing more routes? Yes, I think I know. But for me, the advantage is that I get the solution at my urls. I am not stuck in the default Zend settings.

But, like most things, YMMV.

+2
source

It all depends on your exact requirements. If you just want to pass one or two parameters, the first method will be the easiest. It is impractical to determine the route for each action. Several scenarios in which you want to define routes are as follows:

  • Long URLs. If the list of parameters for a specific action is very long, you may need to determine the route so that you can omit the keys from the request and, therefore, shorten the URL.
  • Fancy URLs. If you want to deviate from the usual Zend Framework url controller / action pattern and define a different url pattern for your application (for example, ends with ".html")
  • Friendly Slugs / SEO URLs

To take an example blog, you can define routes for blog posts so that the URL is optimized for SEO. At the same time, you might want to save the URL for editing / deleting / posting comments, etc., to stay in ZF by default and use $ this-> getRequest-> getParam () to access the request parameters in this context.

To summarize, the elegant solution will be a combination of default routes and url patterns.

+1
source

In a previous answer, @ janenz00 mentioned "long URLs" as one of the reasons for using routes:

Long URLs. If the list of parameters for a specific action is very long, you may need to determine the route so that you can omit the keys from the request and, therefore, shorten the URL.

Say we have an employee controller with an index action that shows a table of employees with some additional data (such as age, department ...) for each employee. The index action can take the following parameters:

  • a page parameter (required)
  • a sortby parameter (optional) that accepts a single column name for sorting (e.g. age)
  • a dept parameter (optional), which accepts the name of the department and shows only employees working in this department

Add the following route. Please note that when using this route, we cannot specify the dept parameter without first specifying the sortby parameter.

 $route = new Zend_Controller_Router_Route( 'employee/index/:page/:sortby/:dept', array( 'controller' => 'employee', 'action' => 'index') ); 

If we selected these parameters in our action methods, we could have avoided this problem (since the parameter keys are specified in the URL):

 http://example.com/employee/index/page/1/dept/staff 

Perhaps I am considering this incorrectly (or perhaps I do not see the full potential of routing), but for me the only reasons for using routes are:

  • If your URLs do not match the traditional pattern /module/controller/action
  • If you want your URLs to be more SEO optimized

If your only reason for using routes is to use named parameters, then I think it's best to use these parameters in your action methods for two reasons:

  • Keeping the number of routes at a minimum will reduce the amount of time and resources spent by the router.
  • Passing parameter keys in a URL allows us to use more complex URLs with optional parameters.

Any thoughts or advice on this topic are more than welcome!

0
source

Source: https://habr.com/ru/post/1446115/


All Articles