당신은 환경에 따라 나뭇 가지 파일을 렌더링 할 FrameworkBundle:Template:template
컨트롤러를 사용할 수 있습니다. Robots.txt는 Content-type: text/html
과 함께 반환됩니다.
robots.txt
을 Content-type: text/plain
으로 반환하려면 다음 작업을 수행해야합니다.
routing.yml
:
app_robots:
pattern: /robots.txt
defaults:
_controller: AppBundle:Static:robots
# template: AppBundle:Static:robots_%kernel.environment%.txt.twig
src/AppBundle/Controller/StaticController.php
:
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
* StaticController controller.
*/
class StaticController extends Controller
{
/**
* Displays robots.txt.
*/
public function robotsAction($template = null)
{
$response = new Response();
$response->headers->set('Content-Type', 'text/plain');
return $this->render($template ?: sprintf(
"AppBundle:Static:robots_%s.txt.twig",
$this->container->getParameter('kernel.environment')
), array(), $response);
}
}
AppBundle/Resources/views/Static/robots_prod.txt.twig
:
# Production robots.txt
User-agent: *
Allow:/
AppBundle/Resources/views/Static/robots_staging.txt.twig
:
# Staging robots.txt
User-agent: *
Disallow:/
원래 web/robots.txt 파일을 지워서 심포니 라우팅을 제어 할 수 있습니다. –
'defaults '에'_format : txt'를''text/plain'으로 제공하기 위해' text/html'. – ACJ