Skip to content

Commit 35aba96

Browse files
committed
Adding Aura Router and Pimple container
1 parent cbef813 commit 35aba96

File tree

4 files changed

+252
-7
lines changed

4 files changed

+252
-7
lines changed

_tuts/aura-router.diff

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,86 @@ index 0c27e0c..aee9a74 100644
7979
"name": "symfony/http-foundation",
8080
"version": "v2.5.6",
8181
"target-dir": "Symfony/Component/HttpFoundation",
82+
diff --git a/index.php b/index.php
83+
index ed2564e..d8c644d 100644
84+
--- a/index.php
85+
+++ b/index.php
86+
@@ -11,21 +11,53 @@ try {
87+
// create a request object to help us
88+
use Symfony\Component\HttpFoundation\Request;
89+
use Symfony\Component\HttpFoundation\Response;
90+
-$request = Request::createFromGlobals();
91+
+use Aura\Router\RouterFactory;
92+
93+
+$request = Request::createFromGlobals();
94+
$uri = $request->getPathInfo();
95+
96+
-if ($uri == '/' || $uri == '') {
97+
+$routerFactory = new RouterFactory();
98+
+$router = $routerFactory->newInstance();
99+
+
100+
+// create a router, build the routes, and then execute it
101+
+$router->add('attendees_list', '/attendees')
102+
+ ->addValues(['controller' => 'attendees_controller']);
103+
+$router->add('homepage', '{/name}')
104+
+ ->addValues(['controller' => 'homepage_controller']);
105+
+$route = $router->match($uri, $request->server->all());
106+
+
107+
+// merge the matched attributes back into Symfony's request
108+
+if ($route) {
109+
+ $request->attributes->add($route->params);
110+
+}
111+
+
112+
+// get the "controller" out, or default to error404_controller
113+
+$controller = $request->attributes->get('controller', 'error404_controller');
114+
+
115+
+// execute the controller and get the response
116+
+$response = call_user_func_array($controller, array($request));
117+
+if (!$response instanceof Response) {
118+
+ throw new Exception(sprintf('Your controller "%s" did not return a response!!', $controller));
119+
+}
120+
+
121+
+$response->send();
122+
+
123+
+/*
124+
+ * My Controllers!
125+
+ */
126+
+function homepage_controller(Request $request) {
127+
128+
$content = '<h1>PHP Camp!</h1>';
129+
$content .= '<a href="/attendees">See the attendees</a>';
130+
- if ($name = $request->query->get('name')) {
131+
+ if ($name = $request->attributes->get('name')) {
132+
$content .= sprintf('<p>Oh, and hello %s!</p>', $name);
133+
}
134+
135+
- $response = new Response($content);
136+
+ return new Response($content);
137+
+}
138+
139+
-} elseif ($uri == '/attendees') {
140+
+function attendees_controller(Request $request) {
141+
+ global $dbh;
142+
143+
$sql = 'SELECT * FROM php_camp';
144+
$content = '<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />';
145+
@@ -40,14 +72,16 @@ if ($uri == '/' || $uri == '') {
146+
}
147+
$content .= '</table>';
148+
149+
- $response = new Response($content);
150+
+ return new Response($content);
151+
+}
152+
153+
-} else {
154+
+function error404_controller(Request $request) {
155+
$content = '<h1>404 Page not Found</h1>';
156+
$content .= '<p>Find a boy (or girl) scout - they can fix this!</p>';
157+
158+
$response = new Response($content);
159+
$response->setStatusCode(404);
160+
+
161+
+ return $response;
162+
}
163+
164+
-$response->send();

_tuts/pimple-dic.diff

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
diff --git a/composer.json b/composer.json
2+
index 52e5602..5aff5e1 100644
3+
--- a/composer.json
4+
+++ b/composer.json
5+
@@ -1,6 +1,7 @@
6+
{
7+
"require": {
8+
"symfony/http-foundation": "~2.5",
9+
- "aura/router": "~2.1"
10+
+ "aura/router": "~2.1",
11+
+ "pimple/pimple": "~3.0"
12+
}
13+
}
14+
diff --git a/composer.lock b/composer.lock
15+
index aee9a74..9642f7c 100644
16+
--- a/composer.lock
17+
+++ b/composer.lock
18+
@@ -4,7 +4,7 @@
19+
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
20+
"This file is @generated automatically"
21+
],
22+
- "hash": "05160fed37a43d33fa4fa22bcc76836f",
23+
+ "hash": "bb1db9d97f1d15cc2909421fd8279bc0",
24+
"packages": [
25+
{
26+
"name": "aura/router",
27+
@@ -61,6 +61,52 @@
28+
"time": "2014-10-22 14:14:56"
29+
},
30+
{
31+
+ "name": "pimple/pimple",
32+
+ "version": "v3.0.0",
33+
+ "source": {
34+
+ "type": "git",
35+
+ "url": "https://github.com/fabpot/Pimple.git",
36+
+ "reference": "876bf0899d01feacd2a2e83f04641e51350099ef"
37+
+ },
38+
+ "dist": {
39+
+ "type": "zip",
40+
+ "url": "https://api.github.com/repos/fabpot/Pimple/zipball/876bf0899d01feacd2a2e83f04641e51350099ef",
41+
+ "reference": "876bf0899d01feacd2a2e83f04641e51350099ef",
42+
+ "shasum": ""
43+
+ },
44+
+ "require": {
45+
+ "php": ">=5.3.0"
46+
+ },
47+
+ "type": "library",
48+
+ "extra": {
49+
+ "branch-alias": {
50+
+ "dev-master": "3.0.x-dev"
51+
+ }
52+
+ },
53+
+ "autoload": {
54+
+ "psr-0": {
55+
+ "Pimple": "src/"
56+
+ }
57+
+ },
58+
+ "notification-url": "https://packagist.org/downloads/",
59+
+ "license": [
60+
+ "MIT"
61+
+ ],
62+
+ "authors": [
63+
+ {
64+
+ "name": "Fabien Potencier",
65+
+ "email": "fabien@symfony.com"
66+
+ }
67+
+ ],
68+
+ "description": "Pimple is a simple Dependency Injection Container for PHP 5.3",
69+
+ "homepage": "http://pimple.sensiolabs.org",
70+
+ "keywords": [
71+
+ "container",
72+
+ "dependency injection"
73+
+ ],
74+
+ "time": "2014-07-24 09:48:15"
75+
+ },
76+
+ {
77+
"name": "symfony/http-foundation",
78+
"version": "v2.5.6",
79+
"target-dir": "Symfony/Component/HttpFoundation",
80+
diff --git a/index.php b/index.php
81+
index d8c644d..029e7d9 100644
82+
--- a/index.php
83+
+++ b/index.php
84+
@@ -12,30 +12,52 @@ try {
85+
use Symfony\Component\HttpFoundation\Request;
86+
use Symfony\Component\HttpFoundation\Response;
87+
use Aura\Router\RouterFactory;
88+
+use Pimple\Container;
89+
90+
-$request = Request::createFromGlobals();
91+
-$uri = $request->getPathInfo();
92+
+$c = new Container();
93+
94+
-$routerFactory = new RouterFactory();
95+
-$router = $routerFactory->newInstance();
96+
+// configuration
97+
+$c['connection_string'] = 'sqlite:'.__DIR__.'/data/database.sqlite';
98+
99+
-// create a router, build the routes, and then execute it
100+
-$router->add('attendees_list', '/attendees')
101+
- ->addValues(['controller' => 'attendees_controller']);
102+
-$router->add('homepage', '{/name}')
103+
- ->addValues(['controller' => 'homepage_controller']);
104+
-$route = $router->match($uri, $request->server->all());
105+
+// Service setup
106+
+$c['connection'] = function(Container $c) {
107+
+ return new PDO($c['connection_string']);
108+
+};
109+
+
110+
+$c['request'] = function() {
111+
+ return Request::createFromGlobals();
112+
+};
113+
+
114+
+$c['router'] = function() {
115+
+ $routerFactory = new RouterFactory();
116+
+
117+
+ $router = $routerFactory->newInstance();
118+
+
119+
+ // create a router, build the routes, and then execute it
120+
+ $router->add('attendees_list', '/attendees')
121+
+ ->addValues(['controller' => 'attendees_controller']);
122+
+ $router->add('homepage', '{/name}')
123+
+ ->addValues(['controller' => 'homepage_controller']);
124+
+
125+
+ return $router;
126+
+};
127+
+
128+
+
129+
+$uri = $c['request']->getPathInfo();
130+
+
131+
+
132+
+$route = $c['router']->match($uri, $c['request']->server->all());
133+
134+
// merge the matched attributes back into Symfony's request
135+
if ($route) {
136+
- $request->attributes->add($route->params);
137+
+ $c['request']->attributes->add($route->params);
138+
}
139+
140+
// get the "controller" out, or default to error404_controller
141+
-$controller = $request->attributes->get('controller', 'error404_controller');
142+
+$controller = $c['request']->attributes->get('controller', 'error404_controller');
143+
144+
// execute the controller and get the response
145+
-$response = call_user_func_array($controller, array($request));
146+
+$response = call_user_func_array($controller, array($c['request'], $c));
147+
if (!$response instanceof Response) {
148+
throw new Exception(sprintf('Your controller "%s" did not return a response!!', $controller));
149+
}
150+
@@ -56,8 +78,8 @@ function homepage_controller(Request $request) {
151+
return new Response($content);
152+
}
153+
154+
-function attendees_controller(Request $request) {
155+
- global $dbh;
156+
+function attendees_controller(Request $request, Container $c) {
157+
+ $dbh = $c['connection'];
158+
159+
$sql = 'SELECT * FROM php_camp';
160+
$content = '<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />';

_tuts/steps.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
"id": "aura-router",
1515
"name": "Aura: Router",
1616
"description": null
17+
},
18+
{
19+
"id": "pimple-dic",
20+
"name": "Pimple: DIC",
21+
"description": null
1722
}
1823
],
19-
"sha": "0ce63f01166eda74f7c74511bf08726bf8dacf67"
24+
"sha": "cbef8137eacd24440c00333da631331aed4f9e72"
2025
}

_tuts/symfony-httpfoundation.diff

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ index 0000000..0c27e0c
103103
+ "platform-dev": []
104104
+}
105105
diff --git a/index.php b/index.php
106-
index a2b3c5d..ed2564e 100644
106+
index 688df6c..ed2564e 100644
107107
--- a/index.php
108108
+++ b/index.php
109109
@@ -1,4 +1,5 @@
@@ -112,14 +112,11 @@ index a2b3c5d..ed2564e 100644
112112

113113
try {
114114
$dbPath = __DIR__.'/data/database.sqlite';
115-
@@ -7,36 +8,46 @@ try {
115+
@@ -7,33 +8,46 @@ try {
116116
die('Panic! '.$e->getMessage());
117117
}
118118

119-
-$uri = $_SERVER['REQUEST_URI'];
120-
-if ($pos = strpos($uri, '?')) {
121-
- $uri = substr($uri, 0, $pos);
122-
-}
119+
-$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
123120
+// create a request object to help us
124121
+use Symfony\Component\HttpFoundation\Request;
125122
+use Symfony\Component\HttpFoundation\Response;

0 commit comments

Comments
 (0)