简体   繁体   中英

Optimizing Zend framework routes

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.

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM