如何编写直接提供请求的Zend Framework App(将URL映射到文件)?

时间:2017-06-27 18:33:03

标签: php zend-framework3 psr-7

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

我想转移到ZF3堆栈,并且路由有所不同。我想在ZF3上为新模块保留ZF路由模型,但也能够原样使用旧版应用程序,因为重写该应用程序是禁止的。

有办法吗?

不确定是否可以这样做,但我在这里查看了中间件层:

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

例如,我设置了这个类,不知道下一步该做什么。

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;
    }
}

1 个答案:

答案 0 :(得分:1)

如果你看看你的.htaccess或你的配置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]

如果您阅读了评论,则可以看到文件和目录按原样提供。