在 Ubuntu 安裝 Symfony2 + SonataAdminBundle 記錄

edited 十月 2013 in 程式架構
文件很多連來連去或是少了一些細節,所以達到這一步好像花了不少時間...

SonataAdminBundle 有興趣可以看看官方文件介紹
http://sonata-project.org/bundles/admin/master

下面是操作記錄, #後面是註解
$ pwd #操作位置
/home/kiang/public_html/demo
$ wget http://symfony.com/download?v=Symfony_Standard_Vendors_2.0.9.tgz #下載程式
$ tar -xzf download\?v\=Symfony_Standard_Vendors_2.0.9.tgz #解壓縮
$ cd Symfony
$ ls #解壓縮後目錄
app bin deps deps.lock LICENSE README.md src vendor web
$ chmod -R 777 app/cache/ app/logs/ #調整目錄權限
$ cd bin/
$ wget https://gist.github.com/raw/1590354/fc1189850eda80edad7a080be0c238a64f4ebdfd/vendors_export #下載一個小工具
$ chmod 755 vendors_export #修改指令權限
$ cd ../
$ nano deps #調整 deps 檔案,加入下面這些
[FOSUserBundle]
    git=git://github.com/FriendsOfSymfony/FOSUserBundle.git
    target=bundles/FOS/UserBundle

[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

[SonataAdminBundle]
    git=git://github.com/sonata-project/SonataAdminBundle.git
    target=/bundles/Sonata/AdminBundle

[SonatajQueryBundle]
    git=http://github.com/sonata-project/SonatajQueryBundle.git
    target=/bundles/Sonata/jQueryBundle

[KnpMenuBundle]
    git=https://github.com/KnpLabs/KnpMenuBundle.git
    target=/bundles/Knp/Bundle/MenuBundle

[KnpMenu]
    git=https://github.com/KnpLabs/KnpMenu.git
    target=/knp/menu

[SonataDoctrineORMAdminBundle]
    git=http://github.com/sonata-project/SonataDoctrineORMAdminBundle.git
    target=/bundles/Sonata/DoctrineORMAdminBundle
$ ./bin/vendors_export #執行小工具來下載必要程式
$ nano app/autoload.php #編輯 app/autoload.php ,加入下面內容
$loader->registerNamespaces(array(
    // ...
    new  Sonata\jQueryBundle\SonatajQueryBundle(),
    new  Knp\Bundle\MenuBundle\KnpMenuBundle(),
    new  Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
    new  FOS\UserBundle\FOSUserBundle(),
    new  Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
    new  Sonata\AdminBundle\SonataAdminBundle(),
    new  Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(),
    // ...
));

$ nano app/AppKernel.php #編輯 app/AppKernel.php ,加入下面內容
public function registerBundles()
{
    return array(
        // ...
        'FOS' => __DIR__.'/../vendor/bundles',
        'Sonata'        => __DIR__.'/../vendor/bundles',
        'Application'   => __DIR__,
        'Knp\Bundle' => __DIR__.'/../vendor/bundles',
        'Knp\Menu'   => __DIR__.'/../vendor/knp/menu/src',
        // ...
    );
}

$ nano app/config/config.yml #編輯 app/config/config.yml ,加入下面內容
fos_user:
    db_driver: orm
    firewall_name: main
    user_class: Application\Sonata\UserBundle\Entity\User

$ ./app/console sonata:easy-extends:generate SonataUserBundle #執行指令來產生需要的 Entity

$ nano app/autoload.php #編輯 app/AppKernel.php ,加入下面內容
$loader->registerNamespaces(array(
    // ...
    new  Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
    // ...
));

$ ./app/console assets:install web #安裝 bundles 的靜態檔案(圖片、js等等)
$ sudo chmod -R 777 app/cache/ app/logs/ #修改權限,因為剛剛的操作可能會產生一些快取資料讓網頁伺服器無法存取
$ nano app/config/routing.yml #編輯 app/config/routing.yml ,加入下面內容
homepage:
    pattern:   /
    defaults:  { _controller: SonataAdminBundle:Core:dashboard }

admin:
    resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix: /admin

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

sonata_user:
    resource: '@SonataUserBundle/Resources/config/routing/admin_security.xml'
    prefix: /admin

=> 透過瀏覽器打開 http://localhost/~kiang/demo/Symfony/web/app_dev.php/_configurator/ ,藉此產生資料庫連線設定

$ nano app/config/parameters.ini #編輯app/config/parameters.ini ,將產生的設定貼進去
[parameters]
    database_driver="pdo_mysql"
    database_host="localhost"
    database_port=""
    database_name="kiang_symfony"
    database_user="root"
    database_password="password"
    mailer_transport="smtp"
    mailer_host="localhost"
    mailer_user=""
    mailer_password=""
    locale="en"
    secret="70679749e8855305157eaefa2063c2f388cc1eb8"

=> 透過瀏覽器打開 http://localhost/~kiang/demo/Symfony/web/app_dev.php/admin/dashboard ,點選 users -> link_add ,建立一個角色為 ROLE_SUPER_ADMIN 的管理者帳號

$ nano app/config/security.yml #編輯 app/config/security.yml ,改為下面這樣
security:
    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
            
    encoders:
        Application\Sonata\UserBundle\Entity\User:
            algorithm: sha512
            encode-as-base64: true
            iterations: 10

    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
                use_referer:    true
            logout:
                path:           /admin/logout
                target:         /admin/login

            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 }

到這裡,已經有基本的使用者與群組管理功能,進一步的操作就可以開始參考相關文件

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=6492
Sign In or Register to comment.