symfony快速搭建後颱

2019-07-13 10:48:00
CJL
原創
4681


https://github.com/kevinpapst/AdminLTEBundle AdminLTE bundle for Symfony 4 

https://www.cnblogs.com/forget-remember/p/10172199.html  最近項目需要用到AdminLTE,所以整理一份中文版的小教程

https://symfony.com/doc/master/bundles/SonataAdminBundle/index.html Admin Bundle


爲提高安裝速度切換阿裡composer源

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

詳細蔘考:http://www.centphp.com/view/153


1、創建基礎symfony項目

symfony new --full my_demo_project

詳細蔘考:https://symfony.com/doc/4.2/setup.html

以下操作都需進入項目目録執行


2、添加Admin Bundle

composer require sonata-project/admin-bundle

詳細蔘考:https://sonata-project.org/bundles/admin/3-x/doc/getting_started/installation.html


3、添加 doctrine

composer require sonata-project/doctrine-orm-admin-bundle

詳細蔘考:https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/installation.html


4、創建entity

bin/console make:entity

按提示填寫蔘數進行生成

詳細蔘考:https://symfony.com/doc/4.2/doctrine.html#creating-an-entity-class


5、映射到數據庫

bin/console make:migration
bin/console doctrine:migrations:migrate

先生成數據庫陞級文件,再執行數據庫陞級文件

詳細蔘考:https://symfony.com/doc/4.2/doctrine.html#migrations-creating-the-database-tables-schema


6、啟動服務

配置nginx,蔘考 https://symfony.com/doc/4.2/setup/web_server_configuration.html#nginx

或者直接啟動內置webserver

symfony server:start


7、生成admin管理

 bin/console make:sonata:admin

按提示輸入entity類名、admin類名、controller等信息

生成後檢查service是否配置成功

詳細蔘考:https://sonata-project.org/bundles/admin/3-x/doc/getting_started/creating_an_admin.html


8、瀏覽器訪問可以看到頁麵展示


高級應用:

1、官方文檔:https://sonata-project.org/bundles/admin/3-x/doc/index.html

大部分問題都可以在官方文檔找到答案


2、修改logo和項目名

修改配置文件config/packages/sonata_admin.yaml

sonata_admin:
    title: '代碼覆蓋率分析'
    title_logo: static/logo.jpg


3、默認語言改爲中文

sonata語言配置 修改配置文件:config/packages/sonata_translation.yaml

sonata_translation:
    locales: ['zh_CN', 'en']
    default_locale: 'zh_CN'

整體項目語言配置:config/packages/translation.yaml

framework:
    default_locale: zh_CN
    translator:
        default_path: '%kernel.project_dir%/translations'
        fallbacks:
            - en


4、添加項目語言翻譯

admin類修改使用的語言domain,不修改的話默認語言domain爲messages

public function getTranslationDomain()
{
    return 'AdminEntityMessages';
}

創建語言翻譯文件 translations/AdminEntityMessages.zh_CN.yaml   默認爲translations/messages.zh_CN.yaml

#實體字段名稱定義
Action: 操作
Id: 編號
Name: 名稱
Host: 域名
Dir: 代碼目録
Status: 狀態

前麵是原顯示的英文字段,後麵是中文名稱


5、修改sonata默認的中文翻譯

創建文件translations/SonataAdminBundle.zh_CN.yaml

btn_advanced_filters: 高級搜索
link_filters: 過濾器

原英文名稱可以在源碼中查找


6、列錶頁麵添加自定義操作按鈕

在admin類中修改方法 configureListFields

protected function configureListFields(ListMapper $listMapper): void
{
    $listMapper
        ->add('id')
        ->add('name')
        ->add('host')
        ->add('dir')
        ->add('status', 'choice', [
            'choices' => array_flip(self::$status),
        ])
        ->add('_action', null, [
            'actions' => [
                'report' => [
                    'template' => 'actions/list__action_report.html.twig',
                ],
                'show' => [],
                'edit' => [],
                'delete' => [],
            ],
        ]);
}

其中report是自己添加的操作,通過template 蔘數設置按鈕顯示使用的模闆文件

templates/actions/list__action_report.html.twig

{# templates/CRUD/list__action_report.html.twig #}
<a class="btn btn-sm btn-default view_link" href="{{ url('generateReport', {id: object.id}) }}">生成報錶</a>

twig支持的方法可以通過debug命令查找

bin/console debug:twig

twig語法蔘考:https://www.kancloud.cn/yunye/twig-cn/159454

twig中調試輸齣:https://symfony.com/doc/4.2/templating/debug.html


7、添加左側導航欄按鈕

修改默認配置 config/packages/sonata_admin.yaml

sonata_admin:
    title: '代碼覆蓋率分析'
    title_logo: static/logo.jpg
    templates:
        layout: standard_layout.html.twig

把默認的layout模闆文件改爲我們自己的

templates/standard_layout.html.twig

{% extends '@SonataAdmin/standard_layout.html.twig' %}
{% block side_bar_after_nav %}
    <ul class="sidebar-menu" data-widget="tree">
        <li class="first">
            <a href="{{ path('generatePhpConfig') }}">
                保存配置
            </a>
        </li>
    </ul>
{% endblock %}


8、左側導航設置爲頂級菜單,去除二級菜單

修改config/packages/sonata_admin.yaml

sonata_admin:
    dashboard:
        blocks:
            - { type: sonata.admin.block.admin_list, position: left }
        groups:
            settings:
                label: 任務配置
                label_catalogue: ~
                on_top: true
                items:
                    - admin.task

在菜單配置中添加 on_top: true



發錶評論
評論通過審核後顯示。
流量統計