Symfony 6 Создание своего bundle
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
-
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.
- Use the Symfony CLI or Composer to create a new project:
-
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.
- Start the Symfony server:
Step 2: Review Symfony Documentation
- Visit the Symfony Documentation to familiarize yourself with the framework and its components.
Step 3: Configure Composer
-
Create the Bundle Directory:
- Navigate to your project directory and create a folder for your bundle:
mkdir -p src/Bundle/Elenyum/HelloBundle
- Navigate to your project directory and create a folder for your bundle:
-
Add composer.json File:
- Create a
composer.json
file in theHelloBundle
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\\": "" } } }
- Create a
-
Set Autoloading for Tests (optional):
- Add the namespace for tests:
"autoload-dev": { "psr-4": { "Elenyum\\HelloBundle\\Test\\": "tests/" } }
- Add the namespace for tests:
Step 4: Create the Bundle Class
-
Create ElenyumHelloBundle Class:
- Inside
src/Bundle/Elenyum/HelloBundle
, create a file namedElenyumHelloBundle.php
:namespace Elenyum\HelloBundle; use Symfony\Component\HttpKernel\Bundle\AbstractBundle; class ElenyumHelloBundle extends AbstractBundle { public function getPutch() { // Load configuration files } }
- Inside
-
Create the Controller:
- Create a
Controller
directory and anIndexController.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!']); } }
- Create a
Step 5: Create service.yaml File
-
Create service.yaml:
- In the
config/packages
directory, create aservices.yaml
file:services: _defaults: autowire: true autoconfigure: true Elenyum\HelloBundle\Controller\IndexController: ~
- In the
-
Leave Parameters Empty:
- Ensure that the
parameters
section is left blank for now.
- Ensure that the
Step 6: Register the Bundle
- Register the Bundle:
- Open
config/bundles.php
and add your bundle:return [ // Other bundles... Elenyum\HelloBundle\ElenyumHelloBundle::class => ['all' => true], ];
- Open
Step 7: Configure Routes
- Create routes.yaml:
- In the
config/routes
directory, create aroutes.yaml
file:hello: path: /hello controller: Elenyum\HelloBundle\Controller\IndexController
- In the
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.