@@ -35,69 +35,31 @@ Creating a Page: Route and Controller
35
35
Before continuing, make sure you've read the :doc: `Installation </book/installation >`
36
36
chapter and can access your new Symfony app in the browser.
37
37
38
- Suppose you want to create a page ``/lucky/number `` that generates a lucky (well,
38
+ Suppose you want to create a page - ``/lucky/number `` - that generates a lucky (well,
39
39
random) number and prints it. To do that, create a "Controller class" and a
40
40
"controller" method inside of it that will be executed when someone goes to
41
41
``/lucky/number ``::
42
42
43
- .. configuration-block ::
44
-
45
- .. code-block :: php-annotations
46
-
47
- // src/AppBundle/Controller/LuckyController.php
48
- namespace AppBundle\Controller;
43
+ // src/AppBundle/Controller/LuckyController.php
44
+ namespace AppBundle\Controller;
49
45
50
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
51
- use Symfony\Component\HttpFoundation\Response;
46
+ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
47
+ use Symfony\Component\HttpFoundation\Response;
52
48
53
- class LuckyController
49
+ class LuckyController
50
+ {
51
+ /**
52
+ * @Route("/lucky/number")
53
+ */
54
+ public function numberAction()
54
55
{
55
- /**
56
- * @Route("/lucky/number")
57
- */
58
- public function numberAction()
59
- {
60
- $number = rand(0, 100);
56
+ $number = rand(0, 100);
61
57
62
- return new Response(
63
- '<html><body>Lucky number: '.$number.'</body></html>'
64
- );
65
- }
58
+ return new Response(
59
+ '<html><body>Lucky number: '.$number.'</body></html>'
60
+ );
66
61
}
67
-
68
- .. code-block :: yaml
69
-
70
- # app/config/routing.yml
71
- lucky_number :
72
- path : /lucky/number
73
- defaults : { _controller: AppBundle:Lucky:number }
74
-
75
- .. code-block :: xml
76
-
77
- <!-- app/config/routing.xml -->
78
- <?xml version =" 1.0" encoding =" UTF-8" ?>
79
- <routes xmlns =" http://symfony.com/schema/routing"
80
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
81
- xsi : schemaLocation =" http://symfony.com/schema/routing
82
- http://symfony.com/schema/routing/routing-1.0.xsd" >
83
-
84
- <route id =" lucky_number" path =" /lucky/number" >
85
- <default key =" _controller" >AppBundle:Lucky:number</default >
86
- </route >
87
- </routes >
88
-
89
- .. code-block :: php
90
-
91
- // app/config/routing.php
92
- use Symfony\Component\Routing\RouteCollection;
93
- use Symfony\Component\Routing\Route;
94
-
95
- $collection = new RouteCollection();
96
- $collection->add('lucky_number', new Route('/lucky/number', array(
97
- '_controller' => 'AppBundle:Lucky:number',
98
- )));
99
-
100
- return $collection;
62
+ }
101
63
102
64
"Controller class" is a convenient way to group several "controllers" together.
103
65
0 commit comments