8000 [FrameworkBundle][Routing] added a Loader namespace to handle control… · symfony/symfony@7055ba0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7055ba0

Browse files
committed
[FrameworkBundle][Routing] added a Loader namespace to handle controllers
1 parent 4574f85 commit 7055ba0

25 files changed

+1062
-239
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CHANGELOG
1919
* Added support for Translator paths, Twig paths in translation commands.
2020
* Added support for PHP files with translations in translation commands.
2121
* Added support for boolean container parameters within routes.
22+
* Added `Routing\Loader` and `Routing\Loader\Configurator` namespaces to ease defining routes with default controllers
2223

2324
4.2.0
2425
-----

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515

1616
<service id="routing.resolver" class="Symfony\Component\Config\Loader\LoaderResolver" />
1717

18-
<service id="routing.loader.xml" class="Symfony\Component\Routing\Loader\XmlFileLoader">
18+
<service id="routing.loader.xml" class="Symfony\Bundle\FrameworkBundle\Routing\Loader\XmlFileLoader">
1919
<tag name="routing.loader" />
2020
<argument type="service" id="file_locator" />
2121
</service>
2222

23-
<service id="routing.loader.yml" class="Symfony\Component\Routing\Loader\YamlFileLoader">
23+
<service id="routing.loader.yml" class="Symfony\Bundle\FrameworkBundle\Routing\Loader\YamlFileLoader">
2424
<tag name="routing.loader" />
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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<xsd:schema xmlns="http://symfony.com/schema/routing"
4+
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
5+
targetNamespace="http://symfony.com/schema/routing"
6+
elementFormDefault="qualified">
7+
8+
<xsd:redefine schemaLocation="http://symfony.com/schema/routing/routing-1.0.xsd">
9+
<xsd:complexType name="routes">
10+
<xsd:complexContent>
11+
<xsd:extension base="routes">
12+
<xsd:choice minOccurs="0" maxOccurs="unbounded">
13+
<xsd:element name="import" type="import" />
14+
<xsd:element name="route" type="route" />
15+
<xsd:element name="template-route" type="template-route" />
16+
<xsd:element name="redirect-route" type="redirect-route" />
17+
<xsd:element name="url-redirect-route" type="url-redirect-route" />
18+
</xsd:choice>
19+
</xsd:extension>
20+
</xsd:complexContent>
21+
</xsd:complexType>
22+
</xsd:redefine>
23+
24+
<xsd:complexType name="template-route">
25+
<xsd:sequence>
26+
<xsd:group ref="configs" minOccurs="0" maxOccurs="unbounded" />
27+
<xsd:element name="path" type="localized-path" minOccurs="0" maxOccurs="unbounded" />
28+
</xsd:sequence>
29+
<xsd:attribute name="id" type="xsd:string" use="required" />
30+
<xsd:attribute name="path" type="xsd:string" />
31+
<xsd:attribute name="host" type="xsd:string" />
32+
<xsd:attribute name="schemes" type="xsd:string" />
33+
<xsd:attribute name="methods" type="xsd:string" />
34+
<xsd:attribute name="locale" type="xsd:string" />
35+
<xsd:attribute name="format" type="xsd:string" />
36+
<xsd:attribute name="utf8" type="xsd:boolean" />
37+
38+
<xsd:attribute name="template" type="xsd:string" />
39+
<xsd:attribute name="max-age" type="xsd:int" />
40+
<xsd:attribute name="shared-max-age" type="xsd:int" />
41+
<xsd:attribute name="private" type="xsd:boolean" />
42+
</xsd:complexType>
43+
44+
<xsd:complexType name="redirect-route">
45+
<xsd:sequence>
46+
<xsd:group ref="configs" minOccurs="0" maxOccurs="unbounded" />
47+
<xsd:element name="path" type="localized-path" minOccurs="0" maxOccurs="unbounded" />
48+
</xsd:sequence>
49+
<xsd:attribute name="id" type="xsd:string" use="required" />
50+
<xsd:attribute name="path" type="xsd:string" />
51+
<xsd:attribute name="host" type="xsd:string" />
52+
<xsd:attribute name="schemes" type="xsd:string" />
53+
<xsd:attribute name="methods" type="xsd:string" />
54+
<xsd:attribute name="locale" type="xsd:string" />
55+
<xsd:attribute name="format" type="xsd:string" />
56+
<xsd:attribute name="utf8" type="xsd:boolean" />
57+
58+
<xsd:attribute name="redirect-to-route" type="xsd:string" />
59+
<xsd:attribute name="permanent" type="xsd:boolean" />
60+
<xsd:attribute name="ignore-attributes" type="xsd:string" />
61+
<xsd:attribute name="keep-request-method" type="xsd:boolean" />
62+
<xsd:attribute name="keep-query-params" type="xsd:boolean" />
63+
</xsd:complexType>
64+
65+
<xsd:complexType name="url-redirect-route">
66+
<xsd:sequence>
67+
<xsd:group ref="configs" minOccurs="0" maxOccurs="unbounded" />
68+
<xsd:element name="path" type="localized-path" minOccurs="0" maxOccurs="unbounded" />
69+
</xsd:sequence>
70+
<xsd:attribute name="id" type="xsd:string" use="required" />
71+
<xsd:attribute name="path" type="xsd:string" />
72+
<xsd:attribute name="host" type="xsd:string" />
73+
<xsd:attribute name="methods" type="xsd:string" />
74+
<xsd:attribute name="locale" type="xsd:string" />
75+
<xsd:attribute name="format" type="xsd:string" />
76+
<xsd:attribute name="utf8" type="xsd:boolean" />
77+
78+
<xsd:attribute name="redirect-to-url" type="xsd:string" />
79+
<xsd:attribute name="permanent" type="xsd:boolean" />
80+
<xsd:attribute name="scheme" type="xsd:string" />
81+
<xsd:attribute name="http-port" type="xsd:int" />
82+
<xsd:attribute name="https-port" type="xsd:int" />
83+
<xsd:attribute name="keep-request-method" type="xsd:boolean" />
84+
</xsd:complexType>
85+
86+
</xsd:schema>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Component\Routing\Loader\Configurator\RouteConfigurator;
15+
16+
/**
17+
* @author Jules Pietri <jules@heahprod.com>
18+
*/
19+
class RedirectRouteConfigurator extends RouteConfigurator
20+
{
21+
/**
22+
* @param bool $permanent Whether the redirection is permanent
23+
*
24+
* @return $this
25+
*/
26+
final public function permanent(bool $permanent = true)
27+
{
28+
return $this->defaults(['permanent' => $permanent]);
29+
}
30+
31+
/**
32+
* @param bool|array $ignoreAttributes Whether to ignore attributes or an array of attributes to ignore
33+
*
34+
* @return $this
35+
*/
36+
final public function ignoreAttributes($ignoreAttributes = true)
37+
{
38+
return $this->defaults(['ignoreAttributes' => $ignoreAttributes]);
39+
}
40+
41+
/**
42+
* @param bool $keepRequestMethod Whether redirect action should keep HTTP request method
43+
*
44+
* @return $this
45+
*/
46+
final public function keepRequestMethod(bool $keepRequestMethod = true)
47+
{
48+
return $this->defaults(['keepRequestMethod' => $keepRequestMethod]);
49+
}
50+
51+
/**
52+
* @param bool $keepQueryParams Whether redirect action should keep query parameters
53+
*
54+
* @return $this
55+
*/
56+
final public function keepQueryParams(bool $keepQueryParams = true)
57+
{
58+
return $this->defaults(['keepQueryParams' => $keepQueryParams]);
59+
}
60+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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\RoutingConfigurator as BaseRoutingConfigurator;
17+
use Symfony\Component\Routing\Loader\PhpFileLoader;
18+
use Symfony\Component\Routing\RouteCollection;
19+
20+
/**
21+
* @author Jules Pietri <jules@heahprod.com>
22+
*/
23+
class RoutingConfigurator extends BaseRoutingConfigurator
24+
{
25+
private $collection;
26+
27+
public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file)
28+
{
29+
parent::__construct($collection, $loader, $path, $file);
30+
31+
$this->collection = $collection;
32+
}
33+
34+
/**
35+
* @param string|array $path The path, or the localized paths of the route
36+
* @param string $template The template name
37+
*/
38+
final public function template(string $name, $path, string $template): TemplateRouteConfigurator
39+
{
40+
return new TemplateRouteConfigurator(...$this->getRouteConfiguratorArguments($name, $path, [
41+
'template' => $template,
42+
'_controller' => TemplateController::class,
43+
]));
44+
}
45+
46+
/**
47+
* @param string|array $path The path, or the localized paths of the route
48+
* @param string $route The route name to redirect to
49+
*/
50+
final public function redirectToRoute(string $name, $path, string $route): RedirectRouteConfigurator
51+
{
52+
return new RedirectRouteConfigurator(...$this->getRouteConfiguratorArguments($name, $path, [
53+
'route' => $route,
54+
'_controller' => RedirectController::class.'::redirectAction',
55+
]));
56+
}
57+
58+
/**
59+
* @param string|array $path The path, or the localized paths of the route
60+
* @param string $url The absolute path or URL to redirect to
61+
*/
62+
final public function redirectToUrl(string $name, $path, string $url): UrlRedirectRouteConfigurator
63+
{
64+
return new UrlRedirectRouteConfigurator(...$this->getRouteConfiguratorArguments($name, $path, [
65+
'path' => $url,
66+
'_controller' => RedirectController::class.'::urlRedirectAction',
67+
]));
68+
}
69+
70+
private function getRouteConfiguratorArguments(string $name, $path, array $defaults): array
71+
{
72+
$route = $this->createLocalizedRoute($this->collection, $name, $path);
73+
$route->addDefaults($defaults);
74+
75+
return [$this->collection, $route];
76+
}
77+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Component\Routing\Loader\Configurator\RouteConfigurator;
15+
16+
/**
17+
* @author Jules Pietri <jules@heahprod.com>
18+
*/
19+
class TemplateRouteConfigurator extends RouteConfigurator
20+
{
21+
/**
22+
* @param int|null $maxAge Max age for client caching
23+
*
24+
* @return $this
25+
*/
26+
final public function maxAge(?int $maxAge)
27+
{
28+
return $this->defaults(['maxAge' => $maxAge]);
29+
}
30+
31+
/**
32+
* @param int|null $sharedMaxAge Max age for shared (proxy) caching
33+
*
34+
* @return $this
35+
*/
36+
final public function sharedMaxAge(?int $sharedMaxAge)
37+
{
38+
return $this->defaults(['sharedAge' => $sharedMaxAge]);
39+
}
40+
41+
/**
42+
* @param bool|null $private Whether or not caching should apply for client caches only
43+
*
44+
* @return $this
45+
*/
46+
final public function private(?bool $private = true)
47+
{
48+
return $this->defaults(['private' => $private]);
49+
}
50+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\Component\Routing\Loader\Configurator\RouteConfigurator;
15+
16+
/**
17+
* @author Jules Pietri <jules@heahprod.com>
18+
*/
19+
class UrlRedirectRouteConfigurator extends RouteConfigurator
20+
{
21+
/**
22+
* @param bool $permanent Whether the redirection is permanent
23+
*
24+
* @return $this
25+
*/
26+
final public function permanent(bool $permanent = true)
27+
{
28+
return $this->defaults(['permanent' => $permanent]);
29+
}
30+
31+
/**
32+
* @param string|null $scheme The URL scheme (null to keep the current one)
33+
*
34+
* @return $this
35+
*/
36+
final public function scheme(?string $scheme)
37+
{
38+
return $this->defaults(['scheme' => $scheme]);
39+
}
40+
41+
/**
42+
* @param int|null $httpPort The HTTP port (null to keep the current one for the same scheme or the default configured port)
43+
*
44+
* @return $this
45+
*/
46+
final public function httpPort(?int $httpPort)
47+
{
48+
return $this->defaults(['httpPort' => $httpPort]);
49+
}
50+
51+
/**
52+
* @param int|null $httpsPort The HTTPS port (null to keep the current one for the same scheme or the default configured port)
53+
*
54+
* @return $this
55+
*/
56+
final public function httpsPort(?int $httpsPort)
57+
{
58+
return $this->defaults(['httpsPort' => $httpsPort]);
59+
}
60+
61+
/**
62+
* @param bool $keepRequestMethod Whether redirect action should keep HTTP request method
63+
*
64+
* @return $this
65+
*/
66+
final public function keepRequestMethod(bool $keepRequestMethod = true)
67+
{
68+
return $this->defaults(['keepRequestMethod' => $keepRequestMethod]);
69+
}
70+
}

0 commit comments

Comments
 (0)
0