8000 [FrameworkBundle][Routing] added Configurators to handle template and… · symfony/symfony@9e19392 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e19392

Browse files
committed
[FrameworkBundle][Routing] added Configurators to handle template and redirect controllers
1 parent 9bfa258 commit 9e19392

26 files changed

+815
-205
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
5.1.0
55
-----
66

7+
* Added `Routing\Loader` and `Routing\Loader\Configurator` namespaces to ease defining routes with default controllers
78
* Added the `framework.router.context` configuration node to configure the `RequestContext`
89
* Made `MicroKernelTrait::configureContainer()` compatible with `ContainerConfigurator`
910
* Added a new `mailer.message_bus` option to configure or disable the message bus to use to send mails.
@@ -29,7 +30,7 @@ CHANGELOG
2930
* Removed the `translator.selector` and `session.save_listener` services
3031
* Removed `SecurityUserValueResolver`, use `UserValueResolver` instead
3132
* Removed `routing.loader.service`.
32-
* Service route loaders must be tagged with `routing.route_loader`.
33+
* Service route loaders must be tagged with `routing.route_loader`.
3334
* Added `slugger` service and `SluggerInterface` alias
3435
* Removed the `lock.store.flock`, `lock.store.semaphore`, `lock.store.memcached.abstract` and `lock.store.redis.abstract` services.
3536
* Removed the `router.cache_class_prefix` parameter.
@@ -81,8 +82,8 @@ CHANGELOG
8182
options if you're using Symfony's serializer.
8283
* [BC Break] Removed the `framework.messenger.routing.send_and_handle` configuration.
8384
Instead of setting it to true, configure a `SyncTransport` and route messages to it.
84-
* Added information about deprecated aliases in `debug:autowiring`
85-
* Added php ini session options `sid_length` and `sid_bits_per_character`
85+
* Added information about deprecated aliases in `debug:autowiring`
86+
* Added php ini session options `sid_length` and `sid_bits_per_character`
8687
to the `session` section of the configuration
8788
* Added support for Translator paths, Twig paths in translation commands.
8889
* Added support for PHP files with translations in translation commands.

src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Kernel;
1313

14+
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\RoutingConfigurator;
15+
use Symfony\Bundle\FrameworkBundle\Routing\Loader\PhpFileLoader as RoutingPhpFileLoader;
1416
use Symfony\Component\Config\Loader\LoaderInterface;
1517
use Symfony\Component\DependencyInjection\ContainerBuilder;
1618
use Symfony\Component\DependencyInjection\Loader\Configurator\AbstractConfigurator;
1719
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1820
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader as ContainerPhpFileLoader;
1921
use Symfony\Component\DependencyInjection\Reference;
20-
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
21-
use Symfony\Component\Routing\Loader\PhpFileLoader as RoutingPhpFileLoader;
2222
use Symfony\Component\Routing\RouteCollection;
2323
use Symfony\Component\Routing\RouteCollectionBuilder;
2424

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<argument type="service" id="file_locator" />
2626
</service>
2727

28-
<service id="routing.loader.php" class="Symfony\Component\Routing\Loader\PhpFileLoader">
28+
<service id="routing.loader.php" class="Symfony\Bundle\FrameworkBundle\Routing\Loader\PhpFileLoader">
2929
<tag name="routing.loader" />
3030
<argument type="service" id="file_locator" />
3131
</service>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
13+
14+
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits\AddTrait;
15+
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator as BaseRouteConfigurator;
16+
17+
/**
18+
* @author Jules Pietri <jules@heahprod.com>
19+
*/
20+
final class GoneRouteConfigurator extends BaseRouteConfigurator
21+
{
22+
use AddTrait;
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
13+
14+
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits\AddTrait;
15+
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator as BaseRouteConfigurator;
16+
17+
/**
18+
* @author Jules Pietri <jules@heahprod.com>
19+
*/
20+
final class NotFoundRouteConfigurator extends BaseRouteConfigurator
21+
{
22+
use AddTrait;
23+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
13+
14+
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits\AddTrait;
15+
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
16+
17+
/**
18+
* @author Jules Pietri <jules@heahprod.com>
19+
*/
20+
class RedirectRouteConfigurator extends RouteConfigurator
21+
{
22+
use AddTrait;
23+
24+
/**
25+
* @param bool $permanent Whether the redirection is permanent
26+
*
27+
* @return $this
28+
*/
29+
final public function permanent(bool $permanent = true)
30+
{
31+
return $this->defaults(['permanent' => $permanent]);
32+
}
33+
34+
/**
35+
* @param bool|array $ignoreAttributes Whether to ignore attributes or an array of attributes to ignore
36+
*
37+
* @return $this
38+
*/
39+
final public function ignoreAttributes($ignoreAttributes = true)
40+
{
41+
return $this->defaults(['ignoreAttributes' => $ignoreAttributes]);
42+
}
43+
44+
/**
45+
* @param bool $keepRequestMethod Whether redirect action should keep HTTP request method
46+
*
47+
* @return $this
48+
*/
49+
final public function keepRequestMethod(bool $keepRequestMethod = true)
50+
{
51+
return $this->defaults(['keepRequestMethod' => $keepRequestMethod]);
52+
}
53+
54+
/**
55+
* @param bool $keepQueryParams Whether redirect action should keep query parameters
56+
*
57+
* @return $this
58+
*/
59+
final public function keepQueryParams(bool $keepQueryParams = true)
60+
{
61+
return $this->defaults(['keepQueryParams' => $keepQueryParams]);
62+
}
63+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
13+
14+
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
15+
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
16+
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits\AddTrait;
17+
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator as BaseRouteConfigurator;
18+
19+
/**
20+
* @author Jules Pietri <jules@heahprod.com>
21+
*/
22+
class RouteConfigurator extends BaseRouteConfigurator
23+
{
24+
use AddTrait;
25+
26+
/**
27+
* @param string $template The template name
28+
* @param array $context The template variables
29+
*/
30+
final public function template(string $template, array $context = []): TemplateRouteConfigurator
31+
{
32+
return (new TemplateRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
33+
->defaults([
34+
'_controller' => TemplateController::class,
35+
'template' => $template,
36+
'context' => $context,
37+
])
38+
;
39+
}
40+
41+
/**
42+
* @param string $route The route name to redirect to
43+
*/
44+
final public function redirectToRoute(string $route): RedirectRouteConfigurator
45+
{
46+
return (new RedirectRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
47+
->defaults([
48+
'_controller' => RedirectController::class.'::redirectAction',
49+
'route' => $route,
50+
])
51+
;
52+
}
53+
54+
/**
55+
* @param string $url The relative path or URL to redirect to
56+
*/
57+
final public function redirectToUrl(string $url): UrlRedirectRouteConfigurator
58+
{
59+
return (new UrlRedirectRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
60+
->defaults([
61+
'_controller' => RedirectController::class.'::urlRedirectAction',
62+
'path' => $url,
63+
])
64+
;
65+
}
66+
67+
final public function notFound(): NotFoundRouteConfigurator
68+
{
69+
return (new NotFoundRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
70+
->defaults([
71+
'_controller' => RedirectController::class.'::redirectAction',
72+
'route' => '',
73+
])
74+
;
75+
}
76+
77+
final public function gone(): GoneRouteConfigurator
78+
{
79+
return (new GoneRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
80+
->defaults([
81+
'_controller' => RedirectController::class.'::redirectAction',
82+
'route' => '',
83+
'permanent' => true,
84+
])
85+
;
86+
}
87+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
13+
14+
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits\AddTrait;
15+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator as BaseRoutingConfigurator;
16+
17+
class RoutingConfigurator extends BaseRoutingConfigurator
18+
{
19+
use AddTrait;
20+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
13+
14+
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits\AddTrait;
15+
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
16+
17+
/**
18+
* @author Jules Pietri <jules@heahprod.com>
19+
*/
20+
class TemplateRouteConfigurator extends RouteConfigurator
21+
{
22+
use AddTrait;
23+
24+
/**
25+
* @param int|null $maxAge Max age for client caching
26+
*
27+
* @return $this
28+
*/
29+
final public function maxAge(?int $maxAge)
30+
{
31+
return $this->defaults(['maxAge' => $maxAge]);
32+
}
33+
34+
/**
35+
* @param int|null $sharedMaxAge Max age for shared (proxy) caching
36+
*
37+
* @return $this
38+
*/
39+
final public function sharedMaxAge(?int $sharedMaxAge)
40+
{
41+
return $this->defaults(['sharedAge' => $sharedMaxAge]);
42+
}
43+
44+
/**
45+
* @param bool|null $private Whether or not caching should apply for client caches only
46+
*
47+
* @return $this
48+
*/
49+
final public function private(?bool $private = true)
50+
{
51+
return $this->defaults(['private' => $private]);
52+
}
53+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits;
13+
14+
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\RouteConfigurator;
15+
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\RoutingConfigurator;
16+
use Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator;
17+
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator as BaseRouteConfigurator;
18+
19+
trait AddTrait
20+
{
21+
/**
22+
* Adds a route.
23+
*
24+
* @param string|array $path the path, or the localized paths of the route
25+
*
26+
* @return RouteConfigurator
27+
*/
28+
public function add(string $name, $path): BaseRouteConfigurator
29+
{
30+
$parentConfigurator = $this instanceof CollectionConfigurator ? $this : ($this instanceof RouteConfigurator ? $this->parentConfigurator : null);
31+
$route = $this->createLocalizedRoute($this->collection, $name, $path, $this->name, $this->prefixes);
32+
33+
return new RouteConfigurator($this->collection, $route, $this->name, $parentConfigurator, $this->prefixes);
34+
}
35+
36+
/**
37+
* Adds a route.
38+
*
39+
* @param string|array $path the path, or the localized paths of the route
40+
*
41+
* @return RouteConfigurator
42+
*/
43+
final public function __invoke(string $name, $path): BaseRouteConfigurator
44+
{
45+
return $this->add($name, $path);
46+
}
47+
}

0 commit comments

Comments
 (0)
0