0

Background: I have a legacy app that serves files as-is. That is, i.e. when I go to http://server/subfolder/my_index.php?value=x it goes into subfolder on the file system and serves a file called my_index.php and passes it the URL parameters and returns a response.

I want to move onto ZF3 stack, and routing there is different. I want to retain ZF routing model for new modules on ZF3, but also be able to use the legacy app as-is, since rewriting that app is prohibitive.

Is there a way to do so?

Not sure if it is the way to do it but I looked into the Middleware layer here:

I am not clear on how to use them and if they will help me.

For example, I set up this class and not sure what to do next.

namespace Application\Middleware;

use Psr\Http\Message\ServerRequestInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Zend\Http\Response;

class IndexMiddleware implements MiddlewareInterface
{

    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
    {
        $response = new \Zend\Diactoros\Response();
        return $response;
    }
}
Dennis
  • 7,907
  • 11
  • 65
  • 115

1 Answers1

1

If you look at what your .htaccess or your config Nginx maybe, you would have something like that in a ZF3 project:

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting or installed the project in a subdirectory,
# the base path will be prepended to allow proper resolution of
# the index.php file; it will work in non-aliased environments
# as well, providing a safe, one-size fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L]

If you read the comments, you can see that files and directory are served as is if they exist.

Thomas Dutrion
  • 1,844
  • 1
  • 11
  • 9
  • I see.. I will try to move my legacy file folder structure into the `./public` folder and see if that works – Dennis Jun 27 '17 at 18:43
  • it did work (I put ZF3 into main folder). I did have thoughts moving legacy files into `public` before but never tried it until now. But something was off, like when file was not found I think it was trying to go through ZF3 routing and that slowed things down. Maybe. – Dennis Jun 27 '17 at 21:01
  • I went with the approach of placing ZF3 into a subfolder instead. That is, my legacy app is untouched, a new folder is added (I called it `zf`). It worked. Then I moved it all one folder up to where ZF3's config, vendor, and other folders ended up being in the main DOCUMENT_ROOT. It seems to work. I call `http://host/whatever.php` for main app and I call `http://host/public/` for ZF3. It seems like now I can have the best of both words - legacy and ZF3 and all on the same repo (as I wanted). I bet I can call one from the other as well. – Dennis Jun 27 '17 at 21:03
  • also reason I installed it into subfolder first was to isolate ZF3, and because I have my own config, vendor folders already. I had to edit `composer.json` slightly to merge the two worlds together – Dennis Jun 27 '17 at 21:06
  • Can someone post an example for a virtual host file for a zf3 project with nginx? – cwhisperer Jun 12 '18 at 21:18
  • the question has already been covered: https://stackoverflow.com/questions/19731555/how-to-make-zend-framework-2-work-with-nginx - Anything that's not working for you cwhisperer? – Thomas Dutrion Jun 12 '18 at 21:23