User Tools

Site Tools


symfony:symfony_tutorial_notes:create_your_first_page_in_symfony

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
symfony:symfony_tutorial_notes:create_your_first_page_in_symfony [2015/09/15 03:41] – created mithatsymfony:symfony_tutorial_notes:create_your_first_page_in_symfony [2015/09/17 23:38] (current) mithat
Line 1: Line 1:
 ====== Create Your First Page in Symfony ====== ====== 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''**.
 +
 +<file php src/AppBundle/Controller/LuckyController.php>
 +<?php
 +
 +namespace AppBundle\Controller;
 +
 +use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 +use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 +use Symfony\Component\HttpFoundation\Response;
 +
 +class LuckyController extends Controller
 +{
 +    /**
 +     * @Route("/lucky/number/{count}", name="lucky_number_count")
 +     */
 +    public function numberAction($count)
 +    {
 +        $numbers = array();
 +        for ($i = 0; $i < $count; $i++) {
 +            $numbers[] = rand(0, 100);
 +        }
 +        $numbersList = implode(', ', $numbers);
 +
 +        return $this->render(
 +            'lucky/number.html.twig', [
 +                'luckyNumberList' => $numbersList
 +            ]
 +        );
 +    }
 +}
 +</file>
 +
 +<file twig app/Resources/views/lucky/number.html.twig>
 +{% extends 'base.html.twig' %}
 +{% block body %}
 +    <h1>Lucky Numbers: {{ luckyNumberList }}</h1>
 +{% endblock %}
 +</file>
 ===== Creating a new route/controller (static URL) ===== ===== Creating a new route/controller (static URL) =====
  
Line 50: Line 97:
 </file> </file>
 or or
-<file php src/AppBundle/Controller/uckyController.php>+<file php src/AppBundle/Controller/LuckyController.php>
 // ... // ...
 // --> don't forget this new use statement // --> don't forget this new use statement
Line 74: Line 121:
  
 ===== Dynamic URL Patterns ===== ===== Dynamic URL Patterns =====
-<file php LuckyController.php>+<file php src/AppBundle/Controller/LuckyController.php>
     /**     /**
      * @Route("/lucky/number/{count}")      * @Route("/lucky/number/{count}")
Line 114: Line 161:
 </code> </code>
  
-Templates live in **''../app/Resources/views/''**:+Templates live in **''app/Resources/views''**:
  
 <file twig app/Resources/views/lucky/number.html.twig> <file twig app/Resources/views/lucky/number.html.twig>
symfony/symfony_tutorial_notes/create_your_first_page_in_symfony.1442288477.txt.gz · Last modified: 2015/09/15 03:41 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki