Symfony Cheatsheat

Here’s a bunch of random notes on Symfony. These are kind of a summary of the SymfonyCasts tutorial track here.

Creating a New Controller

  1. Right-click on the directory where you want your controller to to (probably src/Controller)
  2. Choose “New PHP Class”
    Because we installed the Symfony plugin, PhpStorm can pre-fill the namespace

Controller Functions (or Actions)

  • Must return a Symfony Response object
    Symfony\Component\HttpFoundation

Routes

Can either define routes in the routes.yaml file or with Annotations.

Routes Using Annotations

If using Annotations, you’ll need to install the composer package:
composer require annotations
And then remove any routes defined in the routes.yaml file.
To use, add a block comment at the top of the function. Use this option when the autocomplete box comes up:

Symfony\Component\Routing\Annotation\Route

Define an annotation like this:

/**
 * @Route(“/“)
 */

Profiler and Debug Bar

composer require profiler —dev

Packs

Packs are just a bunch of libraries all bundled together

Debug Pack

Debug Pack includes the profiler, plus a bunch of other stuff

composer require debug —dev

Unbundling Packs

If you want to manage each library separately from the entire package, you can “unbundle” them with:

composer unpack [pack name]

Routes

To show all the routes currently registered in your app run:

./bin/console debug:router

To override the auto-generated route name, add the name attribute to the routes file or the annotation.

/**
 * @Route("/", name="app_homepage")
 */

To specify the method, use the methods attribute

/**
 * @Route("/news/{slug}/heart", name="article_toggle_heart", methods={"POST"})
 */

To use wildcards for paths in Twig, pass in the parameter as the 2nd argument

href="{{ path('article_toggle_heart', {slug: slug}) }}"

Twig

To install Twig:

composer require twig

To use it in your controller, make sure your controller extends AbstractController and add the corresponding use statement

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class PasswordController extends AbstractController
{
    ...
}

To pass variables to a Twig template, pass an associate array as the 2nd argument to render

return $this->render('article/show.html.twig', [
    'title' => ucwords(str_replace('-', ' ', $slug)),
    'slug' => $slug,
    'comments' => $comments
]);

To use wildcards for paths in Twig, pass in the parameter as the 2nd argument in the Twig template

href="{{ path('article_toggle_heart', {slug: slug}) }}"

Autowiring

If you want an object/service available in your controller method, just add it as an argument with the type. For example, say we want the logger available:

public function toggleArticleHeart($slug, LoggerInterface $logger)
{
    $logger->info('Log my message');
}

To quickly show all the things that can be autowired, run

 ./bin/console debug:autowiring 

Places where Autowiring works:
1. Controller actions – type hint the service
2. In the __construct() method of the service (preferred place)

Logs

Logs are located in var/log/dev.log. You can tail the logs in the terminal with

tail -f var/log/dev.log

If you log stuff in your controller it will show up in that log file.

Bundles and Services

Bundles give you services. If you add a Bundle, you get a Service. Services are Tools.
A bundle usually includes :
1. Some php classes
2. Configuration that will add one or more services

A service is basically a class that does work.

Main ideas about services:
1. There are many services in the container and each has an id
2. The services you’ll use 99% of the time show up in debug:autowiring

To get a list of the services in the container (just the most important ones):

./bin/console debug:autowiring

To see all the services in the container run:

./bin/console debug:container --show-private

Cache

The built-in Symfony cache normally clears itself when we change a file. You might need to sometimes clear it anyway though.

./bin/console cache:clear

Production

To switch the app to production environment, you need to make the change in two places:
1. In .env change APP_ENV=dev to APP_ENV=prod
2. In public/index.php change

$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
to
$kernel = new Kernel($_SERVER['APP_ENV'], false);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.