docs updated

This commit is contained in:
Nick Yeoman 2023-02-28 15:18:25 -08:00
parent d162e456a6
commit 42a6e0734a
4 changed files with 117 additions and 14 deletions

View File

@ -2,9 +2,10 @@
## Requirements ## Requirements
* [Install Composer](https://www.nickyeoman.com/page/install-composer-on-ubuntu) (sudo nala install php-curl php-xml php-mysql) * ubuntu php-cli (sudo nala install php-curl php-xml php-mysql)
* [Install Composer](https://www.nickyeoman.com/page/install-composer-on-ubuntu)
* [Symfony cli](https://symfony.com/download) * [Symfony cli](https://symfony.com/download)
* php-cli
## Create your Project ## Create your Project

View File

@ -138,7 +138,7 @@ echo "DBUSER=theuser${GEN}" >> .env
GEN=`echo $RANDOM | md5sum | head -c 12` GEN=`echo $RANDOM | md5sum | head -c 12`
echo "DBPASSWORD=${GEN}" >> .env echo "DBPASSWORD=${GEN}" >> .env
echo '' >> .env echo '' >> .env
echo '# DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=8&charset=utf8mb4"' >> .env echo '# DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=mariadb-10.4.11&charset=utf8mb4"' >> .env
# TODO: move these here after you have symfony production ready # TODO: move these here after you have symfony production ready

View File

@ -2,23 +2,123 @@
## How to Create a bundle ## How to Create a bundle
[Official Symfony Docs - Create a bundle](https://symfony.com/doc/current/bundles.html#creating-a-bundle) First we are going to get the bundle functioning in an existing project, then move it to composer/packigst.
mkdir -p lib/bundleName/src ### Create files and directories
touch lib/bundleName/src/bundleName.php
You have to add the namespace\bundle name to config/bundles.php Example:
```vendorName\bundleName\bundleName::class => ['all' => true],``` ```bash
newdirs='lib/ContactForm/src'
And you have to auto load the class by adding the next line in composer.json: 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;
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
<?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
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"vendorName\\bundleName\\": "lib/bundleName/src/", "NickYeoman\\ContactForm\\": "lib/ContactForm/src/",
"App\\": "src/" "App\\": "src/"
} }
}, },
``` ```
(replace vendorName\bundleName and make sure the directory is correct)
run ```composer dump-autoload``` after updating the composer.json 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)

View File

@ -3,3 +3,5 @@
* [Symfony 6 full course](https://www.youtube.com/watch?v=QPky3r2prEI) * [Symfony 6 full course](https://www.youtube.com/watch?v=QPky3r2prEI)
* [Symfony6 for beginners](https://www.youtube.com/watch?v=PMERdlfL6LE&list=PLFHz2csJcgk-t8ErN1BHUUxTNj45dkSqS) * [Symfony6 for beginners](https://www.youtube.com/watch?v=PMERdlfL6LE&list=PLFHz2csJcgk-t8ErN1BHUUxTNj45dkSqS)
* [Symfonycasts](https://symfonycasts.com/tracks/symfony) * [Symfonycasts](https://symfonycasts.com/tracks/symfony)
* https://github.com/msalsas/symfony-bundle-skeleton
* [bundle medium](https://macrini.medium.com/how-to-create-service-bundles-for-a-symfony-application-f266ecf01fca)