Symfony2。 管理パネルで表形式リストをすばやく準備するための汎用ツール

Symfony2のバンドルについてです。Symfony2の最初のバージョンは2年以上前に書きました。 この間ずっと、同僚と私はそれを積極的に使用し、バンドルは定期的に改善されました。 コミュニティと共有することにしました。

ほとんどすべてのアプリケーションで、エンティティのテーブルリストを表示する必要があります。ページネーションが必要です。また、すべてのフィールドで並べ替えることができ、柔軟なフィルタリングが可能です。 裁判所に提供されたAdminPanelBundleが解決するのはこれらのタスクです。 もちろん、これは新しいものではありません-同じSonataAdminBundleは同様の機能を提供しますが、Sonataは多くの設定と依存関係を持つ(言葉の意味では)モンスターであり、私の目標は大きなテーブル配列を介した高速で柔軟なナビゲーションを実装することでした。

バンドルでできること:

デモはこちら 、ソースコードはこちらです。

インストールと基本構成


いつものように-実行
composer require "zk2/admin-panel-bundle:dev-master" 

バンドルknplabs / knp-paginator-bundlebraincrafted / bootstrap-bundleを使用します。アプリケーションにない場合はインストールされます

KnpPaginatorBundleのセットアップ
app / AppKernel.phpで、バンドルを初期化します
 // app/AppKernel.php public function registerBundles() { return array( // ... new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(), // ... ); } 


BraincraftedBootstrapBundleのセットアップ
app / AppKernel.phpで、バンドルを初期化します
 // app/AppKernel.php public function registerBundles() { return array( // ... new Braincrafted\Bundle\BootstrapBundle\BraincraftedBootstrapBundle(), // ... ); } 

セットアップは、 ここで簡単に説明されています

 # app/config/config.yml ....... # Assetic Configuration assetic: debug: "%kernel.debug%" use_controller: false bundles: [ ] filters: #   node less: node: /usr/bin/node #     $ whereis node node_paths: [/usr/lib/node_modules] # $ whereis node_modules apply_to: "\.less$" cssrewrite: ~ braincrafted_bootstrap: less_filter: less jquery_path: %kernel.root_dir%/../web/js/jquery-1.11.1.js #   jQuery 

次に行うこと:

 php app/console braincrafted:bootstrap:install php app/console assetic:dump 


app / AppKernel.phpでバンドルを初期化し、app / config / config.ymlで必要な設定を追加します。

 // app/AppKernel.php public function registerBundles() { return array( // ... new Zk2\Bundle\AdminPanelBundle\Zk2AdminPanelBundle(), // ... ); } 

 # app/config/config.yml ...... twig: ...... form: resources: - "Zk2AdminPanelBundle:AdminPanel:bootstrap_form_div_layout.html.twig" #     zk2_admin_panel: check_flag_super_admin: false # --  true,       "flagSuperAdmin()",    pagination_template: Zk2AdminPanelBundle:AdminPanel:pagination.html.twig # -    sortable_template: Zk2AdminPanelBundle:AdminPanel:sortable.html.twig # -        

スタイル、アイコンなどをロードします

 php app/console asset:install web --symlink 


使用する


小さなアプリケーション「Cars」の例でデモンストレーションします。
古典的な構造-国->ブランド->モデル
満たされたデータを厳密に判断しないでください-すべてが「懐中電灯から」です。

コントローラーはZk2 \ Bundle \ AdminPanelBundle \ AdminPanel \ AdminPanelControllerを継承する必要があります
親コンストラクターは以下を受け入れます。

 namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Zk2\Bundle\AdminPanelBundle\AdminPanel\AdminPanelController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class DefaultController extends AdminPanelController { /** * Constructor */ public function __construct() { parent::__construct('AppBundle\Entity\Model','m'); } 

listAction-mainメソッド
  /** * listAction * * @Route("/", name="model_list") * * @return renderView */ public function listAction( Request $request ) { //      //  isZk2Granded      //   app/config.yml  zk2_admin_panel.check_flag_super_admin == true, //    " " /* if ( false === $this->isZk2Granded(array('ROLE_LIST')) ) { throw new AccessDeniedException(); }*/ //     if( $this->isReset() ) { return $this->redirect( $this->generateUrl( $this->get('request')->get('_route') ) ); } //    $this->buildListFields(); //        $items = $this->getListFields(); //   $this->getEm()->buildQuery(); //         -- "m,b,c" , //       -- "b.id AS brand_id,b.name AS brand_name,m.name,m.color" //   ,        ,      //         $this->getQuery() ->select( 'b.id AS brand_id,b.name AS brand_name,c.name AS country_name,b.logo,m.id AS id,m.name,' .'m.color,m.airbag,m.sales,m.speed,m.price,m.dateView') ->leftJoin('m.brand','b') ->leftJoin('b.country','c') ; //    if( !$this->get('request')->query->has('sort') ) { $this->getQuery()->orderBy('m.id','DESC'); } //   $this->buildFilterFields(); //   $this->checkFilters(); //  KnpPaginator $pagination = $this->getPaginator(30); //       $filter_form = $this->getViewFiltersForm(); //    -  $this->initAutosum(); $autosum = $this->getSumColumns(); return $this->render('AppBundle:Model:list.html.twig', array( 'results' => $pagination, 'items' => $items, 'filter_form' => $filter_form, //        'is_new' => false, //$this->isZk2Granded(array('ROLE_NEW_ITEM')), 'autosum' => $autosum, //     (PHP::number_format),      'zkNumberFormat' => array('0','.',' '), )); } 

テーブル列の作成:

addInListメソッドは配列を受け取ります。

オプション配列のデフォルト値は次のとおりです。


オプションとその使用に関する詳細は、ソースコードAdminPanelBundle / Resources / views / AdminPanel / adminList.html.twigにあります。

任意のオプションを渡すことができますが、SymfonyのオーバーライドのいずれかでadminList.html.twigテンプレートを再定義し、必要に応じて処理する必要があります。

  /** *    */ public function buildListFields() { $this ->addInList(array( 'name', //   $this->trans('Brand','messages'), //   'b', //   array( //      ,    ( b.name AS brand_name ) //      ,      // (    Model::getBrandName() ) 'method' => 'brand_name', //     ( @Route("/brand/{id}/edit", name="brand_edit") ) 'link_id' => 'brand_edit', //      ,    ( b.id AS brand_id ) //      ,      // (    Model::getBrandId() ) //  link_id ,  lid ,     ID    'lid' => 'brand_id' ), )) ->addInList(array( 'name', $this->trans('Country','messages'), 'c', array( 'method' => 'country_name', ), )) ->addInList(array( 'logo', $this->trans('Logo','messages'), 'b', array( 'sort' => false, 'style' => 'text-align:center', 'icon_path' => '/img/' ), )) ->addInList(array( 'name', $this->trans('Model','messages'), 'm', array( 'link_id' => 'model_edit', ), )) ->addInList(array( 'color', $this->trans('Color','messages'), 'm', array( 'style' => 'text-align:center' ), )) ->addInList(array( 'airbag', $this->trans('Airbag','messages'), 'm', array( 'filter' => 'yes_no', //   ""  "" 'style' => 'text-align:center' ), )) ->addInList(array( 'sales', $this->trans('Sales','messages'), 'm', array( 'autosum' => 'sales_sum', //     'style' => 'text-align:center' ), )) ->addInList(array( 'speed', $this->trans('Max speed','messages'), 'm', array( 'style' => 'text-align:center' ), )) ->addInList(array( 'price', $this->trans('Price','messages'), 'm', array( 'style' => 'text-align:center', 'zkNumberFormat' => array(2,'.',' ') ), )) ->addInList(array( 'dateView', $this->trans('Date','messages'), 'm', array( 'func' => 'dateTimeFormat', //  DateTime 'dateTimeFormat' => 'Ym-d', 'style' => 'text-align:center' ), )) ; } 

フィルターの作成:

addInFilterメソッドは配列を受け取ります:

フィルタータイプ:

  /** *   */ public function buildFilterFields() { $this ->addInFilter(array( // --  ,   'b_name', 'zk2_admin_panel_entity_filter', $this->trans('Brand','messages'), 5, 'smal_int', array( 'entity_type' => 'entity', 'entity_class' => 'AppBundle\Entity\Brand', 'property' => 'name', 'sf_query_builder' => array( //      'alias' => 'b', 'where' => 'b.id IS NOT NULL', 'order_field' => 'b.name', 'order_type' => 'ASC', ) ))) ->addInFilter(array( 'm_name', 'zk2_admin_panel_text_filter', $this->trans('Model','messages'), 5, 'light_text' )) ->addInFilter(array( //  ,  - 'm_color', 'zk2_admin_panel_choice_filter', $this->trans('Color','messages'), 5, 'smal_int', array('sf_choice' => array( 'black' => 'black', 'blue' => 'blue', 'brown' => 'brown', 'green' => 'green', 'red' => 'red', 'silver' => 'silver', 'white' => 'white', 'yellow' => 'yellow', )), )) ->addInFilter(array( 'm_airbag', 'zk2_admin_panel_boolean_filter', $this->trans('Airbag','messages'), )) ->addInFilter(array( 'm_door', 'zk2_admin_panel_text_filter', $this->trans('Number of doors','messages'), 5, 'medium_int' )) ->addInFilter(array( 'm_speed', 'zk2_admin_panel_text_filter', $this->trans('Max speed','messages'), 5, 'medium_int' )) ->addInFilter(array( 'm_prise', 'zk2_admin_panel_text_filter', $this->trans('Price','messages'), 5, 'medium_int' )) ->addInFilter(array( //    'm_dateView', 'zk2_admin_panel_date_filter', $this->trans('Date','messages'), 2 )) ; } 

フォームのメソッド
  /** * edit Brand Action * * @Route("/brand/{id}/edit", name="brand_edit") * * @param Request $request * @param integer $id * * @return renderView */ public function editBrandAction( Request $request, $id ) { ............ } /** * edit Action * * @Route("/model/{id}/edit", name="model_edit") * * @param Request $request * @param integer $id * * @return renderView */ public function editAction( Request $request, $id ) { ..... } } 

さて、非常にシンプルなテンプレート
 # AppBundle:Model:list.html.twig {% extends "Zk2AdminPanelBundle::base.html.twig" %} {% block zk2_title %}Models list{% endblock %} {% block zk2_h %}<h1>General list</h1>{% endblock %} {% block zk2_body %} {% if filter_form %} {% include 'Zk2AdminPanelBundle:AdminPanel:adminFilter.html.twig' with { 'filter_form': filter_form, 'colspan': 2, {# -     #} 'this_path': path('model_list') } %} {% endif %} {% include 'Zk2AdminPanelBundle:AdminPanel:adminList.html.twig' with { 'items': items, 'results': results, 'Zk2NumberFormat': zkNumberFormat } %} {% if is_new %}  "" {% endif %} {% endblock %} 

Source: https://habr.com/ru/post/J250039/


All Articles