12345678910111213141516171819202122232425262728293031323334353637 |
- <?php
- namespace demo\Controllers;
- use Silex\Application;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\HttpKernelInterface;
- /**
- * Class DelegatingController demonstrates delegation of response building.
- *
- * It shows redirection to a path, and forwarding to a subrequest using either
- * a path or a named route.
- *
- * @package demo\Controllers
- */
- class DelegatingController {
- // Redirect via response header
- public function redirectPath(Application $app) {
- return $app->redirect('/', Response::HTTP_TEMPORARY_REDIRECT); // Default 302.
- }
- // Forward to another controller to avoid redirection, using a path.
- public function forwardPath(Application $app) {
- $subRequest = Request::create('/blogs', 'GET');
- return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
- }
- public function forwardName(Application $app) {
- /** @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface $generator */
- $generator = $app['url_generator'];
- $subRequest = Request::create($generator->generate('blog_list', 'GET'));
- return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
- }
- }
|