{"id":625,"date":"2013-02-05T03:35:13","date_gmt":"2013-02-05T03:35:13","guid":{"rendered":"http:\/\/invisiblezero.net\/?p=311"},"modified":"2013-02-05T03:35:13","modified_gmt":"2013-02-05T03:35:13","slug":"sonata-fos-user-as-a-base","status":"publish","type":"post","link":"http:\/\/ndthanh.com\/sonata-fos-user-as-a-base\/","title":{"rendered":"Sonata & FOS user as a base"},"content":{"rendered":"

SonataAdminBundle + FOSUserBundle: Have a GOOD base project
\nThis article is going to explain \/how to\/ correctly create a backoffice<\/p>\n

(admin panel + crud) in Symfony 2.<\/p>\n

Generally, for any web project, you need a back office to manager<\/p>\n

entities, data, users and groups and, fortunately today there are a<\/p>\n

number of bundles for Symfony 2 which allow you to create an admin panel<\/p>\n

quickly.<\/p>\n

<\/p>\n

(.. because it is boring to redevelop phpmyadmin we can save ourselves<\/p>\n

the effort and reutilise existing Symfony2 bundles)<\/p>\n

What bundles do you need to install?<\/p>\n

\n[FOSUserBundle]\ngit=git:\/\/github.com\/FriendsOfSymfony\/FOSUserBundle.git\ntarget=bundles\/FOS\/UserBundle\n\n[SonatajQueryBundle]\ngit=https:\/\/github.com\/sonata-project\/SonatajQueryBundle.git\ntarget=\/bundles\/Sonata\/jQueryBundle\n\n[SonataAdminBundle]\ngit=https:\/\/github.com\/sonata-project\/SonataAdminBundle.git\ntarget=\/bundles\/Sonata\/AdminBundle\n\n[MenuBundle]\ngit=https:\/\/github.com\/KnpLabs\/KnpMenuBundle.git\ntarget=\/bundles\/Knp\/Bundle\/MenuBundle\n\n[KnpMenu]\ngit=https:\/\/github.com\/KnpLabs\/KnpMenu.git\ntarget=\/knp\/menu\n\n[SonataUserBundle]\ngit=git:\/\/github.com\/sonata-project\/SonataUserBundle.git\ntarget=\/bundles\/Sonata\/UserBundle\n\n[SonataEasyExtendsBundle]\ngit=git:\/\/github.com\/sonata-project\/SonataEasyExtendsBundle.git\ntarget=\/bundles\/Sonata\/EasyExtendsBundle\n\n[SonataDoctrineORMAdminBundle]\ngit=https:\/\/github.com\/sonata-project\/SonataDoctrineORMAdminBundle.git\ntarget=\/bundles\/Sonata\/DoctrineORMAdminBundle\n<\/pre>\n

Add the namespaces to app\/autoload.php<\/p>\n

\n\/\/ app\/autoload.php\n$loader->registerNamespaces(array(\n\/\/ ...\n'FOS'\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 => __DIR__.'\/..\/vendor\/bundles',\n\n'Sonata'\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 => __DIR__.'\/..\/vendor\/bundles',\n\n'Application'\u00a0\u00a0\u00a0\u00a0\u00a0 => __DIR__,\n\n'Knp'\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 => array(\n\n__DIR__.'\/..\/vendor\/bundles',\n__DIR__.'\/..\/vendor\/knp\/menu\/src',\n),\n\/\/ ...\n));\n<\/pre>\n

Enable the bundles in app\/AppKernel.php<\/p>\n

\n\/\/ app\/AppKernel.php\npublic function registerBundles()\n{\n$bundles = array(\n\/\/ ...\nnew FOS\\UserBundle\\FOSUserBundle(),\nnew Sonata\\jQueryBundle\\SonatajQueryBundle(),\nnew Sonata\\AdminBundle\\SonataAdminBundle(),\nnew Sonata\\DoctrineORMAdminBundle\\SonataDoctrineORMAdminBundle(),\nnew Knp\\Bundle\\MenuBundle\\KnpMenuBundle(),\nnew Sonata\\UserBundle\\SonataUserBundle('FOSUserBundle'),\nnew Sonata\\EasyExtendsBundle\\SonataEasyExtendsBundle(),\n\/\/ ...\n);\n\/\/ ...\n}\n<\/pre>\n

Add<\/p>\n

\n# app\/config\/config.yml\nfos_user:\ndb_driver: orm\nfirewall_name: main\nuser_class: Application\\Sonata\\UserBundle\\Entity\\User\n<\/pre>\n

Run<\/p>\n

\nphp app\/console sonata:easy-extends:generate SonataUserBundle\n<\/pre>\n

It is going to generate an Application UserBundle, but you can use your own.<\/p>\n

To develop your understanding, do this by default and try after to readapt the code with your UserBundle<\/p>\n

Add the new Bundle to app\/AppKernel.php<\/p>\n

\n\/\/ app\/AppKernel.php\npublic function registerbundles()\n{\n$bundles = array(\n\/\/ Application Bundles\n\/\/ ...\nnew Application\\Sonata\\UserBundle\\ApplicationSonataUserBundle(),\n\/\/ ...\n);\n\/\/ ...\n}\n<\/pre>\n

Add routing<\/p>\n

\n# app\/config\/routing.yml\nfos_user_security:\nresource: "@FOSUserBundle\/Resources\/config\/routing\/security.xml"\n\nfos_user_profile:\nresource: "@FOSUserBundle\/Resources\/config\/routing\/profile.xml"\nprefix: \/profile\n\nfos_user_register:\nresource: "@FOSUserBundle\/Resources\/config\/routing\/registration.xml"\nprefix: \/register\n\nfos_user_resetting:\nresource: "@FOSUserBundle\/Resources\/config\/routing\/resetting.xml"\nprefix: \/resetting\n\nfos_user_change_password:\nresource: "@FOSUserBundle\/Resources\/config\/routing\/change_password.xml"\nprefix: \/change-password\n\nadmin:\nresource: '@SonataAdminBundle\/Resources\/config\/routing\/sonata_admin.xml'\nprefix: \/admin\n\n_sonata_admin:\nresource: .\ntype: sonata_admin\nprefix: \/admin\n\nsoanata_user:\nresource: '@SonataUserBundle\/Resources\/config\/routing\/admin_security.xml'\nprefix: \/admin\n\nsonata_user_impersonating:\npattern: \/\ndefaults: { _controller: SonataPageBundle:Page:catchAll }\n<\/pre>\n

Add the following to app\/config\/security.yml<\/p>\n

\n# app\/config\/security.yml\nsecurity:\nencoders:\nFOS\\UserBundle\\Model\\UserInterface: sha512\n\nrole_hierarchy:\nROLE_ADMIN:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ROLE_USER\nROLE_SUPER_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]\nSONATA:\n- ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT\u00a0 # if you are not using acl then this line must be uncommented\n\nproviders:\nfos_userbundle:\nid: fos_user.user_manager\n\nfirewalls:\n\n# -> custom firewall for the admin area of the URL\nadmin:\npattern:\u00a0\u00a0\u00a0\u00a0\u00a0 \/admin(.*)\nform_login:\nprovider:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fos_userbundle\nlogin_path:\u00a0\u00a0\u00a0\u00a0 \/admin\/login\nuse_forward:\u00a0\u00a0\u00a0 false\ncheck_path:\u00a0\u00a0\u00a0\u00a0 \/admin\/login_check\nfailure_path:\u00a0\u00a0 null\nlogout:\npath:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/admin\/logout\nanonymous:\u00a0\u00a0\u00a0 true\n# -> end custom configuration\n\n# defaut login area for standard users\nmain:\npattern:\u00a0\u00a0\u00a0\u00a0\u00a0 .*\nform_login:\nprovider:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fos_userbundle\nlogin_path:\u00a0\u00a0\u00a0\u00a0 \/login\nuse_forward:\u00a0\u00a0\u00a0 false\ncheck_path:\u00a0\u00a0\u00a0\u00a0 \/login_check\nfailure_path:\u00a0\u00a0 null\nlogout:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 true\nanonymous:\u00a0\u00a0\u00a0 true\n\n# ...\n\naccess_control:\n# URL of FOSUserBundle which need to be available to anonymous users\n- { path: ^\/_wdt, role: IS_AUTHENTICATED_ANONYMOUSLY }\n- { path: ^\/_profiler, role: IS_AUTHENTICATED_ANONYMOUSLY }\n- { path: ^\/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }\n\n# -> custom access control for the admin area of the URL\n- { path: ^\/admin\/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }\n- { path: ^\/admin\/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }\n- { path: ^\/admin\/login-check$, role: IS_AUTHENTICATED_ANONYMOUSLY }\n# -> end\n\n- { path: ^\/register, role: IS_AUTHENTICATED_ANONYMOUSLY }\n- { path: ^\/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }\n\n# Secured part of the site\n# This config requires being logged for the whole site and having the admin role for the admin part.\n# Change these rules to adapt them to your needs\n- { path: ^\/admin, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }\n- { path: ^\/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }\n# ...\n<\/pre>\n

Do theses commands for the end<\/p>\n

\nphp app\/console doctrine:schema:update --force\nphp app\/console assets:install web\nphp app\/console cache:clear\nphp app\/console fos:user:create admin admin@example.com password --super-admin\n<\/pre>\n

You need to enable your translator in \/app\/config.yml<\/p>\n

\nframework:\ntranslator: ~\n<\/pre>\n

That\u2019s it!!!<\/p>\n

As you can see, a lotof things must be done for it to<\/p>\n

work<\/p>\n

But after this initial step you will only need to modify the XML files<\/p>\n

and the Admin class ((plural?)) of your new bundles.<\/p>\n

With this demo you should have a functioning admin interface for \u00ab user \u00bb<\/p>\n

and \u00ab group \u00bb entities. This is accomplished only with the following 2<\/p>\n

files:<\/p>\n

– Setup UserAdmin Class (found in AdminFolder in the bundle SonataUserBundle) (see more info in SonataAdminBundle Doc)<\/p>\n

– Setup admin_orm.xml (found in Ressources\/coinfig in the bundle SonataUserBundle) (see more info in SonataAdminBundle Doc)<\/p>\n

When you need to administer more entities in your application, editing<\/p>\n

these two files is all you will need to do.<\/p>\n

For more information, see: http:\/\/sonata-project.org\/bundles\/admin\/master\/doc\/index.html<\/p>\n","protected":false},"excerpt":{"rendered":"

SonataAdminBundle + FOSUserBundle: Have a GOOD base project This article is going to explain \/how to\/ correctly create a backoffice (admin panel + crud) in Symfony 2. Generally, for any web project, you need a back office to manager entities, data, users and groups and, fortunately today there are a number of bundles for Symfony…<\/p>\n

Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"aioseo_notices":[],"views":5,"_links":{"self":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/625"}],"collection":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/comments?post=625"}],"version-history":[{"count":0,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/625\/revisions"}],"wp:attachment":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/media?parent=625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/categories?post=625"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/tags?post=625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}