我可以在Symfony2中拥有特定于方案的路由吗?

时间:2014-10-08 20:49:12

标签: symfony

如果方案是https,我想提供一个页面,如果没有则抛出404。这在Symfony路由中是否可行?

http://mysite.com/api/members< - 应抛出404

http s ://mysite.com/api/members< - 应该正常工作

我使用的以下路由是对http进行自动重定向,而不是404:

acme_two_index:
    path:     /api/members
    defaults: { _controller: AcmeTestBundle:Default:members }
    schemes:  [https]
    methods:  [GET]

1 个答案:

答案 0 :(得分:0)

除了您现有的路线之外,您可以尝试这一点,但我担心它的功效,因为schemes参数旨在强制路由到特定协议:

acme_two_index_notfound:
    path:     /api/members
    defaults: { _controller: AcmeTestBundle:Default:membersNotFound }
    schemes:  [http]
    methods:  [GET]

并在DefaultController.php中单独设置一个控制器:

public function membersNotFoundAction()
{
    throw $this->createNotFoundException('Page not found for this protocol');
    return; // Just in case Symfony needs to see a return
}

您可能需要只允许路由中的所有方案并在控制器中处理它:

acme_two_index:
    path:     /api/members
    defaults: { _controller: AcmeTestBundle:Default:members }
    methods:  [GET]

public function membersAction()
{
    if (!$this->get('request')->isSecure()) {
        throw $this->createNotFoundException('Page not found for this protocol');
    }
    // ... rest of your code
}
相关问题