Symfony2 Tutorial Part 1: Project Setup
This Post is deprecated! See Symfony 2.1.0 Project Setup
About this tutorial:
In this tutorial I will show you how to setup a new symfony2 project on a local webserver. I use Ubuntu 10.04 with the following packages installed:
acl apache2 apache2-mpm-prefork git-core mysql-server-5.1 php-apc php5 php5-cli php5-intl php5-sqlite php5-xdebug php5-xsl phpmyadmin
Lets get started.
- Download Symfony Standard 2.0.7 (http://symfony.com/download?v=Symfony_Standard_Vendors_2.0.7.tgz).
- Extract.
- Git setup:
- In your project directory run:
git init git add * git commit -m 'initial import'
Hint: If you want to ignore a directory simply create a .gitignore file in which you can specify which files or folders are to be ignored. You can put a .gitignore file in any directory you like
- In your project directory run:
- Setup your webserver:
- Create /etc/apache2/sites-available/symfonywith the following content:
<VirtualHost *:80> ServerName symfony.yourhostname DocumentRoot /path/to/your/Symfony/web DirectoryIndex index.php <Directory /path/to/your/Symfony/web> AllowOverride All Allow from All </Directory> </VirtualHost>
- Run
sudo a2ensite symfony
- Add
127.0.0.1 symfony.yourhostname
to /etc/hosts
- Run
sudo /etc/init.d/apache2 reload
You should check if your server configuration is correct. You can do that with the symfony2 config script: http://symfony.yourhostname/config.php
Now you can access your Symfony2 project via http://symfony.yourhostname and http://symfony.yourhostname/app_dev.php
Hint: you need to enable modrewrite to be able to use http://symfony.yourhostname instead of http://symfony.yourhostname/app.php to enable it simply run:sudo a2enmod rewrite sudo /etc/init.d/apache2 restart
- Create /etc/apache2/sites-available/symfonywith the following content:
- Setup ACL’s:
sudo setfacl -R -m u:www-data:rwx -m u:yourusername:rwx app/logs/ app/cache/ sudo setfacl -dR -m u:www-data:rwx -m u:yourusername:rwx app/logs/ app/cache/
you do this, because your webserver (www-data) and yourself need access to the cache and logs directories. Here you can read how to setup and enable acl’s.
Hint: When you get an empty white page in the production environment, its very often the case that your webserver has no write access in the cache and or logs directories. - Setup your Database:
On your MySQL console run:CREATE DATABASE symfony; GRANT ALL ON symfony.* TO symfony@'localhost' IDENTIFIED BY 'pa$$word';
- Setup your symfony config:
replace the content of app/config/parameters.ini with the following:; These parameters can be imported into other config files ; by enclosing the key with % (like %database_user%) ; Comments start with ';', as in php.ini [parameters] database_driver = pdo_mysql database_host = localhost database_port = database_name = symfony database_user = symfony database_password = pa$$word mailer_transport = smtp mailer_host = localhost mailer_user = mailer_password = locale = en secret = Dah0cie3kaeS8Eipoo9ahxei8voe1IeR
And that’s it. Now you can use your nice new Symfony2 installation.
If you want to know more about the symfony2 project setup, read the official documentation: http://symfony.com/doc/2.0/book/installation.html
Setting up a Symfony2 Project with FOSUserBundle, SonataUserBundle and SonataAdminBundle
Tested with Symfony Standard 2.0.7 (http://symfony.com/download?v=Symfony_Standard_Vendors_2.0.7.tgz)
This is basically a merge of the installation-guides of FOSUserBundle (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md), SonataAdminBundle (http://sonata-project.org/bundles/admin/master/doc/reference/installation.html) and SonataUserBundle (http://sonata-project.org/bundles/user/master/doc/reference/installation.html)
- Add
[FOSUserBundle] git=git://github.com/FriendsOfSymfony/FOSUserBundle.git target=bundles/FOS/UserBundle [SonatajQueryBundle] git=http://github.com/sonata-project/SonatajQueryBundle.git target=/bundles/Sonata/jQueryBundle [SonataAdminBundle] git=http://github.com/sonata-project/SonataAdminBundle.git target=/bundles/Sonata/AdminBundle [MenuBundle] git=https://github.com/KnpLabs/KnpMenuBundle.git target=/bundles/Knp/Bundle/MenuBundle [KnpMenu] git=https://github.com/KnpLabs/KnpMenu.git target=/knp/menu [SonataUserBundle] git=git://github.com/sonata-project/SonataUserBundle.git target=/bundles/Sonata/UserBundle [SonataEasyExtendsBundle] git=git://github.com/sonata-project/SonataEasyExtendsBundle.git target=/bundles/Sonata/EasyExtendsBundle [SonataDoctrineORMAdminBundle] git=http://github.com/sonata-project/SonataDoctrineORMAdminBundle.git target=/bundles/Sonata/DoctrineORMAdminBundleto your deps file. (Don’t forget the trailing newline.)
- Run
php bin/vendors install --reinstall
- Add the namespaces to app/autoload.php
// app/autoload.php $loader->registerNamespaces(array( // ... 'FOS' => __DIR__.'/../vendor/bundles', 'Sonata' => __DIR__.'/../vendor/bundles', 'Application' => __DIR__, 'Knp' => array( __DIR__.'/../vendor/bundles', __DIR__.'/../vendor/knp/menu/src', ), // ... )); - Enable the bundles in app/AppKernel.php
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new FOS\UserBundle\FOSUserBundle(), new Sonata\jQueryBundle\SonatajQueryBundle(), new Sonata\AdminBundle\SonataAdminBundle(), new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(), new Knp\Bundle\MenuBundle\KnpMenuBundle(), new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'), new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(), // ... ); // ... } - Add
# app/config/config.yml fos_user: db_driver: orm firewall_name: main user_class: Application\Sonata\UserBundle\Entity\Userto app/config/config.yml
- Run
php app/console sonata:easy-extends:generate SonataUserBundle
- Add the new Bundle to app/AppKernel.php
// app/AppKernel.php public function registerbundles() { $bundles = array( // Application Bundles // ... new Application\Sonata\UserBundle\ApplicationSonataUserBundle(), // ... ); // ... } - Add
# app/config/routing.yml fos_user_security: resource: "@FOSUserBundle/Resources/config/routing/security.xml" fos_user_profile: resource: "@FOSUserBundle/Resources/config/routing/profile.xml" prefix: /profile fos_user_register: resource: "@FOSUserBundle/Resources/config/routing/registration.xml" prefix: /register fos_user_resetting: resource: "@FOSUserBundle/Resources/config/routing/resetting.xml" prefix: /resetting fos_user_change_password: resource: "@FOSUserBundle/Resources/config/routing/change_password.xml" prefix: /change-password admin: resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml' prefix: /admin _sonata_admin: resource: . type: sonata_admin prefix: /admin soanata_user: resource: '@SonataUserBundle/Resources/config/routing/admin_security.xml' prefix: /admin
to app/config/routing.yml
- Add the following to app/config/security.yml
# app/config/security.yml security: encoders: FOS\UserBundle\Model\UserInterface: sha512 role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] SONATA: - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT # if you are not using acl then this line must be uncommented providers: fos_userbundle: id: fos_user.user_manager firewalls: # -> custom firewall for the admin area of the URL admin: pattern: /admin(.*) form_login: provider: fos_userbundle login_path: /admin/login use_forward: false check_path: /admin/login_check failure_path: null logout: path: /admin/logout anonymous: true # -> end custom configuration # defaut login area for standard users main: pattern: .* form_login: provider: fos_userbundle login_path: /login use_forward: false check_path: /login_check failure_path: null logout: true anonymous: true # ... access_control: # URL of FOSUserBundle which need to be available to anonymous users - { path: ^/_wdt, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/_profiler, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } # -> custom access control for the admin area of the URL - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/admin/login-check$, role: IS_AUTHENTICATED_ANONYMOUSLY } # -> end - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } # Secured part of the site # This config requires being logged for the whole site and having the admin role for the admin part. # Change these rules to adapt them to your needs - { path: ^/admin, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] } - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY } # ... - Update your DB-Schema. Run
php app/console doctrine:schema:update --force
- Now, install the assets from the different bundles:
php app/console assets:install web
- Clear your cache:
php app/console cache:clear
- You can create an admin-user and a test-user with the following commands.
php app/console fos:user:create admin admin@example.com password --super-admin
php app/console fos:user:create testuser test@example.com password
- You need to enable your translator in /app/config.yml.
- Create app/Application/Sonata/UserBundle/Recources/translations/SonataUserBundle.en.ymlwith the following content:
# app/Application/Sonata/UserBundle/Recources/translations/SonataUserBundle.en.yml form: label_username: Username label_email: Email label_plain_password: Password (Plain) label_groups: Groups label_roles: Roles label_locked: Locked label_expired: Expired label_enabled: Enabled label_credentials_expired: Credentials Expired label_name: Name list: label_username: Username label_email: Email label_enabled: Enabled label_locked: Locked label_created_at: Created At label_roles: Roles label_name: Name filter: label_username: Username label_locked: Locked label_email: Email label_id: ID label_name: Name
Remove the AcmeDemoBundle in a Symfony2 project
Tested with Symfony Standard 2.0.6 (http://symfony.com/download?v=Symfony_Standard_Vendors_2.0.6.tgz)
- Delete src/Acme folder.
- Delete
// app/AppKernel.php $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
from app/AppKernel.php
- Delete
# app/config/routing_dev.yml _welcome: pattern: / defaults: { _controller: AcmeDemoBundle:Welcome:index } _demo_secured: resource: "@AcmeDemoBundle/Controller/SecuredController.php" type: annotation _demo: resource: "@AcmeDemoBundle/Controller/DemoController.php" type: annotation prefix: /demofrom app/config/routing_dev.php.
- Clear your cache:
php app/console cache:clear