Open NetBeans, on the left column> LMB over src\AppBundle\Controller folder> New PHP Class> LuckyController
LuckyController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php // src/AppBundle/Controller/LuckyController.php namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\Response; class LuckyController { /** * @Route("/lucky/number") */ public function numberAction() { $number = mt_rand(0, 100); // this is plain PHP return new Response( '<html><body>Lucky number: ' . $number . '</body></html>' ); } } |
Go to: http://localhost/symfonytest/first_test_symfony/web/lucky/number
It will render – Lucky number: 87 –
You CAN NOT CHANGE:
– namespace use
– class name LuckyController
You CAN CHANGE
– @Route(“/lucky/number”)
– your public function
You HAVE TO:
– return new Response
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php // src/AppBundle/Controller/LuckyController.php namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\Response; class LuckyController { /** * @Route("/lucky") */ public function myAction() { $numberOne = mt_rand(0, 100); // this is plain PHP $numberTwo = mt_rand(0, 100); // this is plain PHP return new Response( '<html><body>First number: ' . $numberOne . ' Second number: ' . $numberTwo . '</body></html>' ); } } |
Go to: http://localhost/symfonytest/first_test_symfony/web/lucky
First number: 84 Second number: 79