2015-05-19 20 views
0

是否有可能将一组静态HTML文件放在symfony 2.x应用程序下,以使URL根本不会改变?Symfony可以使用以.html结尾的路由服务Twig模板吗?

我想保持这样的网址:HTTP://example.com/town/Blah-blah-in-Bayeux.html

我知道我可以重写example.com/app.php/town /Blah-blah-in-Bayeux.html,以便app.php不是脚本的一部分,但是我怎样才能确保我的路由到默认控制器的城镇行动以.html结尾?

+0

http://symfony.com/doc/current/cookbook/templating/render_without_controller.html – smarber

+0

路径可以是几乎任何你喜欢的。点或“html”没有什么特别之处。如果你这样做是为了保留旧网站的URL,那么一些301重定向到清理URL可能是更好的选择。如果你认为.html结尾有助于搜索引擎优化,那么这是多年前一个模糊的未经证实的想法。 – tetranz

回答

1

这很简单!在这里,我使用了FrameworkExtraBundle @Route注释,其中路由模式简单地以.html结尾。

<?php 
namespace Foo\BarBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; 

class HomeController extends Controller 
{ 
    /** 
    * Demo of a route which includes a .html suffix 
    * 
    * @Route("/town/Blah-blah-in-{townName}.html", name="town") 
    * @Template() 
    */ 
    public function townAction($townName) 
    { 
     # Lookup $town slug to get a town 
     # ... 
     # 404 if town not found 
     # ... 

     # just for illustration 
     $town = $townName; 

     return array('town' => $town); 
    } 
} 
{# src/Foo/BarBundle/Resources/Home/town.html.twig #} 

{% extends 'FooBarBundle::layout.html.twig' %} 

{% block title %}Blah blah in {{ town }} - {{ parent() }}{% endblock %} 

{% block content %} 
    <h1>Blah blah in {{ town }}!</h1> 
    <p> and other content from the original /town/Blah-blah-in-{{ town }}.html</p> 
{% endblock %} 
相关问题