Optimizing Zend framework routes

蹲街弑〆低调 提交于 2019-12-06 13:48:25

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.

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.

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]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!