如何在Symfony2中使用路由配置中的cookie? [英] How to use a cookie in routing configuration in Symfony2?

查看:123
本文介绍了如何在Symfony2中使用路由配置中的cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储在cookie中的 City 参数。我想在我的路由配置中包括它的值作为模式前缀,如:

 #MyBundle / Resources / config / routing .yml 

MyBundle_hotel:
resource:@ MyBundle / Resources / config / routing / hotel.yml
前缀:/%cityNameFromCookie%/ hotel



如何实现?

解决方案

给我们一个使用案例,如何你想要这个工作,因为我没有看到的困难。路由由您可以通过 generateUrl 函数, url twig函数或 path twig函数。



在Twig中你可以这样做:

  {{path('MyBundle_hotel',{cityNameFromCookie:app.request.cookies.get('cityNameFromCookie')})}} 

在控制器操作中

  $ cookieValue = $ this-> get('request') - > cookies-> get('cityNameFromCookie'); 
$ url = $ this-> generateUrl('MyBundle_hotel',array('cityNameFromCookie'=> $ cookieValue));

或从任何可访问容器的地方

  $ cookieValue = $ this-> container-> get('request') - > cookie-> get('cityNameFromCookie'); 
$ url = $ this-> container-> get('router') - > generate('MyBundle_hotel',array('cityNameFromCookie'=> $ cookieValue));

在上一个示例中,您可能想要更改容器的访问方式。



如果你担心它看起来有多复杂,你可以抽象这个逻辑,并把它放在一个服务或扩展路由器 service。



您可以找到有关服务和服务容器在Symfony的文档



您还可以通过命令 php app / console container:debug ,会找到 router 服务及其命名空间,从中可以尝试找出如何扩展 router service(一个非常好的方式来了解服务的工作方式)。



否则,这里是创建服务的简单方法。 p>

在您的services.yml中(在您的Bundle中或在app / config / config.yml中)

  services:
city:
class:MyBundle\Service\CityService
arguments:[@router,@request]

在您的 CityService

 命名空间MyBundle\Service 

class CityService
{

protected $ router;
protected $ request;

public function __construct($ router,$ request)
{
$ this-> router = $ router;
$ this-> request = $ request;
}

public function generateUrl($ routeName,$ routeParams,$ absoluteUrl)
{
$ cookieValue = $ this-> request-> cookies-> ; get('cityNameFromCookie');

$ routeParams = array_merge($ routeParams,array('cityNameFromCookie'=> $ cookieValue));

return $ this-> router-> generateUrl($ routeName,$ routeParams,$ absoluteUrl);
}
}

您可以访问容器的任何地方,能够执行以下操作

  $ this-> container-> get('city') - > generateUrl yourroute',$ params); 

如果你仍然认为这不是一个很好的解决方案;你将不得不扩展路由器服务(或找到一个更好的方式来扩展路由器组件,使其行为方式与你期望的一样)。



我个人使用上面的方法,所以我可以传递一个实体到 path 方法在Twig。您可以在我的 MainService类 PathExtension Twig类 services.yml



,我可以做 forum_path('routename',ForumEntity),在容器感知环境中,我可以做 $ this-> container-> get ('cornichon.forum') - > forumPath('routename',ForumEntity)



您应该有足够的信息决定


I have a City parameter stored in a cookie. I would like to include its value as a pattern prefix in my routing configuration like so:

# MyBundle/Resources/config/routing.yml

MyBundle_hotel:
    resource: "@MyBundle/Resources/config/routing/hotel.yml"
    prefix:   /%cityNameFromCookie%/hotel

How can I achieve that?

解决方案

Give us a use case on how you would want this to work because I don't see the difficulty. Routes are made of parameters that you can specify through the generateUrl function, the url twig function or the path twig function.

In Twig you can do this

{{ path('MyBundle_hotel', {cityNameFromCookie: app.request.cookies.get('cityNameFromCookie')}) }}

In a controller action

$cookieValue = $this->get('request')->cookies->get('cityNameFromCookie');
$url = $this->generateUrl('MyBundle_hotel', array('cityNameFromCookie' => $cookieValue));

Or from any places that have access to the container

$cookieValue = $this->container->get('request')->cookies->get('cityNameFromCookie');
$url = $this->container->get('router')->generate('MyBundle_hotel', array('cityNameFromCookie' => $cookieValue));

In the last example, you will probably want to change how the container is being accessed.

If you are concerned about how complicated it looks like, you can abstract this logic and put it inside a service or extend the router service.

You can find documentation about services and the service container in the Symfony's documentation.

You can also list the services via the command php app/console container:debug and will find the router service and its namespace and from this you can try to figure out how to extend the router service (a very good way to learn how services work).

Otherwise, here is simple way to create a service.

In your services.yml (either in your Bundle or in app/config/config.yml)

services:
    city:
        class: MyBundle\Service\CityService
        arguments: [@router, @request]

In your CityService class

namespace MyBundle\Service

class CityService
{

    protected $router;
    protected $request;

    public function __construct($router, $request) 
    {
        $this->router = $router;
        $this->request = $request;
    }

    public function generateUrl($routeName, $routeParams, $absoluteUrl)
    {
        $cookieValue = $this->request->cookies->get('cityNameFromCookie');

        $routeParams = array_merge($routeParams, array('cityNameFromCookie' => $cookieValue));

        return $this->router->generateUrl($routeName, $routeParams, $absoluteUrl);
    }
}

Anywhere you have access to the container, you will be able to do the following

$this->container->get('city')->generateUrl('yourroute', $params);

If you still think that it isn't a great solution; you will have to extend the router service (or find a better way to extend the router component to make it behave the way you are expecting it to).

I personally use the method above so I can pass an entity to a path method in Twig. You can find an example in my MainService class and PathExtension Twig class defined in the services.yml.

In Twig, I can do forum_path('routename', ForumEntity) and in a container aware environment I can do $this->container->get('cornichon.forum')->forumPath('routename', ForumEntity).

You should have enough information to make an informed decision

这篇关于如何在Symfony2中使用路由配置中的cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆