Zend framework 带可选参数的Zend表达路线

Zend framework 带可选参数的Zend表达路线,zend-framework,mezzio,Zend Framework,Mezzio,我希望使用路由来获取完整的集合,如果可用,还需要筛选集合 所以我的路线是: $app->get("/companies", \App\Handler\CompanyPageHandler::class, 'companies'); 此路线的我的处理程序: use App\Entity\Company; use App\Entity\ExposeableCollection; use Psr\Http\Message\ResponseInterface; use Psr\Http\Mess

我希望使用路由来获取完整的集合,如果可用,还需要筛选集合

所以我的路线是:

$app->get("/companies", \App\Handler\CompanyPageHandler::class, 'companies');
此路线的我的处理程序:

use App\Entity\Company;
use App\Entity\ExposeableCollection;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class CompanyPageHandler extends AbstractHandler
{
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $categories = new ExposeableCollection();
        foreach (['test', 'test1', 'test3'] as $name) {
            $category = new Company();
            $category->setName($name);

            $categories->addToCollection($category);
        }

        return $this->publish($categories);
    }
}
当获得此路线时,我获得了预期的收集

[{"name":"test"},{"name":"test1"},{"name":"test3"}]
所以现在我改变路线

$app->get("/companies[/:search]", \App\Handler\CompanyPageHandler::class, 'companies');
当我浏览到
/公司时,一切都很好。
但是如果我尝试可选参数
/companys/test1
,那么我会得到一个错误

得不到

“我的作曲家”部分:

"require": {
    "php": "^7.1",
    "zendframework/zend-component-installer": "^2.1.1",
    "zendframework/zend-config-aggregator": "^1.0",
    "zendframework/zend-diactoros": "^1.7.1 || ^2.0",
    "zendframework/zend-expressive": "^3.0.1",
    "zendframework/zend-expressive-helpers": "^5.0",
    "zendframework/zend-stdlib": "^3.1",
    "zendframework/zend-servicemanager": "^3.3",
    "zendframework/zend-expressive-fastroute": "^3.0"
},
在Zend Framework 2和Symfony4中,此路由定义工作正常。所以我很困惑。
为什么我的可选参数不起作用?

这是因为您使用的是路由器,正确的语法是:

$app->get("/companies[/{search}]", \App\Handler\CompanyPageHandler::class, 'companies');
或者更严格地验证搜索参数,如下所示:

$app->get("/companies[/{search:[\w\d]+}]", \App\Handler\CompanyPageHandler::class, 'companies');

这是因为您使用的是路由器,正确的语法是:

$app->get("/companies[/{search}]", \App\Handler\CompanyPageHandler::class, 'companies');
或者更严格地验证搜索参数,如下所示:

$app->get("/companies[/{search:[\w\d]+}]", \App\Handler\CompanyPageHandler::class, 'companies');