make: Commands

MakerBundle adds make: console commands that write controllers, entities and forms into your project, as a dev-only dependency.

Syntaxphp bin/console make:controller [controller-class] [--no-template] [--invokable] [--with-tests]

MakerBundle is a code generator. It adds console commands whose names begin with make:. Each one asks a few questions and writes files into your project. A bundle is a package that plugs into a Symfony application and can contribute console commands, configuration and services.

Installing it

Install it as a development dependency:

composer require --dev symfony/maker-bundle

The --dev matters. The recipe registers the bundle in config/bundles.php for the dev environment only, so with APP_ENV=prod the make: commands do not exist. Nothing the generator writes depends on the bundle afterwards, so there is no reason to ship it. php bin/console list make prints every generator available in your project, and --help on any one of them prints its arguments.

The ones you reach for first

make:controller writes a controller class, and a Twig template too when symfony/twig-bundle is installed. make:entity creates or updates a class in src/Entity plus its repository class; it needs doctrine/orm installed, and Stage 3 covers what those files mean. make:form writes a form class, optionally bound to a class you name.

What it does not do

It writes files once. Nothing watches them afterwards. Add a property to a class later and the generated form and templates do not follow. There is no runtime component: the output is ordinary PHP that you now own, and that nobody but you will review.

Example

Example · php
<?php
// src/Controller/ArticleController.php
// Written by: php bin/console make:controller ArticleController

declare(strict_types=1); // the generator does not add this line; add it yourself

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

final class ArticleController extends AbstractController
{
    // Shorthand: the first argument of #[Route] is the path.
    // #[Route('/article', name: 'app_article')]
    // is the same attribute as the longhand:
    // #[Route(path: '/article', name: 'app_article')]
    #[Route('/article', name: 'app_article')]
    public function index(): Response
    {
        return $this->render('article/index.html.twig', [
            'controller_name' => 'ArticleController',
        ]);
    }
}

// GET /article returns 200 and renders templates/article/index.html.twig,
// the placeholder page the same command generated next to this class.
// The route is named app_article: the generator prefixes route names with app_.

When to use it

  • Scaffold a controller and its template instead of typing the namespace and route attribute by hand.
  • Check which generators a project actually has with php bin/console list make.
  • Add fields to an existing entity class and regenerate its getters with make:entity --regenerate.
  • Keep a code generator out of production requirements by installing it with --dev.

More examples

Installing and discovering the commands

The alias and the full package name install the same thing, so you will see both in other people's instructions. Reach for `list make` before writing boilerplate: the set of generators depends on which packages are installed, so a project with Doctrine has `make:entity` and `make:crud` and a project without them does not.

Example · bash
# install the generator, development only
composer require --dev symfony/maker-bundle

# "maker" is a Flex alias for symfony/maker-bundle: the same package
composer require --dev maker

# every make: command this project has
php bin/console list make

# what one command asks for and accepts
php bin/console make:entity --help

Where the dev-only registration lives

This file maps each bundle class to the environments it is active in. The MakerBundle recipe writes the `['dev' => true]` line for you. Look here first when a `make:` command is missing: either the package was installed without `--dev` and the line is wrong, or the line is absent because the recipe was declined.

Example · php
<?php
// config/bundles.php

return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
    // 'all' => true would boot the generator in production too
    Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
];

What the terminal prints

Every generator reports each file it touched as `created`, `updated` or `no change`. Read that list before anything else: it is the only record of what entered your project, and `updated` means an existing file of yours was rewritten.

Example · bash
php bin/console make:controller ArticleController

#  created: src/Controller/ArticleController.php
#  created: templates/article/index.html.twig
#
#            
#   Success! 
#            
#
#  Next: Open your new controller class and add some pages!

Discussion

  • Be the first to comment on this lesson.