[FrameworkBundle] fixed built-in controllers

This commit is contained in:
Fabien Potencier 2010-08-14 22:03:02 +02:00
parent 96e9a682b3
commit 714fa6f652
5 changed files with 54 additions and 16 deletions

View File

@ -3,6 +3,7 @@
namespace Symfony\Bundle\FrameworkBundle\Controller; namespace Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Components\HttpFoundation\Response;
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony framework.
@ -20,8 +21,13 @@ use Symfony\Bundle\FrameworkBundle\Controller;
*/ */
class DefaultController extends Controller class DefaultController extends Controller
{ {
/**
* Renders the Symfony2 welcome page.
*
* @return Response A Response instance
*/
public function indexAction() public function indexAction()
{ {
return $this->render('FrameworkBundle:Default:index'); return $this['templating']->renderResponse('FrameworkBundle:Default:index');
} }
} }

View File

@ -25,13 +25,18 @@ use Symfony\Components\HttpKernel\Exception\HttpException;
class ExceptionController extends Controller class ExceptionController extends Controller
{ {
/** /**
* Converts an Exception to a Response.
*
* @param \Exception $exception An Exception instance
* @param Request $request The original Request instance
* @param array $logs An array of logs
*
* @throws \InvalidArgumentException When the exception template does not exist * @throws \InvalidArgumentException When the exception template does not exist
*/ */
public function exceptionAction(\Exception $exception, Request $originalRequest, array $logs) public function exceptionAction(\Exception $exception, Request $originalRequest, array $logs)
{ {
$template = $this->container->getParameter('kernel.debug') ? 'exception' : 'error'; $template = $this->container->getParameter('kernel.debug') ? 'exception' : 'error';
$request = $this->getRequest();
$format = $format = $originalRequest->getRequestFormat(); $format = $format = $originalRequest->getRequestFormat();
// when using CLI, we force the format to be TXT // when using CLI, we force the format to be TXT
@ -39,7 +44,7 @@ class ExceptionController extends Controller
$format = 'txt'; $format = 'txt';
} }
$template = $this->container->getTemplatingService()->getLoader()->load($template, array( $template = $this['templating']->getLoader()->load($template, array(
'bundle' => 'FrameworkBundle', 'bundle' => 'FrameworkBundle',
'controller' => 'Exception', 'controller' => 'Exception',
'format' => '.'.$format, 'format' => '.'.$format,
@ -73,7 +78,7 @@ class ExceptionController extends Controller
require $template; require $template;
$content = ob_get_clean(); $content = ob_get_clean();
$response = $this->container->getResponseService(); $response = $this['response'];
$response->setStatusCode($code); $response->setStatusCode($code);
$response->setContent($content); $response->setContent($content);

View File

@ -3,6 +3,7 @@
namespace Symfony\Bundle\FrameworkBundle\Controller; namespace Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Components\HttpFoundation\Response;
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony framework.
@ -20,16 +21,25 @@ use Symfony\Bundle\FrameworkBundle\Controller;
*/ */
class InternalController extends Controller class InternalController extends Controller
{ {
public function indexAction() /**
* Forwards to the given controller with the given path.
*
* @param string $path The path
* @param string $controller The controller name
*
* @return Response A Response instance
*/
public function indexAction($path, $controller)
{ {
$request = $this->getRequest(); $request = $this['request'];
$attributes = $request->attributes;
if ('none' !== $request->attributes->get('path')) if ('none' !== $path)
{ {
parse_str($request->attributes->get('path'), $tmp); parse_str($path, $tmp);
$request->attributes->add($tmp); $attributes->add($tmp);
} }
return $this->forward($request->attributes->get('controller'), $request->attributes->all(), $request->query->all()); return $this['controller_resolver']->forward($controller, $attributes->all(), $request->query->all());
} }
} }

View File

@ -3,6 +3,7 @@
namespace Symfony\Bundle\FrameworkBundle\Controller; namespace Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Components\HttpFoundation\Response;
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony framework.
@ -20,7 +21,7 @@ use Symfony\Bundle\FrameworkBundle\Controller;
*/ */
class RedirectController extends Controller class RedirectController extends Controller
{ {
/* /**
* Redirects to another route. * Redirects to another route.
* *
* It expects a route path parameter. * It expects a route path parameter.
@ -28,11 +29,16 @@ class RedirectController extends Controller
* *
* If the route empty, the status code will be 410. * If the route empty, the status code will be 410.
* If the permanent path parameter is set, the status code will be 302. * If the permanent path parameter is set, the status code will be 302.
*
* @param string $route The route pattern to redirect to
* @param Boolean $permanent Whether the redirect is permanent or not
*
* @return Response A Response instance
*/ */
public function redirectAction($route, $permanent = false) public function redirectAction($route, $permanent = false)
{ {
if (!$route) { if (!$route) {
$response = $this->container->getResponseService(); $response = $this['response'];
$response->setStatusCode(410); $response->setStatusCode(410);
return $response; return $response;
@ -40,9 +46,12 @@ class RedirectController extends Controller
$code = $permanent ? 301 : 302; $code = $permanent ? 301 : 302;
$parameters = $this->getRequest()->getPathParameters(); $attributes = $this['request']->attributes->all();
unset($parameters['_route'], $parameters['route']); unset($attributes['_route'], $attributes['route']);
return $this->redirect($this->container->getRouterService()->generate($route, $parameters), $code); $response = $this['response'];
$response->setRedirect($this['router']->generate($route, $attributes), $code);
return $response;
} }
} }

View File

@ -3,6 +3,7 @@
namespace Symfony\Bundle\FrameworkBundle\Controller; namespace Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Components\HttpFoundation\Response;
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony framework.
@ -20,8 +21,15 @@ use Symfony\Bundle\FrameworkBundle\Controller;
*/ */
class TemplateController extends Controller class TemplateController extends Controller
{ {
/**
* Renders a template.
*
* @param string $template The template name
*
* @return Response A Response instance
*/
public function templateAction($template) public function templateAction($template)
{ {
return $this->render($template); return $this['templating']->renderResponse($template);
} }
} }