Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit6d7415f

Browse files
committed
Updated routing articles to Symfony 4
1 parente92386c commit6d7415f

12 files changed

+167
-151
lines changed

‎routing/conditions.rst‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,37 @@ define arbitrary matching logic, use the ``conditions`` routing option:
1212

1313
..code-block::yaml
1414
15+
# config/routes.yaml
1516
contact:
1617
path:/contact
17-
defaults:{ _controller:AcmeDemoBundle:Main:contact }
18+
defaults:{ _controller:'App\Controller\DefaultController::contact' }
1819
condition:"context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'"
1920
2021
..code-block::xml
2122
23+
<!-- config/routes.xml-->
2224
<?xml version="1.0" encoding="UTF-8" ?>
2325
<routesxmlns="http://symfony.com/schema/routing"
2426
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2527
xsi:schemaLocation="http://symfony.com/schema/routing
2628
http://symfony.com/schema/routing/routing-1.0.xsd">
2729
2830
<routeid="contact"path="/contact">
29-
<defaultkey="_controller">AcmeDemoBundle:Main:contact</default>
31+
<defaultkey="_controller">App\Controller\DefaultController::contact</default>
3032
<condition>context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'</condition>
3133
</route>
3234
</routes>
3335
3436
..code-block::php
3537
38+
// config/routes.php
3639
use Symfony\Component\Routing\RouteCollection;
3740
use Symfony\Component\Routing\Route;
3841
3942
$collection = new RouteCollection();
4043
$collection->add('contact', new Route(
4144
'/contact', array(
42-
'_controller' => 'AcmeDemoBundle:Main:contact',
45+
'_controller' => 'App\Controller\DefaultController::contact',
4346
),
4447
array(),
4548
array(),

‎routing/custom_route_loader.rst‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ and therefore have two important methods:
3636
:method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::supports`
3737
and:method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::load`.
3838

39-
Take these lines from the ``routing.yml`` in the Symfony Standard Edition:
39+
Take these lines from the ``routes.yaml`` in the Symfony Standard Edition:
4040

4141
..code-block::yaml
4242
4343
# config/routes.yaml
44-
app:
45-
resource:'@AppBundle/Controller/'
46-
type:annotation
44+
controllers:
45+
resource:../src/Controller/
46+
type:annotation
4747
4848
When the main loader parses this, it tries all registered delegate loaders and calls
4949
their:method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::supports`
50-
method with the given resource (``@AppBundle/Controller/``)
50+
method with the given resource (``../src/Controller/``)
5151
and type (``annotation``) as arguments. When one of the loader returns ``true``,
5252
its:method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::load` method
5353
will be called, which should return a:class:`Symfony\\Component\\Routing\\RouteCollection`
@@ -56,7 +56,7 @@ containing :class:`Symfony\\Component\\Routing\\Route` objects.
5656
..note::
5757

5858
Routes loaded this way will be cached by the Router the same way as
59-
when they are defined in one of the default formats (e.g. XML,YML,
59+
when they are defined in one of the default formats (e.g. XML,YAML,
6060
PHP file).
6161

6262
Creating a custom Loader
@@ -97,7 +97,7 @@ you do. The resource name itself is not actually used in the example::
9797
// prepare a new route
9898
$path = '/extra/{parameter}';
9999
$defaults = array(
100-
'_controller' => 'AppBundle:Extra:extra',
100+
'_controller' => 'App\Controller\ExtraController::extra',
101101
);
102102
$requirements = array(
103103
'parameter' => '\d+',
@@ -120,8 +120,7 @@ you do. The resource name itself is not actually used in the example::
120120
}
121121

122122
Make sure the controller you specify really exists. In this case you
123-
have to create an ``extraAction()`` method in the ``ExtraController``
124-
of the ``AppBundle``::
123+
have to create an ``extra()`` method in the ``ExtraController``::
125124

126125
// src/Controller/ExtraController.php
127126
namespace App\Controller;
@@ -131,7 +130,7 @@ of the ``AppBundle``::
131130

132131
class ExtraController extends Controller
133132
{
134-
public functionextraAction($parameter)
133+
public functionextra($parameter)
135134
{
136135
return new Response($parameter);
137136
}
@@ -152,6 +151,7 @@ Now define a service for the ``ExtraLoader``:
152151
153152
..code-block::xml
154153
154+
<!-- config/services.xml-->
155155
<?xml version="1.0" ?>
156156
<containerxmlns="http://symfony.com/schema/dic/services"
157157
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -169,6 +169,7 @@ Now define a service for the ``ExtraLoader``:
169169
170170
..code-block::php
171171
172+
// config/services.php
172173
use App\Routing\ExtraLoader;
173174
174175
$container
@@ -198,6 +199,7 @@ What remains to do is adding a few lines to the routing configuration:
198199
199200
..code-block::xml
200201
202+
<!-- config/routes.xml-->
201203
<?xml version="1.0" encoding="UTF-8" ?>
202204
<routesxmlns="http://symfony.com/schema/routing"
203205
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -217,10 +219,10 @@ What remains to do is adding a few lines to the routing configuration:
217219
218220
return $collection;
219221
220-
The important part here is the ``type`` key. Its value should be"extra" as
222+
The important part here is the ``type`` key. Its value should be``extra`` as
221223
this is the type which the ``ExtraLoader`` supports and this will make sure
222224
its ``load()`` method gets called. The ``resource`` key is insignificant
223-
for the ``ExtraLoader``, so it is set to".".
225+
for the ``ExtraLoader``, so it is set to``.`` (a single dot).
224226

225227
..note::
226228

@@ -256,7 +258,7 @@ configuration file - you can call the
256258
{
257259
$collection = new RouteCollection();
258260

259-
$resource = '@AppBundle/Resources/config/import_routing.yml';
261+
$resource = '@ThirdPartyBundle/Resources/config/routing.yaml';
260262
$type = 'yaml';
261263

262264
$importedRoutes = $this->import($resource, $type);

‎routing/external_resources.rst‎

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ This can be done by "importing" directories into the routing configuration:
1414
..code-block::yaml
1515
1616
# config/routes.yaml
17-
app:
18-
resource:'@AppBundle/Controller/'
19-
type:annotation# required to enable the Annotation reader for this resource
17+
controllers:
18+
resource:../src/Controller/
19+
type:annotation
2020
2121
..code-block::xml
2222
@@ -27,8 +27,7 @@ This can be done by "importing" directories into the routing configuration:
2727
xsi:schemaLocation="http://symfony.com/schema/routing
2828
http://symfony.com/schema/routing/routing-1.0.xsd">
2929
30-
<!-- the type is required to enable the annotation reader for this resource-->
31-
<importresource="@AppBundle/Controller/"type="annotation"/>
30+
<importresource="../src/Controller/"type="annotation"/>
3231
</routes>
3332
3433
..code-block::php
@@ -38,35 +37,31 @@ This can be done by "importing" directories into the routing configuration:
3837
3938
$collection = new RouteCollection();
4039
$collection->addCollection(
41-
// second argument is the type, which is required to enable
42-
// the annotation reader for this resource
43-
$loader->import("@AppBundle/Controller/", "annotation")
40+
$loader->import("../src/Controller/", "annotation")
4441
);
4542
4643
return $collection;
4744
4845
..note::
4946

50-
When importing resources from YAML, the key (e.g. ``app``) is meaningless.
47+
When importing resources from YAML, the key (e.g. ``controllers``) is meaningless.
5148
Just be sure that it's unique so no other lines override it.
5249

5350
The ``resource`` key loads the given routing resource. In this example the
54-
resource is a directory, where the ``@AppBundle`` shortcut syntax resolves
55-
to the full path of the AppBundle. When pointing to a directory, all files
56-
in that directory are parsed and put into the routing.
51+
resource is a directory and all files in that directory are parsed and put into
52+
the routing.
5753

5854
..note::
5955

60-
You can also include other routing configuration files, this is often
61-
used to import the routing of third party bundles:
56+
You can also include other routing configuration files:
6257

6358
..configuration-block::
6459

6560
..code-block::yaml
6661
6762
# config/routes.yaml
6863
app:
69-
resource:'@AcmeOtherBundle/Resources/config/routing.yml'
64+
resource:'@ThirdPartyBundle/Resources/config/routing.yaml'
7065
7166
..code-block::xml
7267
@@ -77,7 +72,7 @@ in that directory are parsed and put into the routing.
7772
xsi:schemaLocation="http://symfony.com/schema/routing
7873
http://symfony.com/schema/routing/routing-1.0.xsd">
7974
80-
<importresource="@AcmeOtherBundle/Resources/config/routing.xml" />
75+
<importresource="@ThirdPartyBundle/Resources/config/routing.xml" />
8176
</routes>
8277
8378
..code-block::php
@@ -87,7 +82,7 @@ in that directory are parsed and put into the routing.
8782
8883
$collection = new RouteCollection();
8984
$collection->addCollection(
90-
$loader->import("@AcmeOtherBundle/Resources/config/routing.php")
85+
$loader->import("@ThirdPartyBundle/Resources/config/routing.php")
9186
);
9287
9388
return $collection;
@@ -96,16 +91,16 @@ Prefixing Imported Routes
9691
~~~~~~~~~~~~~~~~~~~~~~~~~
9792

9893
You can also choose to provide a "prefix" for the imported routes. For example,
99-
suppose you want to prefix allroutes in the AppBundle with ``/site`` (e.g.
94+
suppose you want to prefix allapplication routes with ``/site`` (e.g.
10095
``/site/blog/{slug}`` instead of ``/blog/{slug}``):
10196

10297
..configuration-block::
10398

10499
..code-block::yaml
105100
106101
# config/routes.yaml
107-
app:
108-
resource:'@AppBundle/Controller/'
102+
controllers:
103+
resource:'../src/Controller/'
109104
type:annotation
110105
prefix:/site
111106
@@ -119,7 +114,7 @@ suppose you want to prefix all routes in the AppBundle with ``/site`` (e.g.
119114
http://symfony.com/schema/routing/routing-1.0.xsd">
120115
121116
<import
122-
resource="@AppBundle/Controller/"
117+
resource="../src/Controller/"
123118
type="annotation"
124119
prefix="/site" />
125120
</routes>
@@ -129,7 +124,7 @@ suppose you want to prefix all routes in the AppBundle with ``/site`` (e.g.
129124
// config/routes.php
130125
use Symfony\Component\Routing\RouteCollection;
131126
132-
$app = $loader->import('@AppBundle/Controller/', 'annotation');
127+
$app = $loader->import('../src/Controller/', 'annotation');
133128
$app->addPrefix('/site');
134129
135130
$collection = new RouteCollection();

‎routing/extra_information.rst‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ to your controller, and as attributes of the ``Request`` object:
1717
blog:
1818
path:/blog/{page}
1919
defaults:
20-
_controller:AppBundle:Blog:index
20+
_controller:App\Controller\BlogController::index
2121
page:1
2222
title:"Hello world!"
2323
@@ -31,7 +31,7 @@ to your controller, and as attributes of the ``Request`` object:
3131
http://symfony.com/schema/routing/routing-1.0.xsd">
3232
3333
<routeid="blog"path="/blog/{page}">
34-
<defaultkey="_controller">AppBundle:Blog:index</default>
34+
<defaultkey="_controller">App\Controller\BlogController::index</default>
3535
<defaultkey="page">1</default>
3636
<defaultkey="title">Hello world!</default>
3737
</route>
@@ -45,7 +45,7 @@ to your controller, and as attributes of the ``Request`` object:
4545
4646
$collection = new RouteCollection();
4747
$collection->add('blog', new Route('/blog/{page}', array(
48-
'_controller' => 'AppBundle:Blog:index',
48+
'_controller' => 'App\Controller\BlogController::index',
4949
'page' => 1,
5050
'title' => 'Hello world!',
5151
)));
@@ -55,16 +55,16 @@ to your controller, and as attributes of the ``Request`` object:
5555
Now, you can access this extra parameter in your controller, as an argument
5656
to the controller method::
5757

58-
public functionindexAction($page, $title)
58+
public functionindex($page, $title)
5959
{
6060
// ...
6161
}
6262

6363
Alternatively, the title could be accessed through the ``Request`` object::
6464

6565
use Symfony\Component\HttpFoundation\Request;
66-
67-
public functionindexAction(Request $request, $page)
66+
67+
public functionindex(Request $request, $page)
6868
{
6969
$title = $request->attributes->get('title');
7070

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp