简体   繁体   English

Symfony 2.2 - 从路由生成URL或只显示URL

[英]Symfony 2.2 - Generate URL from route OR simply display URL

I'm developing a navigation system for Symfony 2. It's working really nicely so far. 我正在为Symfony 2开发一个导航系统。到目前为止它的工作非常好。 So far, there is a config file like so: 到目前为止,有一个像这样的配置文件:

# The menu name ...
primary:
    # An item in the menu ...
    Home:
        enabled: 1
        # Routes where the menu item should be shown as 'active' ...
        routes:
            - "a_route_name"
        # Where the link goes to ... the problem ...
        target: "a_route_name"

This layout is working nicely, and the menu works. 这种布局运行良好,菜单有效。 Apart from in my template, I can only generate links using the target value that correspond to routes within an application; 除了在我的模板中,我只能使用与应用程序中的路径对应的目标值生成链接; ie, not an external URL. 即,不是外部URL。

The template is as follows for generating the navigation: 生成导航的模板如下:

{# This is what puts the data for the menu into the page currently ... #}
{% set primary_nav = menu_data('primary') %}

<nav role="navigation" class="primary-nav">
    <ul class="clearfix">
        {% for key, item in primary_nav if item.enabled is defined and item.enabled %}
            {% if item.routes is defined and app.request.attributes.get('_route') in item.routes %}
                <li class="active">
            {% else %}
                <li>
            {% endif %}
                {% if item.target is defined %}
                    <a href="{{ path(item.target) }}">{{ key }}</a>
                {% else %}
                    {{ key }}
                {% endif %}
            </li>
        {% endfor %}
    </ul>
</nav>

Is there a simple way to allow the path() function or, something similar to generate URLs from routes, or just simply use a given URL if it validates as one? 是否有一种简单的方法来允许使用path()函数或者类似于从路由生成URL的方法,或者只是简单地使用给定的URL(如果它验证为一个)?

I got as far as trying url() , and looked around the docs but couldn't see anything. 我尝试了url() ,并查看了文档,但看不到任何东西。

You can create a Twig extension that check if a route exists : 您可以创建一个Twig扩展来检查路由是否存在:

  • if it exists, the corresponding generated url is returned 如果存在,则返回相应的生成的url

  • else, the url (or other stuff) is returned without any change 否则,返回url(或其他东西)而不做任何更改

In your services.yml, declare your twig extension and inject the router component. 在您的services.yml中,声明您的twig扩展并注入路由器组件。 Add the following lines and change namespaces : 添加以下行并更改名称空间:

  fuz_tools.twig.path_or_url_extension:
    class: 'Fuz\ToolsBundle\Twig\Extension\PathOrUrlExtension'
    arguments: ['@router']
    tags:
      - { name: twig.extension }

Then create a Twig\\Extension directory in your bundle, and create PathOrUrlExtension.php : 然后在您的包中创建一个Twig \\ Extension目录,并创建PathOrUrlExtension.php:

<?php

namespace Fuz\ToolsBundle\Twig\Extension;

use Symfony\Bundle\FrameworkBundle\Routing\Router;

class PathOrUrlExtension extends \Twig_Extension
{

    private $_router;

    public function __construct(Router $router)
    {
        $this->_router = $router;
    }

    public function getFunctions()
    {
        return array(
                // will call $this->pathOrUrl if pathOrUrl() function is called from twig
                'pathOrUrl' => new \Twig_Function_Method($this, 'pathOrUrl')
        );
    }

    public function pathOrUrl($pathOrUrl)
    {
        // the route collection returns null on undefined routes
        $exists = $this->_router->getRouteCollection()->get($pathOrUrl);
        if (null !== $exists)
        {
            return $this->_router->generate($pathOrUrl);
        }
        return $pathOrUrl;
    }

    public function getName()
    {
        return "pathOrUrl";
    }

}

You can now use your new function : 您现在可以使用新功能:

{{ pathOrUrl('fuz_home_test') }}
<br/>
{{ pathOrUrl('http://www.google.com') }}

Will display : 将显示:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM