@@ -55,13 +55,13 @@ Next, create an ``index.php`` file that defines the kernel class and executes it
55
55
{
56
56
// kernel is a service that points to this class
57
57
// optional 3rd argument is the route name
58
- $routes->add('/random/{limit}', 'kernel :randomNumber');
58
+ $routes->add('/random/{limit}', 'Kernel: :randomNumber');
59
59
}
60
60
61
61
public function randomNumber($limit)
62
62
{
63
63
return new JsonResponse(array(
64
- 'number' => rand (0, $limit)
64
+ 'number' => random_int (0, $limit),
65
65
));
66
66
}
67
67
}
@@ -136,11 +136,6 @@ hold the kernel. Now it looks like this::
136
136
use Symfony\Component\DependencyInjection\ContainerBuilder;
137
137
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
138
138
use Symfony\Component\Routing\RouteCollectionBuilder;
139
- use Doctrine\Common\Annotations\AnnotationRegistry;
140
-
141
- $loader = require __DIR__.'/../vendor/autoload.php';
142
- // auto-load annotations
143
- AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
144
139
145
140
class Kernel extends BaseKernel
146
141
{
@@ -149,8 +144,8 @@ hold the kernel. Now it looks like this::
149
144
public function registerBundles()
150
145
{
151
146
$bundles = array(
152
- new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
153
- new Symfony\Bundle\TwigBundle\TwigBundle(),
147
+ new \ Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
148
+ new \ Symfony\Bundle\TwigBundle\TwigBundle(),
154
149
);
155
150
156
151
if ($this->getEnvironment() == 'dev') {
@@ -162,7 +157,7 @@ hold the kernel. Now it looks like this::
162
157
163
158
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
164
159
{
165
- $loader->load(__DIR__.'/config/framework.yaml');
160
+ $loader->load(__DIR__.'/../ config/framework.yaml');
166
161
167
162
// configure WebProfilerBundle only if the bundle is enabled
168
163
if (isset($this->bundles['WebProfilerBundle'])) {
@@ -198,6 +193,12 @@ hold the kernel. Now it looks like this::
198
193
}
199
194
}
200
195
196
+ Before you continue, run this command to add support for the new dependencies:
197
+
198
+ .. code-block :: terminal
199
+
200
+ composer require symfony/yaml symfony/twig-bundle symfony/web-profiler-bundle doctrine/annotations
201
+
201
202
Unlike the previous kernel, this loads an external ``config/framework.yaml `` file,
202
203
because the configuration started to get bigger:
203
204
@@ -208,8 +209,6 @@ because the configuration started to get bigger:
208
209
# config/framework.yaml
209
210
framework :
210
211
secret : S0ME_SECRET
211
- templating :
212
- engines : ['twig']
213
212
profiler : { only_exceptions: false }
214
213
215
214
.. code-block :: xml
@@ -223,9 +222,6 @@ because the configuration started to get bigger:
223
222
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
224
223
225
224
<framework : config secret =" S0ME_SECRET" >
226
- <framework : templating >
227
- <framework : engine >twig</framework : engine >
228
- </framework : templating >
229
225
<framework : profiler only-exceptions =" false" />
230
226
</framework : config >
231
227
</container >
@@ -235,9 +231,6 @@ because the configuration started to get bigger:
235
231
// config/framework.php
236
232
$container->loadFromExtension('framework', array(
237
233
'secret' => 'S0ME_SECRET',
238
- 'templating' => array(
239
- 'engines' => array('twig'),
240
- ),
241
234
'profiler' => array(
242
235
'only_exceptions' => false,
243
236
),
@@ -259,21 +252,20 @@ has one file in it::
259
252
*/
260
253
public function randomNumber($limit)
261
254
{
262
- $number = rand (0, $limit);
255
+ $number = random_int (0, $limit);
263
256
264
257
return $this->render('micro/random.html.twig', array(
265
- 'number' => $number
258
+ 'number' => $number,
266
259
));
267
260
}
268
261
}
269
262
270
- Template files should live in the ``Resources/views/ `` directory of whatever directory
271
- your *kernel * lives in. Since ``Kernel `` lives in ``src/ ``, this template lives
272
- at ``src/Resources/views/micro/random.html.twig ``:
263
+ Template files should live in the ``templates/ `` directory at the root of your project.
264
+ This template lives at ``templates/micro/random.html.twig ``:
273
265
274
266
.. code-block :: html+twig
275
267
276
- <!-- src/Resources/views /micro/random.html.twig -->
268
+ <!-- templates /micro/random.html.twig -->
277
269
<!DOCTYPE html>
278
270
<html>
279
271
<head>
@@ -289,9 +281,13 @@ Finally, you need a front controller to boot and run the application. Create a
289
281
290
282
// public/index.php
291
283
284
+ use App\Kernel;
285
+ use Doctrine\Common\Annotations\AnnotationRegistry;
292
286
use Symfony\Component\HttpFoundation\Request;
293
287
294
- require __DIR__.'/../src/Kernel.php';
288
+ $loader = require __DIR__.'/../vendor/autoload.php';
289
+ // auto-load annotations
290
+ AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
295
291
296
292
$kernel = new Kernel('dev', true);
297
293
$request = Request::createFromGlobals();
@@ -311,13 +307,12 @@ this:
311
307
├─ public/
312
308
| └─ index.php
313
309
├─ src/
314
- | ├─ Kernel.php
315
310
| ├─ Controller
316
311
| | └─ MicroController.php
317
- │ └─ Resources
318
- | └─ views
319
- | └─ micro
320
- | └─ random.html.twig
312
+ | └─ Kernel.php
313
+ ├─ templates/
314
+ | └─ micro/
315
+ | └─ random.html.twig
321
316
├─ var/
322
317
| ├─ cache/
323
318
│ └─ log/
@@ -331,7 +326,7 @@ As before you can use PHP built-in server:
331
326
.. code-block :: terminal
332
327
333
328
cd public/
334
- $ php -S localhost:8000
329
+ $ php -S localhost:8000 -t public/
335
330
336
331
Then see webpage in browser:
337
332
0 commit comments