Skip to content

Commit 9df330d

Browse files
committed
Making request not a service
1 parent 7684e54 commit 9df330d

File tree

4 files changed

+40
-50
lines changed

4 files changed

+40
-50
lines changed

_tuts/pimple-dic.diff

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ index aee9a74..9642f7c 100644
7878
"version": "v2.5.6",
7979
"target-dir": "Symfony/Component/HttpFoundation",
8080
diff --git a/index.php b/index.php
81-
index d8c644d..646ee16 100644
81+
index d8c644d..e64fbc5 100644
8282
--- a/index.php
8383
+++ b/index.php
84-
@@ -1,41 +1,56 @@
84+
@@ -1,30 +1,43 @@
8585
<?php
8686
require __DIR__.'/bootstrap.php';
8787

@@ -97,7 +97,9 @@ index d8c644d..646ee16 100644
9797
use Symfony\Component\HttpFoundation\Response;
9898
use Aura\Router\RouterFactory;
9999
+use Pimple\Container;
100-
+
100+
101+
-$request = Request::createFromGlobals();
102+
-$uri = $request->getPathInfo();
101103
+$c = new Container();
102104
+
103105
+// configuration
@@ -107,58 +109,49 @@ index d8c644d..646ee16 100644
107109
+$c['connection'] = function(Container $c) {
108110
+ return new PDO($c['connection_string']);
109111
+};
110-
+
111-
+$c['request'] = function() {
112-
+ return Request::createFromGlobals();
113-
+};
114-
+
112+
113+
-$routerFactory = new RouterFactory();
114+
-$router = $routerFactory->newInstance();
115115
+$c['router'] = function() {
116116
+ $routerFactory = new RouterFactory();
117117
+
118118
+ $router = $routerFactory->newInstance();
119-
120-
-$request = Request::createFromGlobals();
121-
-$uri = $request->getPathInfo();
119+
+
122120
+ // create a router, build the routes, and then execute it
123121
+ $router->add('attendees_list', '/attendees')
124122
+ ->addValues(['controller' => 'attendees_controller']);
125123
+ $router->add('homepage', '{/name}')
126124
+ ->addValues(['controller' => 'homepage_controller']);
127-
128-
-$routerFactory = new RouterFactory();
129-
-$router = $routerFactory->newInstance();
125+
+
130126
+ return $router;
131127
+};
128+
+
129+
+// run the framework!
130+
+$request = Request::createFromGlobals();
132131

133132
-// create a router, build the routes, and then execute it
134133
-$router->add('attendees_list', '/attendees')
135134
- ->addValues(['controller' => 'attendees_controller']);
136135
-$router->add('homepage', '{/name}')
137136
- ->addValues(['controller' => 'homepage_controller']);
138137
-$route = $router->match($uri, $request->server->all());
139-
+// run the framework!
140138
+$route = $c['router']->match(
141-
+ $c['request']->getPathInfo(),
142-
+ $c['request']->server->all()
139+
+ $request->getPathInfo(),
140+
+ $request->server->all()
143141
+);
144142

145143
// merge the matched attributes back into Symfony's request
146144
if ($route) {
147-
- $request->attributes->add($route->params);
148-
+ $c['request']->attributes->add($route->params);
149-
}
150-
151-
// get the "controller" out, or default to error404_controller
152-
-$controller = $request->attributes->get('controller', 'error404_controller');
153-
+$controller = $c['request']->attributes->get('controller', 'error404_controller');
145+
@@ -35,7 +48,7 @@ if ($route) {
146+
$controller = $request->attributes->get('controller', 'error404_controller');
154147

155148
// execute the controller and get the response
156149
-$response = call_user_func_array($controller, array($request));
157-
+$response = call_user_func_array($controller, array($c['request'], $c));
150+
+$response = call_user_func_array($controller, array($request, $c));
158151
if (!$response instanceof Response) {
159152
throw new Exception(sprintf('Your controller "%s" did not return a response!!', $controller));
160153
}
161-
@@ -56,8 +71,8 @@ function homepage_controller(Request $request) {
154+
@@ -56,8 +69,8 @@ function homepage_controller(Request $request) {
162155
return new Response($content);
163156
}
164157

_tuts/reorganization.diff

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ index 0000000..9adb562
146146
+ return $response;
147147
+}
148148
diff --git a/index.php b/index.php
149-
index 919eee1..2064c68 100644
149+
index 0add68c..2064c68 100644
150150
--- a/index.php
151151
+++ b/index.php
152-
@@ -1,121 +1,9 @@
152+
@@ -1,119 +1,9 @@
153153
<?php
154154
-require __DIR__.'/bootstrap.php';
155155

@@ -172,10 +172,6 @@ index 919eee1..2064c68 100644
172172
- return new PDO($c['connection_string']);
173173
-};
174174
-
175-
-$c['request'] = function() {
176-
- return Request::createFromGlobals();
177-
-};
178-
-
179175
-$c['router'] = function() {
180176
- $routerFactory = new RouterFactory();
181177
-
@@ -186,7 +182,7 @@ index 919eee1..2064c68 100644
186182
- ->addValues(['controller' => 'attendees_controller']);
187183
- $router->add('homepage', '{/name}')
188184
- ->addValues(['controller' => 'homepage_controller']);
189-
-
185+
190186
- return $router;
191187
-};
192188
-$c['logger_writer'] = function(Container $c) {
@@ -198,21 +194,24 @@ index 919eee1..2064c68 100644
198194
-
199195
- return $logger;
200196
-};
201-
-
197+
+require __DIR__.'/bootstrap.php';
198+
202199
-// run the framework!
200+
$request = Request::createFromGlobals();
201+
-
203202
-$route = $c['router']->match(
204-
- $c['request']->getPathInfo(),
205-
- $c['request']->server->all()
203+
- $request->getPathInfo(),
204+
- $request->server->all()
206205
-);
207206
-
208207
-// merge the matched attributes back into Symfony's request
209208
-if ($route) {
210-
- $c['request']->attributes->add($route->params);
209+
- $request->attributes->add($route->params);
211210
-}
212211
-
213212
-// get the "controller" out, or default to error404_controller
214-
-$controller = $c['request']->attributes->get('controller', 'error404_controller');
215-
213+
-$controller = $request->attributes->get('controller', 'error404_controller');
214+
-
216215
-if ($controller == 'error404_controller') {
217216
- $msg = sprintf('Controller not found for "%s"', $c['request']->getPathInfo());
218217
- $c['logger']->err($msg);
@@ -221,13 +220,11 @@ index 919eee1..2064c68 100644
221220
-}
222221
-
223222
-// execute the controller and get the response
224-
-$response = call_user_func_array($controller, array($c['request'], $c));
223+
-$response = call_user_func_array($controller, array($request, $c));
225224
-if (!$response instanceof Response) {
226225
- throw new Exception(sprintf('Your controller "%s" did not return a response!!', $controller));
227226
-}
228-
+require __DIR__.'/bootstrap.php';
229-
230-
+$request = Request::createFromGlobals();
227+
-
231228
+$response = _run_app($request);
232229
$response->send();
233230
-

_tuts/steps.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@
3636
"description": null
3737
}
3838
],
39-
"sha": "0b4952f9ef651c719d8d9fe5f36ac8de77c8bba5"
39+
"sha": "7684e5491e70ed2cd45848f366a3bbc9c5ad6c80"
4040
}

_tuts/zf-logger.diff

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ index 9642f7c..5dfd2c3 100644
198198
],
199199
"packages-dev": [],
200200
diff --git a/index.php b/index.php
201-
index 646ee16..919eee1 100644
201+
index e64fbc5..0add68c 100644
202202
--- a/index.php
203203
+++ b/index.php
204204
@@ -6,11 +6,14 @@ use Symfony\Component\HttpFoundation\Request;
@@ -216,7 +216,7 @@ index 646ee16..919eee1 100644
216216

217217
// Service setup
218218
$c['connection'] = function(Container $c) {
219-
@@ -34,6 +37,15 @@ $c['router'] = function() {
219+
@@ -30,6 +33,15 @@ $c['router'] = function() {
220220

221221
return $router;
222222
};
@@ -231,10 +231,10 @@ index 646ee16..919eee1 100644
231231
+};
232232

233233
// run the framework!
234-
$route = $c['router']->match(
235-
@@ -49,6 +61,13 @@ if ($route) {
234+
$request = Request::createFromGlobals();
235+
@@ -47,6 +59,13 @@ if ($route) {
236236
// get the "controller" out, or default to error404_controller
237-
$controller = $c['request']->attributes->get('controller', 'error404_controller');
237+
$controller = $request->attributes->get('controller', 'error404_controller');
238238

239239
+if ($controller == 'error404_controller') {
240240
+ $msg = sprintf('Controller not found for "%s"', $c['request']->getPathInfo());
@@ -244,5 +244,5 @@ index 646ee16..919eee1 100644
244244
+}
245245
+
246246
// execute the controller and get the response
247-
$response = call_user_func_array($controller, array($c['request'], $c));
247+
$response = call_user_func_array($controller, array($request, $c));
248248
if (!$response instanceof Response) {

0 commit comments

Comments
 (0)