====== Create Your First Page in Symfony ====== ===== TL;DR ===== * Controllers live in **''src/AppBundle/Controller''**. * Templates live in **''app/Resources/views''**. * Use annotation in controllers to specify routes. * **''app/''**: Contains things like configuration and templates--basically, anything that is not PHP code. * **''src/''**: Your PHP code lives here. * **''web/''**: Document root for the project; contains any publicly accessible files like CSS, images and the front controllers (''app_dev.php'' and ''app.php''). * **''vendor/''**: Third-party libraries and bundles downloaded by [[https://getcomposer.org/|Composer]]. * The main configuration file for bundles is **''app/config/config.yml''**. render( 'lucky/number.html.twig', [ 'luckyNumberList' => $numbersList ] ); } } {% extends 'base.html.twig' %} {% block body %}

Lucky Numbers: {{ luckyNumberList }}

{% endblock %}
===== Creating a new route/controller (static URL) ===== Controllers live in **''src/AppBundle/Controller''**. Create with name ''Controller.php'' and use annotation to specify routes: Lucky number: '.$number.'' ); } } ===== JSON ===== // ... /** * @Route("/api/lucky/number") */ public function apiNumberAction() { $data = array( 'lucky_number' => rand(0, 100), ); return new Response( json_encode($data), 200, array('Content-Type' => 'application/json') ); } // ... or // ... // --> don't forget this new use statement use Symfony\Component\HttpFoundation\JsonResponse; // ... /** * @Route("/api/lucky/number") */ public function apiNumberAction() { $data = array( 'lucky_number' => rand(0, 100), ); // calls json_encode and sets the Content-Type header return new JsonResponse($data); } // ... ===== Dynamic URL Patterns ===== /** * @Route("/lucky/number/{count}") */ public function numberAction($count) { $numbers = array(); for ($i = 0; $i < $count; $i++) { $numbers[] = rand(0, 100); } $numbersList = implode(', ', $numbers); return new Response( 'Lucky numbers: '.$numbersList.'' ); } // ... ===== Rendering a Template (with the Service Container) ===== From within the controller action: $html = $this->container->get('templating')->render( 'lucky/number.html.twig', array('luckyNumberList' => $numbersList) ); return new Response($html); or, more tersely: return $this->render( 'lucky/number.html.twig', array('luckyNumberList' => $numbersList) ); Templates live in **''app/Resources/views''**: {% extends 'base.html.twig' %} {% block body %}

Lucky Numbers: {{ luckyNumberList }}

{% endblock %}
===== Directories ===== * ''app/'': Contains things like configuration and templates. Basically, anything that is not PHP code goes here. * ''src/'': Your PHP code lives here. * ''web/'': Document root for the project; contains any publicly accessible files, like CSS, images and the front controllers that execute the app (''app_dev.php'' and ''app.php''). * ''vendor/'': Third-party libraries and bundles downloaded by [[https://getcomposer.org/|Composer]]. ===== Application Configuration ===== The main configuration file for bundles is **''app/config/config.yml''**: framework: secret: "%secret%" router: resource: "%kernel.root_dir%/config/routing.yml" # ... twig: debug: "%kernel.debug%" strict_variables: "%kernel.debug%" # ... Dump of all of the valid configuration under a key: $ app/console config:dump-reference