Table of Contents

Create Your First Page in Symfony

TL;DR

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
            ]
        );
    }
}
app/Resources/views/lucky/number.html.twig
{% extends 'base.html.twig' %}
{% block body %}
    <h1>Lucky Numbers: {{ luckyNumberList }}</h1>
{% endblock %}

Creating a new route/controller (static URL)

Controllers live in src/AppBundle/Controller. Create with name <somename>Controller.php and use annotation to specify routes:

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")
     */
    public function numberAction()
    {
        $number = rand(0, 100);
 
        return new Response(
            '<html><body>Lucky number: '.$number.'</body></html>'
        );
    }
}

JSON

src/AppBundle/Controller/LuckyController.php
    // ...
    /**
     * @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

src/AppBundle/Controller/LuckyController.php
// ...
// --> 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

src/AppBundle/Controller/LuckyController.php
    /**
     * @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(
            '<html><body>Lucky numbers: '.$numbersList.'</body></html>'
        );
    }
 
    // ...

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:

app/Resources/views/lucky/number.html.twig
{% extends 'base.html.twig' %}
{% block body %}
    <h1>Lucky Numbers: {{ luckyNumberList }}</h1>
{% endblock %}

Directories

Application Configuration

The main configuration file for bundles is app/config/config.yml:

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 <key>