Optimizing Zend framework routes
Asked Answered
S

3

2

I'm using a traditional Zend framework application generated using Zend tool. I have several modules in my application all generated using Zend tool too. I'm intending to use the default routes in Zend framework in my application.

For example: www.host.com/module/controller/action/param1/value1/param2/value2

Here are my questions:

  1. Performance wise, using the default routes is the fastest solution, right?
  2. Since I won't ever change the routes, is there a way to make the routing process even faster? caching for example? skipping the routing process entirely? or any other technique?
  3. What is the pros of making custom routes? is it better for SEO?
  4. Can I skip the routing process for AJAX calls? (I know the answer is mostly NO, but maybe I can optimize my call further for AJAX calls)

Note:

I understand the routing process itself and how it works in Zend framework. However, since I won't be using most of the features available I thought maybe it's time for fine tuning :)

Thanks in advance.

Simarouba answered 9/4, 2012 at 8:17 Comment(8)
You answered your questions by yourself after each of them ;) I recommend you this article (a little bit old though) for your last question.Conah
@Conah I know that most of the question are a bit redundant, but my main concern is actually performance optimization :) and thanks for the article ;)Simarouba
i feel you already know the answer :DLunn
@Lunn hahaha :D yes I kinda have a habit for micro optimization :P If um not using it then blow it ;)Simarouba
@Simarouba .. not good with writing to many stories .. if it was a pure code question i'll be killing it now :DLunn
@Lunn :))) can't work around this, can u?! :)))Simarouba
Yes i can work around routes even remove them i add my own ... but am not sure Performance Wise , Skip some process with htaccess , not sure of the pros of customization ... Conclusion it not something that can be done with few lines ..Lunn
@Lunn Yea I know and it seems that it isn't worth the effort. Most of the work I did before was mainly around building ERPs and web applications, so SEO wasn't a concern and I never gave routes any attention. However, it seems more people are asking about customizing routes, so I was hoping if anyone could explain the whole point of doing it and if it is really worth it for smaller projects. :DSimarouba
S
3

1 . Performance wise, using the default routes is the fastest solution, right?

Right, but not using any routing system is the fastest solution (obviously).

2 . Since I won't ever change the routes, is there a way to make the routing process even faster? caching for example? skipping the routing process entirely? or any other technique?

Well, that's the thing, routes are mostly used for flexibility, internationalization, SEO.

The problem with the Zend Framework Router is that it depends and wraps the Front Controller instance, therefore it is hard (well impossible afaik) to cache the Router since the FC itself wraps others things like PDO instances which are not cachable.

So all routes are computed again and again for every requests.

A possible solution when using complex routes while avoiding route computation is to dump all routes to native Apache Rewrite Rules, it will be blazing fast compared to ZF, the main problem with this solution is that you need to manually compute reverse routes and whenever you make a change to a route, you'll need to edit it by hand.

As often, it is scalability vs performance.

3 . What is the pros of making custom routes? is it better for SEO?

Well, it "depends" (tm):

/list?type=products vs /products, the second route wins.

However, /products/page/1 vs /products?page=1 is a win-win (well it actually does not but it is another story).

Other pros:

  • i18n
  • seo
  • usability
  • meaningful url

4 . Can I skip the routing process for AJAX calls? (I know the answer is mostly NO, but maybe I can optimize my call further for AJAX calls)

I don't see any cons against this. I often go for it, except for RESTfull API.

Sukkah answered 10/5, 2012 at 13:6 Comment(5)
+1 FINALLY a complete answer :D now for some more questions ;) concerning point 2 dump all routes to native Apache Rewrite Rules: 1) Do you have a link to a tutorial or more information about how to do that? I have little knowledge when it comes to working with Apache and rewriting rules. 2) I have a feeling that using the rewrite rules means that I'll have to stop using Zend_Application in my project, right? I think this is mandatory to skip the router entirely. I read many times people saying that Zend_Application should be avoided for performance issues.Simarouba
1) Well it is mostly about regex, the best place imo is to dive into the apache manual. 2) True and false, if you don't use any route, the router will be instanciated but no routes will be computed or looped through. So it may be an improvement but I'm not sure it'll be worth to do it so.Audrit
I see. It's what you said before Scalability vs Performance. Cutting the router out of the picture means performance gain, but on the other hand may cause a lot of refactoring if other components in the framework itself depend on it. I think I should build the application normally then profile performance and test if removing the router will have a significant gain or not.Simarouba
@Songo, exactly. Don't do pre-optimization (evil (tm)), profile and try to find the quicker improvement vs performance vs scalability, I've some Zf+Doctrine2 application which handles almost 5.000.000 of requests/day with basic optimization and profiling. The most notable performance optimization I've ever seen is autoloading, opcode AND merging core class together to limit I/OAudrit
I'll definitely keep your advice in my mind when it's time to optimize ;) Again Thank you very much for giving me a satisfying answer :)Simarouba
S
1

Do you profiled the performance of the routing component of ZF or is it a assumption that this is the "bottleneck" of your application?!

In my opinion, if you need that low-latency, optimizing the page speed by disabling the most comfortable features isn't the best attempt to get the target. May you should starting to optimize your SQL querys (slowlog is the keyword here) or other things (xdebug can really help you profiling your application and to discover bottlenecks).

It's all about timing...

What about caching before the zend framework kicks in? What about caching and delivering before your interpreter (php) kicks in? Sounds good? You may want to use proxys like Varnish to realize a good low-latency responsive and to do something like that.

And even if you get a cache-miss from your proxy, you want to use a PHP opcode cache (like APC, XCache, ...). For your ask how to speed-up and optimizing your zend framework, you want to read the Zend Performance Guide.

Siena answered 26/4, 2012 at 17:16 Comment(4)
Thanks for your reply. I agree with you that optimizing the SQL queries and caching will be a better solution for speeding my website up. However, My main concern is to understand the significance of using the route module in Zend framework. I don't actually understand my people create custom routes and in my case I won't ever create any custom routes, so I thought maybe I can get it out of the picture since I'll always use the default routing.Simarouba
I believe that the default routes used in zend can cover many bases for most cases. I think that all the routing options might be one of the cases of feature bloat BUT I can also see that the routes would be useful if trying to retrofit some existing framework with Zend framework functionality.Walkling
Keep in mind, does other components of the framework may use the router/routes for internal use. For example Zend_View_Helper_Url is using the router to create a url (which is in my opinion very useful). If you change internal use of the routing (maybe disallow zf to load the router-resource) you may get unexpected behaviour of other components.Siena
@Siena good point indeed. It seems cutting the router out of the picture might cause hidden problems.Simarouba
M
0

You have two ways either extend Zend_Front_Controller and override its dispatch method and set your own front , or do the changes in zend code itself .

Open

Zend/Controller/Front.php

inside dispatch() method

find and remove following code

 $router = $this->getRouter();
    $router->setParams($this->getParams());

and

 $this->_plugins->routeStartup($this->_request);



      try {
            $router->route($this->_request);
        }  catch (Exception $e) {
            if ($this->throwExceptions()) {
                throw $e;
            }

            $this->_response->setException($e);
        }

        /**
        * Notify plugins of router completion
        */
        $this->_plugins->routeShutdown($this->_request);

After doing this take the request object $this->_request and set controller , action , module name based on url parameters.

$this->_request()->setModuleName($get[0]);
Malformation answered 9/4, 2012 at 8:38 Comment(6)
Thanks for the reply. I presume that you are answering my second question about skipping the routing process. Where do you put $this->_request()->setModuleName($get[0]);? and what is $get[0]? it isn't defined in Zend_Controller_Front?Simarouba
$get is just an example its basically is current($_GET); , the first parameter in url .Malformation
I see so basically I need to do setModuleName($get[0]);, setControllerName($get[1]); & setActionName($get[2]);. But what about the other parameters sent in th query string if any? Do I need to set them too here?Simarouba
unset first there parameters from $_GET then do $this->_request()->setParams($_GET);Malformation
perfect :) Just wondering though, will setting the route this way cause any troubles for me later? Does that really provide a good performance gain?Simarouba
You have cut out Zend_Route out of the picture whatever resource it was taking will turn into little performance gain. Will not give you trouble if you do not use any zend routing stuff.Malformation

© 2022 - 2024 — McMap. All rights reserved.