doucuo9126 2013-04-27 12:56
浏览 17
已采纳

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

I'm developing a navigation system for Symfony 2. It's working really nicely so far. 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; i.e, not an external 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?

I got as far as trying url(), and looked around the docs but couldn't see anything.

  • 写回答

1条回答 默认 最新

  • dqy006150 2013-04-28 06:22
    关注

    You can create a Twig extension that check if a route exists :

    • if it exists, the corresponding generated url is returned

    • else, the url (or other stuff) is returned without any change

    In your services.yml, declare your twig extension and inject the router component. 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 :

    <?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 :

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.