Symfony2 检查 Db 上的路由
Symfony2 Check Route on Db
一个关于 Symfony2 和路由的问题。
在我的路由文件中有这条路由:
offers_category:
pattern: /offers/{category}
defaults: { _controller: MyBundle:Offers:category}
通过这种方式,我可以调用任何 url,它们都会以 200(HTTP 代码)响应。
类别列表是动态的,由管理面板插入(使用 Sonata 创建)并保存在数据库中。
我想检查 "category" 是否存在,然后回复 200 或 404。
有没有办法告诉 Symfony2(在路由文件中)占位符可用的字典?
我认为我必须检查我的控制器内部调用数据库查询,但我希望找到更好或更干净的解决方案。
谢谢大家
我找到了解决方案!
感谢@lenybernard 对他 post 的宝贵建议。
使用@lenybernard 解决方案,我能够使用索引字段配置路由,例如:
www.sitename.com/offers/1
但我需要 url 比如:
www.sitename.com/offers/travel
为此,我使用此方法将未索引的标签映射到 url:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use NameProject\MyBundle\Entity\Category;
/**
* @Route("/offers/{category}")
* @ParamConverter("type", class="NameProjectMyBundle:Category", options={"mapping": {"category": "name"}})
*/
...一切正常!
有一种叫做 ParamConverter 的简单好方法,但是没有直接在路由文件中做任何事情(此外这不是它的作用)而且你是对的,这是 ControllerSide :
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use My\Bundle\MyBundle\Entity\Offers\Category;
class OffersController
{
/**
* @param Category $category
* @ParamConverter("category", class="MyBundle:Offers/category")
*/
public function showAction(Category $category)
{
...
}
}
如文档中所述,幕后发生了几件事:
The converter tries to get a MyBundle:Offers/category object from the
request attributes (request attributes comes from route placeholders
-- here id); If no Category object is found, a 404 Response is generated; If a Category object is found, a new category request attribute is defined
(accessible via $request->attributes->get('category')); As for other
request attributes, it is automatically injected in the controller
when present in the method signature. If you use type hinting as in
the example above, you can even omit the @ParamConverter annotation
altogether because it's automatic :
所以您只需转换变量,参数转换器就会自动抛出 404,很酷吧?
use My\Bundle\MyBundle\Entity\Offers\Category;
class OffersController
{
/**
* @param Category $category
*/
public function showAction(Category $category)
{
...
}
}
一个关于 Symfony2 和路由的问题。
在我的路由文件中有这条路由:
offers_category:
pattern: /offers/{category}
defaults: { _controller: MyBundle:Offers:category}
通过这种方式,我可以调用任何 url,它们都会以 200(HTTP 代码)响应。 类别列表是动态的,由管理面板插入(使用 Sonata 创建)并保存在数据库中。
我想检查 "category" 是否存在,然后回复 200 或 404。 有没有办法告诉 Symfony2(在路由文件中)占位符可用的字典?
我认为我必须检查我的控制器内部调用数据库查询,但我希望找到更好或更干净的解决方案。
谢谢大家
我找到了解决方案!
感谢@lenybernard 对他 post 的宝贵建议。
使用@lenybernard 解决方案,我能够使用索引字段配置路由,例如:
www.sitename.com/offers/1
但我需要 url 比如:
www.sitename.com/offers/travel
为此,我使用此方法将未索引的标签映射到 url:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use NameProject\MyBundle\Entity\Category;
/**
* @Route("/offers/{category}")
* @ParamConverter("type", class="NameProjectMyBundle:Category", options={"mapping": {"category": "name"}})
*/
...一切正常!
有一种叫做 ParamConverter 的简单好方法,但是没有直接在路由文件中做任何事情(此外这不是它的作用)而且你是对的,这是 ControllerSide :
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use My\Bundle\MyBundle\Entity\Offers\Category;
class OffersController
{
/**
* @param Category $category
* @ParamConverter("category", class="MyBundle:Offers/category")
*/
public function showAction(Category $category)
{
...
}
}
如文档中所述,幕后发生了几件事:
The converter tries to get a MyBundle:Offers/category object from the request attributes (request attributes comes from route placeholders -- here id); If no Category object is found, a 404 Response is generated; If a Category object is found, a new category request attribute is defined (accessible via $request->attributes->get('category')); As for other request attributes, it is automatically injected in the controller when present in the method signature. If you use type hinting as in the example above, you can even omit the @ParamConverter annotation altogether because it's automatic :
所以您只需转换变量,参数转换器就会自动抛出 404,很酷吧?
use My\Bundle\MyBundle\Entity\Offers\Category;
class OffersController
{
/**
* @param Category $category
*/
public function showAction(Category $category)
{
...
}
}