2022-11-22 22:14:27 -08:00
# Bundles
## How to Create a bundle
2023-02-28 15:18:25 -08:00
### Create files and directories
2022-12-14 00:26:38 -08:00
2023-02-28 15:18:25 -08:00
Example:
```bash
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
< ?php
namespace NickYeoman\ContactForm\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
2022-12-14 00:26:38 -08:00
2023-02-28 15:18:25 -08:00
class ContactForm extends AbstractController
{
#[Route('/contact-us', name: 'contactform_form')]
public function index() {
return $this->render('@SymfonyCMS/dashboard.html.twig', [
'controller_name' => 'ContactFormController',
]);
}
}
2022-12-14 00:26:38 -08:00
```
2023-02-28 15:18:25 -08:00
You will have to make changes to namespace, classname, route, render.
The ContactForm.php file example:
```php
< ?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:
```json
2022-12-14 00:26:38 -08:00
"autoload": {
"psr-4": {
2023-02-28 15:18:25 -08:00
"NickYeoman\\ContactForm\\": "lib/ContactForm/src/",
2022-12-14 00:26:38 -08:00
"App\\": "src/"
}
},
```
2023-02-28 15:18:25 -08:00
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:
```php
NickYeoman\ContactForm\ContactForm::class => ['all' => true],
```
### Symfony Bundle Config
Create a DependencyInjection file:
```php
< ?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:
```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
```yaml
ContactForm:
resource:
path: ../src/Controller/
namespace: NickYeoman\ContactForm\Controller
type: attribute
```
## Resources
* [Official Symfony Docs - Create a bundle ](https://symfony.com/doc/current/bundles.html#creating-a-bundle )
* [Symfony casts - Bootstrapping ](https://symfonycasts.com/screencast/symfony-bundle/bundle-directory )