Professional Documents
Culture Documents
Contact
In this article, I will explain the steps on how to have an Admin Panel in Symfony2 very easily. Despite having several tutorials on the web, I still found that I had to do a lot of work to make it error-free.
Note: We created a boilerplate of Symfony2 with Admin Panel at Github. I recommend to download the Symfony bundled with Sonata Admin Panel Boilerplate instructions there and follow the . You will have Symfony2 with Admin Panel up and running in no time!
Alternatively, If you wish to do manually, follow the steps described below. For windows users, you will need Git Bash Client to follow instructions below.
Step #1: Copy/Add the following lines in your /deps file. Make sure, you keep the version in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 [TwigGenerator] git=git://github.com/cedriclombardot/TwigGenerator.git target=/twig-generator [PagerFanta] git=git://github.com/whiteoctober/Pagerfanta.git target=/pagerfanta [WhiteOctoberPagerfantaBundle] git=git://github.com/whiteoctober/WhiteOctoberPagerfantaBundle.git target=/bundles/WhiteOctober/PagerfantaBundle version=origin/symfony2.0 [doctrine-fixtures] git=git://github.com/doctrine/data-fixtures.git target=/doctrine-fixtures [DoctrineFixturesBundle] git=git://github.com/doctrine/DoctrineFixturesBundle.git target=/bundles/Doctrine/Bundle/FixturesBundle [DoctrineBundle] git=git://github.com/doctrine/DoctrineBundle.git target=/bundles/Doctrine/Bundle/DoctrineBundle [SonataAdminBundle] git=http://github.com/sonata-project/SonataAdminBundle.git target=/bundles/Sonata/AdminBundle version=origin/2.0 [SonataBlockBundle] git=http://github.com/sonata-project/SonataBlockBundle.git target=/bundles/Sonata/BlockBundle version=origin/2.0 [SonataCacheBundle]
?
Symfony2 - Sonata Admin Bundle Install and Boilerplate | Weblog of Abdullah Rubiyath :: Alif
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
git=http://github.com/sonata-project/SonataCacheBundle.git target=/bundles/Sonata/CacheBundle version=origin/2.0 [SonatajQueryBundle] git=http://github.com/sonata-project/SonatajQueryBundle.git target=/bundles/Sonata/jQueryBundle [SonataUserBundle] git=git://github.com/sonata-project/SonataUserBundle.git target=/bundles/Sonata/UserBundle version=origin/2.0 [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/DoctrineORMAdminBundle version=origin/2.0 [KnpMenuBundle] git=http://github.com/KnpLabs/KnpMenuBundle.git target=/bundles/Knp/Bundle/MenuBundle version=v1.0.0 [KnpMenu] git=http://github.com/KnpLabs/KnpMenu.git target=/knp/menu version=v1.0.0 [Exporter] git=http://github.com/sonata-project/exporter.git target=/exporter [SonataDoctrineExtensions] git=git://github.com/sonata-project/sonata-doctrine-extensions.git target=/sonata-doctrine-extensions [FOSUserBundle] git=git://github.com/FriendsOfSymfony/FOSUserBundle.git target=bundles/FOS/UserBundle version=origin/1.2.x [EntityAudit] git=git://github.com/simplethings/EntityAudit.git target=/bundles/SimpleThings/EntityAudit
Step #2: Now, to install the bundles, run the following command:
1 php bin/vendors install
?
Step #3 (optional): If you do not have a database configured yet, open app/config/parameters.ini and update the database Information. Ensure that the database_user has permission to create database, otherwise, you may want to manually the database separately Step #4: Add the following lines in your app/autoload.php
1 2 3 4 5 6 7 $loader->registerNamespaces(array( //... 'FOS' => __DIR__.'/../vendor/bundles', 'Sonata' => array(__DIR__.'/../vendor/bundles', __DIR__.'/../vendor/sonata-doctrine-extensions/src'), 'Knp' => array(__DIR__.'/../vendor/bundles', __DIR__.'/../vendor/knp/menu/src'),
?
Symfony2 - Sonata Admin Bundle Install and Boilerplate | Weblog of Abdullah Rubiyath :: Alif
8 9 10 11 12 13 14 15
'Exporter' => __DIR__.'/../vendor/exporter/lib', 'SimpleThings' => __DIR__.'/../vendor/bundles/SimpleThings/EntityAudit/src', 'WhiteOctober\PagerfantaBundle' => __DIR__.'/../vendor/bundles', 'Pagerfanta' => __DIR__.'/../vendor/pagerfanta/src', //.. ));
Step #6: Now, translator needs to be added. So, uncomment the following lines in app/config/config.yml
1 2 3 4 5 translator: { fallback: %locale% } #Or you can add the following as well translator: { fallback: en }
?
Symfony2 - Sonata Admin Bundle Install and Boilerplate | Weblog of Abdullah Rubiyath :: Alif
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
# default actions templates, should extend a global templates list: SonataAdminBundle:CRUD:list.html.twig show: SonataAdminBundle:CRUD:show.html.twig edit: SonataAdminBundle:CRUD:edit.html.twig dashboard: blocks: # display a dashboard block - { position: left, type: sonata.admin.block.admin_list } sonata_doctrine_orm_admin: # default value is null, so doctrine uses the value defined in the configuration entity_manager: ~ templates: form: - SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig filter: - SonataDoctrineORMAdminBundle:Form:filter_admin_fields.html.twig types: list: array: SonataAdminBundle:CRUD:list_array.html.twig boolean: SonataAdminBundle:CRUD:list_boolean.html.twig date: SonataAdminBundle:CRUD:list_date.html.twig time: SonataAdminBundle:CRUD:list_time.html.twig datetime: SonataAdminBundle:CRUD:list_datetime.html.twig text: SonataAdminBundle:CRUD:base_list_field.html.twig trans: SonataAdminBundle:CRUD:list_trans.html.twig string: SonataAdminBundle:CRUD:base_list_field.html.twig smallint: SonataAdminBundle:CRUD:base_list_field.html.twig bigint: SonataAdminBundle:CRUD:base_list_field.html.twig integer: SonataAdminBundle:CRUD:base_list_field.html.twig decimal: SonataAdminBundle:CRUD:base_list_field.html.twig identifier: SonataAdminBundle:CRUD:base_list_field.html.twig show: array: SonataAdminBundle:CRUD:show_array.html.twig boolean: SonataAdminBundle:CRUD:show_boolean.html.twig date: SonataAdminBundle:CRUD:show_date.html.twig time: SonataAdminBundle:CRUD:show_time.html.twig datetime: SonataAdminBundle:CRUD:show_datetime.html.twig text: SonataAdminBundle:CRUD:base_show_field.html.twig trans: SonataAdminBundle:CRUD:show_trans.html.twig string: SonataAdminBundle:CRUD:base_show_field.html.twig smallint: SonataAdminBundle:CRUD:base_show_field.html.twig bigint: SonataAdminBundle:CRUD:base_show_field.html.twig integer: SonataAdminBundle:CRUD:base_show_field.html.twig decimal: SonataAdminBundle:CRUD:base_show_field.html.twig
Symfony2 - Sonata Admin Bundle Install and Boilerplate | Weblog of Abdullah Rubiyath :: Alif
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
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 }
Step #9: Now, generate a new module using the following cmd from Git Bash/Terminal:
1 php app/console sonata:easy-extends:generate SonataUserBundle
?
Symfony2 - Sonata Admin Bundle Install and Boilerplate | Weblog of Abdullah Rubiyath :: Alif
Step #12: Update the Group in app/Application/Sonata/UserBundle/Entity/Group.php . Basically, add the ORM annotations. Below is the entire class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 namespace Application\Sonata\UserBundle\Entity; use Sonata\UserBundle\Entity\BaseGroup as BaseGroup; use Doctrine\ORM\Mapping as ORM; /** * This file has been generated by the Sonata EasyExtends bundle ( http://sonataproject.org/easy-extends ) * * References : * working with object : http://www.doctrineproject.org/projects/orm/2.0/docs/reference/working-with-objects/en * * @ORM\Entity * @ORM\Table(name="group") */ class Group extends BaseGroup { /** * @var integer $id
?
Symfony2 - Sonata Admin Bundle Install and Boilerplate | Weblog of Abdullah Rubiyath :: Alif
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
* * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * Get id * * @return integer $id */ public function getId() { return $this->id; } }
Step #13: Add the following line in your app/config/config.yml . Basically, this adds a JSON Type to doctrine
1 2 3 4 5 doctrine: dbal: //... types: json: Sonata\Doctrine\Types\JsonType
?
Step #14: Run the following command to create the database tables
1 php app/console doctrine:schema:update -force
?
Step #15: Install the assets for AdminBundle (js/css etc.) by issuing the following command:
1 php app/console assets:install web
?
Symfony2 - Sonata Admin Bundle Install and Boilerplate | Weblog of Abdullah Rubiyath :: Alif
28 29 30 31 32 33 34 35
Step #17: Create a super-admin user to manage the admin panel and you are ready to go!
1 php app/console fos:user:create admintest admin@test.com pass --super-admin
?
Comments
Submitted by AngyGlamy (not verified) on September 20, 2012 Man i guess there is an error in the HTML somewhere? I can't load the site properly !
found a typo:
I found a typo:
Symfony2 - Sonata Admin Bundle Install and Boilerplate | Weblog of Abdullah Rubiyath :: Alif reply
Submitted by alif on August 24, 2012 Yes, you can keep it. It shouldn't cause any problem. Thanks for catching the typo :-).
Submitted by Willy (not verified) on August 22, 2012 Great job on the script. It worked like a charm. Thanks for saving hours of headache.
Submitted by PHP Prog (not verified) on August 14, 2012 Thank You. Any plans for releasing boiler plate for Symfony version 2.1?
The content of this field is kept private and will not be shown publicly.
Symfony2 - Sonata Admin Bundle Install and Boilerplate | Weblog of Abdullah Rubiyath :: Alif
Homepage
Comment *
Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> Lines and paragraphs break automatically.
CAPTCHA This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Save
Preview