User Tools

Site Tools


symfony:symfony_tutorial_notes: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 Composer.
  • The main configuration file for bundles is app/config/config.yml.
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

  • 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 Composer.

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>
symfony/symfony_tutorial_notes/create_your_first_page_in_symfony.txt · Last modified: 2015/09/17 23:38 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki