Symfony2:#id在url中指向焦点元素


Symfony2: #id in url to focus element

我有这样的导航链接:href="http://mamgrow.lt/veikla.php#konkursai"

因此,它打开了一个以id='konkursai'为焦点的元素页面。

我怎么能在Symfony做到这一点?

我的页面是作为操作创建的,它们的链接在routing.yml 中进行了描述

主控制器:

public function aboutUsAction() {
    return $this->render('MamgrowMainBundle:Main:about-us.html.twig');
}
public function activitiesAction() {
    return $this->render('MamgrowMainBundle:Main:activities.html.twig');
}
public function contactsAction() {
    return $this->render('MamgrowMainBundle:Main:contacts.html.twig');
}

routing.yml:

about-us:
    path:     /apie-mus
    defaults: { _controller: MamgrowMainBundle:Main:aboutUs }
contacts:
    path:     /kontaktai
    defaults: { _controller: MamgrowMainBundle:Main:contacts }
activities:
    path:     /veikla
    defaults: { _controller: MamgrowMainBundle:Main:activities }

只需将哈希部分附加到生成的路径/url:

<a href="{{ path('contacts') }}#some-anchor">Contact Us</a>

此解决方案可以帮助您处理静态页面:

    static-page:
            path: /website/{page}
            defaults: { _controller: MamgrowMainBundle:Main:static }
        public function staticAction($page) {
            return $this->render('MamgrowMainBundle:Main:'.$page.'.html.twig'');
        }
TwigView.html.twig
    <a href="{{ path('static-page', {"page": "contact-us"} ) }}">Contact Us</a>
    <a href="{{ path('static-page', {"page": "about-us "} ) }}">AboutUs </a>

var"page"的名称必须与您的视图完全相同,因为staticAction方法工作正常!!!