Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 477ee19

Browse filesBrowse files
feature #30501 [FrameworkBundle][Routing] added Configurators to handle template and redirect controllers (HeahDude)
This PR was merged into the 5.1-dev branch. Discussion ---------- [FrameworkBundle][Routing] added Configurators to handle template and redirect controllers | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | let's see | Fixed tickets | partially #24640, #25145 | License | MIT | Doc PR | symfony/symfony-docs#11120 While working on symfony/symfony-docs#11085, I felt bad about the long notations required for simple [redirects](https://symfony.com/doc/current/routing/redirect_in_config.html) and [templates rendering](https://symfony.com/doc/current/templating/render_without_controller.html) template actions, but I love and use those features since always. Then I gave it a try yesterday night and now I realised I missed #24640 and that #25145 has been closed x). So here we go, here's my WIP. WDYT of this implementation? ping @javiereguiluz? I'm going to open the PR in the docs so we can discuss the DX changes there too, and keep focus on the code here. Cheers! EDIT ---- This PR now only update PHP-DSL configurators. ______________ TODO: - [x] gather reviews - ~[x] fix xml schema~ - [x] add some tests - ~[ ] handle xsd auto discovery~ - [x] rebase on top of #30507 - [x] ~add shortcuts for #30514~ Commits ------- de74794 [FrameworkBundle][Routing] added Configurators to handle template and redirect controllers
2 parents 9bfa258 + de74794 commit 477ee19
Copy full SHA for 477ee19

25 files changed

+788
-205
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
+4-3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php
+2-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml
+1-1Lines 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>
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 Nicolas Grekas <p@tchwork.com>
19+
*/
20+
class GoneRouteConfigurator 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+
}
+63Lines 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+
}
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\Component\Routing\Loader\Configurator\RouteConfigurator as BaseRouteConfigurator;
17+
18+
/**
19+
* @author Jules Pietri <jules@heahprod.com>
20+
*/
21+
class RouteConfigurator extends BaseRouteConfigurator
22+
{
23+
/**
24+
* @param string $template The template name
25+
* @param array $context The template variables
26+
*/
27+
final public function template(string $template, array $context = []): TemplateRouteConfigurator
28+
{
29+
return (new TemplateRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
30+
->defaults([
31+
'_controller' => TemplateController::class,
32+
'template' => $template,
33+
'context' => $context,
34+
])
35+
;
36+
}
37+
38+
/**
39+
* @param string $route The route name to redirect to
40+
*/
41+
final public function redirectToRoute(string $route): RedirectRouteConfigurator
42+
{
43+
return (new RedirectRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
44+
->defaults([
45+
'_controller' => RedirectController::class.'::redirectAction',
46+
'route' => $route,
47+
])
48+
;
49+
}
50+
51+
/**
52+
* @param string $url The relative path or URL to redirect to
53+
*/
54+
final public function redirectToUrl(string $url): UrlRedirectRouteConfigurator
55+
{
56+
return (new UrlRedirectRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
57+
->defaults([
58+
'_controller' => RedirectController::class.'::urlRedirectAction',
59+
'path' => $url,
60+
])
61+
;
62+
}
63+
64+
final public function gone(): GoneRouteConfigurator
65+
{
66+
return (new GoneRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
67+
->defaults([
68+
'_controller' => RedirectController::class.'::redirectAction',
69+
'route' => '',
70+
])
71+
;
72+
}
73+
}
+20Lines 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+
}
+53Lines 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+
}
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Component\Routing\Loader\Configurator\CollectionConfigurator;
16+
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator as BaseRouteConfigurator;
17+
18+
trait AddTrait
19+
{
20+
/**
21+
* Adds a route.
22+
*
23+
* @param string|array $path the path, or the localized paths of the route
24+
*
25+
* @return RouteConfigurator
26+
*/
27+
public function add(string $name, $path): BaseRouteConfigurator
28+
{
29+
$parentConfigurator = $this instanceof CollectionConfigurator ? $this : ($this instanceof RouteConfigurator ? $this->parentConfigurator : null);
30+
$route = $this->createLocalizedRoute($this->collection, $name, $path, $this->name, $this->prefixes);
31+
32+
return new RouteConfigurator($this->collection, $route, $this->name, $parentConfigurator, $this->prefixes);
33+
}
34+
35+
/**
36+
* Adds a route.
37+
*
38+
* @param string|array $path the path, or the localized paths of the route
39+
*
40+
* @return RouteConfigurator
41+
*/
42+
final public function __invoke(string $name, $path): BaseRouteConfigurator
43+
{
44+
return $this->add($name, $path);
45+
}
46+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.