Create a CRUD in Symfony 7

By duartecancela, 14 March, 2024
symfony_crud

To create a Symfony project with a CRUD associated with a Product entity using the command line, along with installing MakerBundle and ORM (Object-Relational Mapping), you can follow these steps:

  1. Install Symfony CLI: If you haven't already, you need to install Symfony CLI. You can find installation instructions here.
  2. Create Symfony Project: Run the following command to create a new Symfony project:

symfony new my_project_name --version="7.0.*" --webapp

Replace my_project_name with the name of your project.

  1. Navigate to Project Directory: Enter into your project directory:

cd my_project_name

  1. Install MakerBundle: MakerBundle is a set of utilities for code generation. To install it, use Composer:

composer require symfony/maker-bundle --dev

This will install MakerBundle and its dependencies.

  1. Install Doctrine ORM: Doctrine ORM is a powerful tool for database manipulation in Symfony. Install it with Composer:

composer require symfony/orm-pack

This will install Doctrine ORM and its dependencies.

  1. Create Product Entity: To create a Product entity, run the following command:

php bin/console make:entity Product

Follow the prompts to define the fields for your Product entity.

  1. Create CRUD Operations: Next, use Symfony's MakerBundle to create CRUD operations:

php bin/console make:crud Product

This command will generate controllers, forms, templates, and routing for CRUD operations related to the Product entity.

  1. Database Setup: If you haven't already, configure your database settings in the .env file. Then, run migrations to create the necessary tables:

php bin/console doctrine:database:create 

php bin/console make:migration 

php bin/console doctrine:migrations:migrate

This will create the tables needed for your Product entity.

  1. Run the Symfony Server: Start the Symfony server:

symfony server:start

  1. Access CRUD Interface: Once the server is running, you can access the CRUD interface for your Product entity by navigating to http://localhost:8000/product.

That's it! You've created a Symfony project with CRUD operations associated with a Product entity, along with installing MakerBundle and ORM, using the command line.