如何编写一个直接服务请求的Zend Framework应用程序(将URL映射到文件)



背景:我有一个提供文件的旧应用程序。也就是说,即当我进入http://server/subfolder/my_index.php?value=x时,它将进入文件系统上的subfolder并提供一个名为my_index.php的文件,并将其传递给URL参数并返回响应。

我想转到ZF3堆栈,并且路线不同。我想保留ZF3上新模块的ZF路由模型,但也能够使用旧应用程序AS-IS,因为重写该应用程序是过时的。

有没有办法这样做?

不确定是否是这样做的方法,但我在这里查看了中间件:

  • https://docs.zendframework.com/zend-mvc/middleware/和
  • https://zendframework.github.io/zend-diactoros/usage/

我不清楚如何使用它们以及是否会帮助我。

例如,我设置了此类,不确定下一步该怎么做。

namespace ApplicationMiddleware;
use PsrHttpMessageServerRequestInterface;
use InteropHttpServerMiddlewareMiddlewareInterface;
use InteropHttpServerMiddlewareDelegateInterface;
use ZendHttpResponse;
class IndexMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
    {
        $response = new ZendDiactorosResponse();
        return $response;
    }
}

如果您查看 .htaccess或config nginx的内容,则可能在ZF3项目中有类似的东西:

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]

如果您阅读了评论,则可以看到文件和目录的使用,就像存在一样。

相关内容

最新更新