{"id":58,"date":"2017-08-20T20:10:07","date_gmt":"2017-08-20T19:10:07","guid":{"rendered":"http:\/\/blog.brunocsmartin.fr\/?p=58"},"modified":"2017-10-01T16:12:18","modified_gmt":"2017-10-01T15:12:18","slug":"symfony-and-co-mposer","status":"publish","type":"post","link":"https:\/\/blog.brunocsmartin.fr\/index.php\/2017\/08\/20\/symfony-and-co-mposer\/","title":{"rendered":"Symfony and Co mposer"},"content":{"rendered":"<p>A little reminder for the current development.<\/p>\n<p>With this test project : <a href=\"https:\/\/github.com\/brunomartin\/symfony-test\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/brunomartin\/symfony-test<\/a><\/p>\n<p>For using SQLite, need to modify conifig\/config.yml :<br \/>\n<code><br \/>\ndoctrine:<br \/>\ndbal:<br \/>\npdo_mysql -&gt; pdo_sqlite<br \/>\npath: %kernel.cache_dir%\/test.db<\/code><\/p>\n<p><code>&gt;symfony new comptes<br \/>\n&gt;cd comptes\/<br \/>\n&gt;php bin\/console server:run # to test<br \/>\n&gt;php bin\/console generate:controller # to create a controller<br \/>\n&gt;php bin\/console doctrine:generate:entity # to create entity<br \/>\n&gt;php bin\/console doctrine:database:create<br \/>\n&gt;php bin\/console doctrine:schema:update --force<br \/>\n&gt;php bin\/console doctrine:schema:validate<\/code><\/p>\n<p>Si il y a eu mise \u00e0 jour des entit\u00e9s :<br \/>\n<code>php bin\/console doctrine:generate:entities AppBundle<\/code><br \/>\ng\u00e9n\u00e9rera tous les getter et setter pour les champs.<\/p>\n<p>Pour cr\u00e9er des entit\u00e9s \u00e0 partir de la ligne de commande<br \/>\n<code>bin\/console doctrine:generate:entity -n --entity AppBundle:User --fields=\"name:string(100) age:int sexe:string(1)\"<\/code><\/p>\n<p>Pour cr\u00e9er des controller \u00e0 partir de la ligne de commande<br \/>\n<code>bin\/console generate:controller -n --controller=AppBundle:User --actions=\"addUserAction:\/add_user:AppBundle:User:add_user.html.twig\"<\/code><\/p>\n<p>Pour d\u00e9finir des relations entre entit\u00e9s:<\/p>\n<p><code>class Operation<br \/>\n{<br \/>\n...<br \/>\n\/**<br \/>\n* @ORM\\ManyToOne(targetEntity=\"User\", inversedBy=\"operations\")<br \/>\n**\/<br \/>\nprivate $user;<br \/>\n...<br \/>\n}<\/code><\/p>\n<p>class User<br \/>\n{<br \/>\n&#8230;<br \/>\n\/**<br \/>\n* @ORM\\OneToMany(targetEntity=\u00a0\u00bbOperation\u00a0\u00bb, mappedBy=\u00a0\u00bbuser\u00a0\u00bb)<br \/>\n* @var Operation[] An ArrayCollection of Operation objects.<br \/>\n**\/<br \/>\nprivate $operations = null;<br \/>\n&#8230;<br \/>\n}<\/p>\n<p>Pour g\u00e9n\u00e9rer un form<br \/>\n<code>php bin\/console doctrine:generate:form AppBundle:User<\/code><br \/>\nPuis dans un action d&rsquo;un controller<br \/>\n<code>...<br \/>\nuse AppBundle\\Entity\\User;<br \/>\nuse AppBundle\\Form\\UserType;<br \/>\nuse Symfony\\Component\\HttpFoundation\\Request;<br \/>\n...<br \/>\npublic function addUserAction(Request $request)<br \/>\n{<br \/>\n$em = $this-&gt;getDoctrine()-&gt;getManager();<\/code><\/p>\n<p>$user = new User();<br \/>\n$user-&gt;setName(\u00ab\u00a0Bruno\u00a0\u00bb);<\/p>\n<p>$form = $this-&gt;createForm(UserType::class, $user);<br \/>\n$form-&gt;handleRequest($request);<br \/>\nif ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {<br \/>\n$user = $form-&gt;getData();<br \/>\n$em-&gt;persist($user);<br \/>\n$em-&gt;flush();<br \/>\nreturn $this-&gt;redirectToRoute(&lsquo;homepage&rsquo;);<br \/>\n}<\/p>\n<p>return $this-&gt;render(&lsquo;AppBundle:User:add_user.html.twig&rsquo;, array(<br \/>\n&lsquo;form&rsquo; =&gt; $form-&gt;createView(),<br \/>\n));<br \/>\n}<\/p>\n<p>And in the corresponding twig template :<br \/>\n<code>{{ form_start(form) }}<br \/>\n{{ form_widget(form) }}<br \/>\n&lt;input type=\"submit\" value=\"Create\" class=\"btn btn-default pull-right\" \/&gt;<br \/>\n{{ form_end(form) }}<\/code><\/p>\n<p>Pour l&rsquo;environnement de production :<br \/>\n<code><br \/>\nphp bin\/console cache:clear --no-warmup --env=prod<br \/>\nphp bin\/console doctrine:schema:update --force --env=prod<br \/>\n<\/code><\/p>\n<p>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\u00e9er une configuration dans site-avaible symfony.conf<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">&lt;VirtualHost *:80&gt;\r\n    ServerName home.brunocsmartin.fr\r\n    ServerAlias www.home.brunocsmartin.fr\r\n\r\n    DocumentRoot \/usr\/share\/symfony\/web\r\n    &lt;Directory \/usr\/share\/symfony\/web&gt;\r\n        AllowOverride All\r\n        Order Allow,Deny\r\n  Allow from all\r\n\r\n        &lt;IfModule mod_rewrite.c&gt;\r\n            Options -MultiViews\r\n            RewriteEngine On\r\n            RewriteCond %{REQUEST_FILENAME} !-f\r\n            RewriteRule ^(.*)$ app.php [QSA,L]\r\n        &lt;\/IfModule&gt;\r\n    &lt;\/Directory&gt;\r\n\r\n    Alias \/symfony \/usr\/share\/symfony\/web\/\r\n\r\n    &lt;Directory \/usr\/share\/symfony\/web\/bundles&gt;\r\n        &lt;IfModule mod_rewrite.c&gt;\r\n            RewriteEngine Off\r\n        &lt;\/IfModule&gt;\r\n    &lt;\/Directory&gt;\r\n\r\n    ErrorLog ${APACHE_LOG_DIR}\/project_error.log\r\n    CustomLog ${APACHE_LOG_DIR}\/project_access.log combined    \r\n\r\n&lt;\/VirtualHost&gt;\r\n\r\n&lt;IfModule mod_ssl.c&gt;\r\n\r\n&lt;VirtualHost *:443&gt;\r\n    ServerName home.brunocsmartin.fr\r\n    ServerAlias www.home.brunocsmartin.fr\r\n\r\n    DocumentRoot \/usr\/share\/symfony\/web\r\n\r\n    Alias \/symfony \/usr\/share\/symfony\/web\/\r\n    &lt;Directory \/usr\/share\/symfony\/web&gt;\r\n  Options All\r\n        AllowOverride All\r\n        Order Allow,Deny\r\n  Allow from all\r\n\r\n        &lt;IfModule mod_rewrite.c&gt;\r\n            Options -MultiViews\r\n            RewriteEngine On\r\n            RewriteCond %{REQUEST_FILENAME} !-f\r\n            RewriteRule ^(.*)$ app.php [QSA,L]\r\n        &lt;\/IfModule&gt;\r\n    &lt;\/Directory&gt;\r\n\r\n    &lt;Directory \/usr\/share\/symfony\/bundles&gt;\r\n        &lt;IfModule mod_rewrite.c&gt;\r\n            RewriteEngine Off\r\n        &lt;\/IfModule&gt;\r\n    &lt;\/Directory&gt;\r\n\r\n    SSLEngine on\r\n    SSLCertificateFile\t\/etc\/letsencrypt\/live\/home.brunocsmartin.fr\/cert.pem\r\n    SSLCertificateKeyFile \/etc\/letsencrypt\/live\/home.brunocsmartin.fr\/privkey.pem\r\n    SSLCertificateChainFile \/etc\/letsencrypt\/live\/home.brunocsmartin.fr\/chain.pem\r\n\r\n    &lt;FilesMatch \"\\.(cgi|shtml|phtml|php)$\"&gt;\r\n        SSLOptions +StdEnvVars\r\n    &lt;\/FilesMatch&gt;\r\n    &lt;Directory \/usr\/lib\/cgi-bin&gt;\r\n        SSLOptions +StdEnvVars\r\n    &lt;\/Directory&gt;\r\n\r\n\r\n&lt;\/VirtualHost&gt;\r\n\r\n&lt;\/IfModule&gt;<\/pre>\n<p>et appliquer les bonnes permissions :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">sudo setfacl -R -m u:www-data:rX \/usr\/share\/symfony\/\r\nsudo setfacl -R -m u:www-data:rwX\u00a0\/usr\/share\/symfony\/\r\nsudo setfacl -dR -m u:www-data:rwX\u00a0\/usr\/share\/symfony\/<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 -&gt; pdo_sqlite path: %kernel.cache_dir%\/test.db &gt;symfony new comptes &gt;cd comptes\/ &gt;php bin\/console server:run # to test &gt;php bin\/console generate:controller # to create a controller &gt;php bin\/console doctrine:generate:entity # to create entity &gt;php &hellip; <a href=\"https:\/\/blog.brunocsmartin.fr\/index.php\/2017\/08\/20\/symfony-and-co-mposer\/\" class=\"more-link\">Continuer la lecture de <span class=\"screen-reader-text\">Symfony and Co mposer<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-58","post","type-post","status-publish","format-standard","hentry","category-non-classe"],"_links":{"self":[{"href":"https:\/\/blog.brunocsmartin.fr\/index.php\/wp-json\/wp\/v2\/posts\/58"}],"collection":[{"href":"https:\/\/blog.brunocsmartin.fr\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.brunocsmartin.fr\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.brunocsmartin.fr\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.brunocsmartin.fr\/index.php\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":28,"href":"https:\/\/blog.brunocsmartin.fr\/index.php\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/blog.brunocsmartin.fr\/index.php\/wp-json\/wp\/v2\/posts\/58\/revisions\/133"}],"wp:attachment":[{"href":"https:\/\/blog.brunocsmartin.fr\/index.php\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.brunocsmartin.fr\/index.php\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.brunocsmartin.fr\/index.php\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}