This repository has been archived on 2024-08-26. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/docs/Bundles.md
2023-04-03 20:26:25 -07:00

3.1 KiB

Bundles

How to Create a bundle

Create files and directories

Example:

newdirs='lib/ContactForm/src'
mkdir -p ${newdirs}/Controller ${newdirs}/Controller ${newdirs}/Entity ${newdirs}/Repoository ${newdirs}/DependencyInjection
touch ${newdirs}/Controller/ContactFormController.php
touch ${newdirs}/ContactForm.php
touch ${newdirs}/Controller/ContactForm.php

The ContactFormController.php example:

<?php
namespace NickYeoman\ContactForm\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class ContactForm extends AbstractController
{
   
    #[Route('/contact-us', name: 'contactform_form')]
    public function index() {
        return $this->render('@SymfonyCMS/dashboard.html.twig', [
            'controller_name' => 'ContactFormController',
        ]);
    }
}

You will have to make changes to namespace, classname, route, render.

The ContactForm.php file example:

<?php

namespace NickYeoman\ContactForm;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ContactForm extends Bundle
{
    // empty is fine, it will still load
}

Symfony Bundle Functioning

First you have to tell composer where to autoload from "vendorName\\bundleName\\": "lib/bundleName/src/",. In the file composer.json modify the autoload line, for example:

"autoload": {
        "psr-4": {
            "NickYeoman\\ContactForm\\": "lib/ContactForm/src/",
            "App\\": "src/"
        }
    },

and then run composer dump-autoload.

Next, you have to add the namespace\bundle name to config/bundles.php vendorName\bundleName\bundleName::class => ['all' => true], composer would do this automatically if it was grabbing from packigst, but we are just using a dev environment for now. Example:

NickYeoman\ContactForm\ContactForm::class => ['all' => true],

Symfony Bundle Config

Create a DependencyInjection file:

<?php
namespace NickYeoman\ContactForm\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class ContactFormExtension extends Extension 
{
    public function load(array $configs, ContainerBuilder $container)
    {
        
        $loader = new YamlFileLoader(
            $container,
            new FileLocator(__DIR__.'/../../config')
        );
        $loader->load('services.yaml');
    }
}

Next create a service in config/services.yaml:

services:
  NickYeoman\ContactForm\Controller\:
      resource: '../src/Controller/'
      tags: ['controller.service_arguments']

Finally, you can specify the route. Create a new route file config/routes.yaml

ContactForm:
  resource:
    path: ../src/Controller/
    namespace: NickYeoman\ContactForm\Controller
  type: attribute

Resources