2014-10-08 56 views
0

如果方案是https,我想提供一個頁面,如果不是,則拋出404。這在Symfony路由中可能嗎?我可以在Symfony2中使用特定的路線嗎?

HTTP://mysite.com/api/members < - 應該拋出404

HTTP 小號://mysite.com/api/members < - 應該工作

我使用的以下路由是對http進行自動重定向,而不是404:

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

回答

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 
} 
+0

第一個選項不起作用,它看起來第二個是目前唯一的工作。我會看看我是否可以打開功能請求。謝謝 – Jimbotron 2014-10-09 13:58:58

相關問題