Symfony 6 Создание своего bundle

3 min read 18 days ago
Published on Sep 14, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the process of creating a custom bundle in Symfony 6. By the end of this guide, you'll understand when it's appropriate to extract code into a bundle and how to set it up correctly. This step-by-step approach will help you build a solid foundation for your Symfony projects.

Step 1: Create a New Symfony Project

  1. Create the Symfony Project:

    • Use the Symfony CLI or Composer to create a new project:
      symfony new my_project_name --full
      
    • Replace my_project_name with your desired project name.
  2. Verify the Server is Running:

    • Start the Symfony server:
      symfony server:start
      
    • Open your browser and navigate to http://localhost:8000 to ensure the server is operational.

Step 2: Review Symfony Documentation

Step 3: Configure Composer

  1. Create the Bundle Directory:

    • Navigate to your project directory and create a folder for your bundle:
      mkdir -p src/Bundle/Elenyum/HelloBundle
      
  2. Add composer.json File:

    • Create a composer.json file in the HelloBundle directory with the following structure:
      {
        "name": "elenyum/hello-bundle",
        "description": "A simple HelloBundle",
        "type": "symfony-bundle",
        "version": "1.0.0",
        "license": "MIT",
        "require": {
          "php": ">=7.2"
        },
        "autoload": {
          "psr-4": {
            "Elenyum\\HelloBundle\\": ""
          }
        }
      }
      
  3. Set Autoloading for Tests (optional):

    • Add the namespace for tests:
      "autoload-dev": {
        "psr-4": {
          "Elenyum\\HelloBundle\\Test\\": "tests/"
        }
      }
      

Step 4: Create the Bundle Class

  1. Create ElenyumHelloBundle Class:

    • Inside src/Bundle/Elenyum/HelloBundle, create a file named ElenyumHelloBundle.php:
      namespace Elenyum\HelloBundle;
      
      use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
      
      class ElenyumHelloBundle extends AbstractBundle
      {
          public function getPutch()
          {
              // Load configuration files
          }
      }
      
  2. Create the Controller:

    • Create a Controller directory and an IndexController.php file:
      namespace Elenyum\HelloBundle\Controller;
      
      use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
      use Symfony\Component\HttpFoundation\JsonResponse;
      use Symfony\Component\Routing\Annotation\Route;
      
      class IndexController extends AbstractController
      {
          /**
           * @Route("/hello", name="hello")
           */
          public function __invoke()
          {
              return new JsonResponse(['message' => 'Hello, World!']);
          }
      }
      

Step 5: Create service.yaml File

  1. Create service.yaml:

    • In the config/packages directory, create a services.yaml file:
      services:
        _defaults:
          autowire: true
          autoconfigure: true
      
        Elenyum\HelloBundle\Controller\IndexController: ~
      
  2. Leave Parameters Empty:

    • Ensure that the parameters section is left blank for now.

Step 6: Register the Bundle

  1. Register the Bundle:
    • Open config/bundles.php and add your bundle:
      return [
          // Other bundles...
          Elenyum\HelloBundle\ElenyumHelloBundle::class => ['all' => true],
      ];
      

Step 7: Configure Routes

  1. Create routes.yaml:
    • In the config/routes directory, create a routes.yaml file:
      hello:
        path: /hello
        controller: Elenyum\HelloBundle\Controller\IndexController
      

Conclusion

In this tutorial, you've learned how to create a custom bundle in Symfony 6, including setting up your project, configuring Composer, creating classes and routes, and registering your bundle. For further exploration, consider adding more functionalities to your bundle or exploring Symfony's advanced features. You can find the completed project here.