Symfony and Co mposer

A little reminder for the current development.

With this test project : https://github.com/brunomartin/symfony-test

For using SQLite, need to modify conifig/config.yml :

doctrine:
dbal:
pdo_mysql -> pdo_sqlite
path: %kernel.cache_dir%/test.db

>symfony new comptes
>cd comptes/
>php bin/console server:run # to test
>php bin/console generate:controller # to create a controller
>php bin/console doctrine:generate:entity # to create entity
>php bin/console doctrine:database:create
>php bin/console doctrine:schema:update --force
>php bin/console doctrine:schema:validate

Si il y a eu mise à jour des entités :
php bin/console doctrine:generate:entities AppBundle
générera tous les getter et setter pour les champs.

Pour créer des entités à partir de la ligne de commande
bin/console doctrine:generate:entity -n --entity AppBundle:User --fields="name:string(100) age:int sexe:string(1)"

Pour créer des controller à partir de la ligne de commande
bin/console generate:controller -n --controller=AppBundle:User --actions="addUserAction:/add_user:AppBundle:User:add_user.html.twig"

Pour définir des relations entre entités:

class Operation
{
...
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="operations")
**/
private $user;
...
}

class User
{

/**
* @ORM\OneToMany(targetEntity= »Operation », mappedBy= »user »)
* @var Operation[] An ArrayCollection of Operation objects.
**/
private $operations = null;

}

Pour générer un form
php bin/console doctrine:generate:form AppBundle:User
Puis dans un action d’un controller
...
use AppBundle\Entity\User;
use AppBundle\Form\UserType;
use Symfony\Component\HttpFoundation\Request;
...
public function addUserAction(Request $request)
{
$em = $this->getDoctrine()->getManager();

$user = new User();
$user->setName(« Bruno »);

$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$em->persist($user);
$em->flush();
return $this->redirectToRoute(‘homepage’);
}

return $this->render(‘AppBundle:User:add_user.html.twig’, array(
‘form’ => $form->createView(),
));
}

And in the corresponding twig template :
{{ form_start(form) }}
{{ form_widget(form) }}
<input type="submit" value="Create" class="btn btn-default pull-right" />
{{ form_end(form) }}

Pour l’environnement de production :

php bin/console cache:clear --no-warmup --env=prod
php bin/console doctrine:schema:update --force --env=prod

Pour servir le site avec apache, installer libapach2-mod-php, installer le site dans /usr/share, donner les droits suffisants au user www-data, créer une configuration dans site-avaible symfony.conf

<VirtualHost *:80>
    ServerName home.brunocsmartin.fr
    ServerAlias www.home.brunocsmartin.fr

    DocumentRoot /usr/share/symfony/web
    <Directory /usr/share/symfony/web>
        AllowOverride All
        Order Allow,Deny
  Allow from all

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app.php [QSA,L]
        </IfModule>
    </Directory>

    Alias /symfony /usr/share/symfony/web/

    <Directory /usr/share/symfony/web/bundles>
        <IfModule mod_rewrite.c>
            RewriteEngine Off
        </IfModule>
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/project_error.log
    CustomLog ${APACHE_LOG_DIR}/project_access.log combined    

</VirtualHost>

<IfModule mod_ssl.c>

<VirtualHost *:443>
    ServerName home.brunocsmartin.fr
    ServerAlias www.home.brunocsmartin.fr

    DocumentRoot /usr/share/symfony/web

    Alias /symfony /usr/share/symfony/web/
    <Directory /usr/share/symfony/web>
  Options All
        AllowOverride All
        Order Allow,Deny
  Allow from all

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app.php [QSA,L]
        </IfModule>
    </Directory>

    <Directory /usr/share/symfony/bundles>
        <IfModule mod_rewrite.c>
            RewriteEngine Off
        </IfModule>
    </Directory>

    SSLEngine on
    SSLCertificateFile	/etc/letsencrypt/live/home.brunocsmartin.fr/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/home.brunocsmartin.fr/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/home.brunocsmartin.fr/chain.pem

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
        SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory /usr/lib/cgi-bin>
        SSLOptions +StdEnvVars
    </Directory>


</VirtualHost>

</IfModule>

et appliquer les bonnes permissions :

sudo setfacl -R -m u:www-data:rX /usr/share/symfony/
sudo setfacl -R -m u:www-data:rwX /usr/share/symfony/
sudo setfacl -dR -m u:www-data:rwX /usr/share/symfony/

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *