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

Commitea881b8

Browse files
committed
minor#5570 Quick review of 'create framework' tutorial (WouterJ)
This PR was merged into the 2.3 branch.Discussion----------Quick review of 'create framework' tutorialCommits-------3f8e61d Apply suggestions from the comments613d6bc Use underscores in file nameece717e Quick review of 'create framework' tutorial
2 parentsed5a02a +3f8e61d commitea881b8

15 files changed

+92
-115
lines changed

‎create_framework/dependency-injection.rst‎renamed to ‎create_framework/dependency_injection.rst‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ empty class, you might be tempted to move some code from the front controller
77
to it::
88

99
// example.com/src/Simplex/Framework.php
10-
1110
namespace Simplex;
1211

1312
use Symfony\Component\Routing;
@@ -33,7 +32,6 @@ to it::
3332
The front controller code would become more concise::
3433

3534
// example.com/web/front.php
36-
3735
require_once __DIR__.'/../vendor/autoload.php';
3836

3937
use Symfony\Component\HttpFoundation\Request;
@@ -94,7 +92,6 @@ container:
9492
Create a new file to host the dependency injection container configuration::
9593

9694
// example.com/src/container.php
97-
9895
use Symfony\Component\DependencyInjection;
9996
use Symfony\Component\DependencyInjection\Reference;
10097

@@ -147,7 +144,6 @@ it in other object definitions.
147144
The front controller is now only about wiring everything together::
148145

149146
// example.com/web/front.php
150-
151147
require_once __DIR__.'/../vendor/autoload.php';
152148

153149
use Symfony\Component\HttpFoundation\Request;
@@ -165,7 +161,6 @@ As all the objects are now created in the dependency injection container, the
165161
framework code should be the previous simple version::
166162

167163
// example.com/src/Simplex/Framework.php
168-
169164
namespace Simplex;
170165

171166
use Symfony\Component\HttpKernel\HttpKernel;

‎create_framework/event-dispatcher.rst‎renamed to ‎create_framework/event_dispatcher.rst‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ To make it work, the framework must dispatch an event just before returning
3434
the Response instance::
3535

3636
// example.com/src/Simplex/Framework.php
37-
3837
namespace Simplex;
3938

4039
use Symfony\Component\HttpFoundation\Request;
@@ -46,9 +45,9 @@ the Response instance::
4645

4746
class Framework
4847
{
49-
protected $matcher;
50-
protected $resolver;
51-
protected $dispatcher;
48+
private $matcher;
49+
private $resolver;
50+
private $dispatcher;
5251

5352
public function __construct(EventDispatcher $dispatcher, UrlMatcherInterface $matcher, ControllerResolverInterface $resolver)
5453
{
@@ -85,7 +84,6 @@ Each time the framework handles a Request, a ``ResponseEvent`` event is
8584
now dispatched::
8685

8786
// example.com/src/Simplex/ResponseEvent.php
88-
8987
namespace Simplex;
9088

9189
use Symfony\Component\HttpFoundation\Request;
@@ -118,7 +116,6 @@ The last step is the creation of the dispatcher in the front controller and
118116
the registration of a listener for the ``response`` event::
119117

120118
// example.com/web/front.php
121-
122119
require_once __DIR__.'/../vendor/autoload.php';
123120

124121
// ...
@@ -197,7 +194,6 @@ the priority to ``-255``::
197194
Let's refactor the code a bit by moving the Google listener to its own class::
198195

199196
// example.com/src/Simplex/GoogleListener.php
200-
201197
namespace Simplex;
202198

203199
class GoogleListener
@@ -220,7 +216,6 @@ Let's refactor the code a bit by moving the Google listener to its own class::
220216
And do the same with the other listener::
221217

222218
// example.com/src/Simplex/ContentLengthListener.php
223-
224219
namespace Simplex;
225220

226221
class ContentLengthListener
@@ -259,7 +254,6 @@ information to the dispatcher via the ``getSubscribedEvents()`` method. Have a
259254
look at the new version of the ``GoogleListener``::
260255

261256
// example.com/src/Simplex/GoogleListener.php
262-
263257
namespace Simplex;
264258

265259
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -277,7 +271,6 @@ look at the new version of the ``GoogleListener``::
277271
And here is the new version of ``ContentLengthListener``::
278272

279273
// example.com/src/Simplex/ContentLengthListener.php
280-
281274
namespace Simplex;
282275

283276
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

‎create_framework/front-controller.rst‎renamed to ‎create_framework/front_controller.rst‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ spice things up a little bit, let's go crazy and add another page that says
66
goodbye::
77

88
// framework/bye.php
9-
109
require_once __DIR__.'/vendor/autoload.php';
1110

1211
use Symfony\Component\HttpFoundation\Request;
@@ -26,7 +25,6 @@ The PHP way of doing the refactoring would probably be the creation of an
2625
include file::
2726

2827
// framework/init.php
29-
3028
require_once __DIR__.'/vendor/autoload.php';
3129

3230
use Symfony\Component\HttpFoundation\Request;
@@ -38,7 +36,6 @@ include file::
3836
Let's see it in action::
3937

4038
// framework/index.php
41-
4239
require_once __DIR__.'/init.php';
4340

4441
$input = $request->get('name', 'World');
@@ -49,7 +46,6 @@ Let's see it in action::
4946
And for the "Goodbye" page::
5047

5148
// framework/bye.php
52-
5349
require_once __DIR__.'/init.php';
5450

5551
$response->setContent('Goodbye!');
@@ -76,7 +72,6 @@ routing all client requests to a single PHP script.
7672
Such a script might look like the following::
7773

7874
// framework/front.php
79-
8075
require_once __DIR__.'/vendor/autoload.php';
8176

8277
use Symfony\Component\HttpFoundation\Request;
@@ -103,7 +98,6 @@ Such a script might look like the following::
10398
And here is for instance the new ``hello.php`` script::
10499

105100
// framework/hello.php
106-
107101
$input = $request->get('name', 'World');
108102
$response->setContent(sprintf('Hello %s', htmlspecialchars($input, ENT_QUOTES, 'UTF-8')));
109103

@@ -194,15 +188,13 @@ the ``setContent()`` directly from the front controller script::
194188
And the ``hello.php`` script can now be converted to a template::
195189

196190
<!-- example.com/src/pages/hello.php -->
197-
198191
<?php $name = $request->get('name', 'World') ?>
199192

200193
Hello <?php echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?>
201194

202195
We have the first version of our framework::
203196

204197
// example.com/web/front.php
205-
206198
require_once __DIR__.'/../vendor/autoload.php';
207199

208200
use Symfony\Component\HttpFoundation\Request;

‎create_framework/http-foundation.rst‎renamed to ‎create_framework/http_foundation.rst‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Even if the "application" we wrote in the previous chapter was simple enough,
1818
it suffers from a few problems::
1919

2020
// framework/index.php
21-
2221
$input = $_GET['name'];
2322

2423
printf('Hello %s', $input);
@@ -27,7 +26,6 @@ First, if the ``name`` query parameter is not defined in the URL query string,
2726
you will get a PHP warning; so let's fix it::
2827

2928
// framework/index.php
30-
3129
$input = isset($_GET['name']) ? $_GET['name'] : 'World';
3230

3331
printf('Hello %s', $input);
@@ -60,7 +58,6 @@ snippet of PHP code is not natural and feels ugly. Here is a tentative PHPUnit
6058
unit test for the above code::
6159

6260
// framework/test.php
63-
6461
class IndexTest extends \PHPUnit_Framework_TestCase
6562
{
6663
public function testHello()
@@ -147,7 +144,6 @@ Now, let's rewrite our application by using the ``Request`` and the
147144
``Response`` classes::
148145

149146
// framework/index.php
150-
151147
require_once __DIR__.'/vendor/autoload.php';
152148

153149
use Symfony\Component\HttpFoundation\Request;
@@ -227,7 +223,7 @@ With the ``Response`` class, you can easily tweak the response::
227223

228224
..tip::
229225

230-
To debug aResponse, cast it to a string; it will return the HTTP
226+
To debug aresponse, cast it to a string; it will return the HTTP
231227
representation of the response (headers and content).
232228

233229
Last but not the least, these classes, like every other class in the Symfony
@@ -239,7 +235,7 @@ framework?
239235

240236
Even something as simple as getting the client IP address can be insecure::
241237

242-
if ($myIp == $_SERVER['REMOTE_ADDR']) {
238+
if ($myIp === $_SERVER['REMOTE_ADDR']) {
243239
// the client is a known one, so give it some more privilege
244240
}
245241

@@ -248,7 +244,7 @@ production servers; at this point, you will have to change your code to make
248244
it work on both your development machine (where you don't have a proxy) and
249245
your servers::
250246

251-
if ($myIp == $_SERVER['HTTP_X_FORWARDED_FOR'] || $myIp == $_SERVER['REMOTE_ADDR']) {
247+
if ($myIp === $_SERVER['HTTP_X_FORWARDED_FOR'] || $myIp=== $_SERVER['REMOTE_ADDR']) {
252248
// the client is a known one, so give it some more privilege
253249
}
254250

@@ -258,7 +254,7 @@ chained proxies)::
258254

259255
$request = Request::createFromGlobals();
260256

261-
if ($myIp == $request->getClientIp()) {
257+
if ($myIp === $request->getClientIp()) {
262258
// the client is a known one, so give it some more privilege
263259
}
264260

@@ -271,7 +267,7 @@ explicitly trust your reverse proxies by calling ``setTrustedProxies()``::
271267

272268
Request::setTrustedProxies(array('10.0.0.1'));
273269

274-
if ($myIp == $request->getClientIp(true)) {
270+
if ($myIp === $request->getClientIp(true)) {
275271
// the client is a known one, so give it some more privilege
276272
}
277273

‎create_framework/http-kernel-controller-resolver.rst‎renamed to ‎create_framework/http_kernel_controller_resolver.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ a Request object. All controller resolvers implement the following interface::
5050

5151
namespace Symfony\Component\HttpKernel\Controller;
5252

53+
// ...
5354
interface ControllerResolverInterface
5455
{
5556
function getController(Request $request);
@@ -151,7 +152,6 @@ method is not defined, an argument has no matching attribute, ...).
151152
Let's conclude with the new version of our framework::
152153

153154
// example.com/web/front.php
154-
155155
require_once __DIR__.'/../vendor/autoload.php';
156156

157157
use Symfony\Component\HttpFoundation\Request;

‎create_framework/http-kernel-httpkernel-class.rst‎renamed to ‎create_framework/http_kernel_httpkernel_class.rst‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ feedback when a problem arises.
2323
Here is the new framework code::
2424

2525
// example.com/src/Simplex/Framework.php
26-
2726
namespace Simplex;
2827

2928
use Symfony\Component\HttpKernel\HttpKernel;
@@ -35,7 +34,6 @@ Here is the new framework code::
3534
And the new front controller::
3635

3736
// example.com/web/front.php
38-
3937
require_once __DIR__.'/../vendor/autoload.php';
4038

4139
use Symfony\Component\HttpFoundation\Request;
@@ -79,13 +77,14 @@ thrown ``Exception`` instance to ease exception manipulation and display. It
7977
can take any valid controller as an exception handler, so you can create an
8078
ErrorController class instead of using a Closure::
8179

82-
$listener = new HttpKernel\EventListener\ExceptionListener('Calendar\\Controller\\ErrorController::exceptionAction');
80+
$listener = new HttpKernel\EventListener\ExceptionListener(
81+
'Calendar\\Controller\\ErrorController::exceptionAction'
82+
);
8383
$dispatcher->addSubscriber($listener);
8484

8585
The error controller reads as follows::
8686

8787
// example.com/src/Calendar/Controller/ErrorController.php
88-
8988
namespace Calendar\Controller;
9089

9190
use Symfony\Component\HttpFoundation\Response;
@@ -148,7 +147,6 @@ is to convert the controller return value to a proper Response instance, but
148147
only if needed::
149148

150149
// example.com/src/Simplex/StringResponseListener.php
151-
152150
namespace Simplex;
153151

154152
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp