Search code examples
phpzend-frameworkzend-route

How to use match function in Zend (ZF1) Custom route


I’m developing a website under Zend Framework 1.12 where users can upload pictures to illustrate their profile.

I’m trying to implement a function that will check, every time a miniature is displayed, if it exists and save it in different sizes if not (the objective is to accelerate browsing by reducing HTTP request size):

  1. The registered user upload his full size original picture (e.g. 2400 x 2400px 1MB)
  2. The picture is saved in a folder named USER_SIZE on the server
  3. Other users navigate on the website:
    • home page or any other page
    • page with the list of users where I want to display 50x50 miniature for each user
    • detailed user profile where I want to display a 100x100 miniature picture

To do this I was hoping to use a customized router that would transform picture URL and automatically will scale / crop and save the miniatures if it doesn’t already exists.

Public page (the view) :

<html>
<body>
  ...
  <img src="<?php echo $this->url(array("method" => "scale", "params" => "50x50", "filename" => $userFileName), "image")">
  ...
</body>
</html>

Custom route :

<?php

require_once 'Zend/Controller/Router/Route/Abstract.php';

Class Application_Controllers_Route_Image extends Zend_Controller_Router_Route_Abstract
{
  public static function getInstance(Zend_Config $config)
  {
    return new self($config->route);
  }

  // NOT WORKING
  public function match($path)
  {
    // Check if the URL is a miniature picture URL
    if (preg_match("#miniatures/([^/]+)/([^/]*)/(.+)#"){
      // resize picture and save miniature
      ...
    }
  }

  // WORKING
  public function assemble($data = array)
  {
    // Return a new path name for the miniature that should have been
    // resized and saved in a different folder for which the name is constructed
    // with params used in the view
    return sprint("miniatures/%s/%s/%s", $data["method"], $data["params"], $data["filename"]
  }
}

Of course all this code is very simplified. I have been able to make the custom route work to generate the miniature URL and receive something like miniatures/scale/50x50/userpicture.jpg as a result of the match function. However, I’m totally unable to capture the call to the router in the match function. This is very poorly documented in Zend documentation and I didn’t find any ideas with Google or StackExchange.

Do you know when and how the match function is called in the workflow? Does anybody has done something similar that could help?


Solution

  • Thank you Tim you're right!

    The default ZF htaccess will ignore files that already exist. If you're creating these resized images at the same URL as your route (but in the public folder), subsequent requests will bypass ZF. This is not necessarily a bad thing, but I think that's why it's not working as you expect.

    The problem was related to the htaccess in which I had a URL rewriting parameter for IMG files save in the public folder subdirectory.

    I also had to store the resized images outside the public folder.