少しづつプラグインについていろいろ調べていますが管理画面への新規ページをどうするのか確認することを忘れていました。
と、云うわけで今回は管理画面への新規ページ追加について調べたいと思います。
表示変更などを変更するだけであれば管理画面は不要ではありますが、大掛かりなプラグインになると個別の管理画面は必要となることが多いです。
今回はそんな管理画面についての処理をやってみたいと思います。
・
・
・
出来ました。
ファイル自体は前回に確認したプラグインジェネレーターにて基本的なファイルを作成しています。
ファイル数が多くなりますので基本的には追加・変更したファイルのみの記載となります。
※ 全ファイル欲しいと云う奇特な方がおられる場合、コメントにてご要望下さい(笑)
■プラグインファイル
/ServiceProvider/NakwebTest007ServiceProvider.php
/Controller/NakwebTest007Controller.php
/Form/Type/NakwebTest007Type.php
/Resource/template/admin/index.twig
▼/ServiceProvider/NakwebTest007ServiceProvider.php
<?php
/*
* This file is part of the NakwebTest007
*
* Copyright (C) 2017 ナックウェブテスト007
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\NakwebTest007\ServiceProvider;
use Eccube\Application;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\FingersCrossedHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Plugin\NakwebTest007\Form\Type\NakwebTest007ConfigType;
use Plugin\NakwebTest007\Form\Type\NakwebTest007Type;
use Silex\Application as BaseApplication;
use Silex\ServiceProviderInterface;
use Symfony\Component\Yaml\Yaml;
class NakwebTest007ServiceProvider implements ServiceProviderInterface
{
public function register(BaseApplication $app)
{
// 管理画面用 独自コントローラ
$app->match('/'.$app['config']['admin_route'].'/plugin/NakwebTest007', 'Plugin\NakwebTest007\Controller\NakwebTest007Controller::index')->bind('plugin_NakwebTest007');
// プラグイン用設定画面
$app->match('/'.$app['config']['admin_route'].'/plugin/NakwebTest007/config', 'Plugin\NakwebTest007\Controller\ConfigController::index')->bind('plugin_NakwebTest007_config');
// 独自コントローラ
$app->match('/plugin/[lower_code]/hello', 'Plugin\NakwebTest007\Controller\NakwebTest007Controller::index')->bind('plugin_NakwebTest007_hello');
// Form
$app['form.types'] = $app->share($app->extend('form.types', function ($types) use ($app) {
$types[] = new NakwebTest007ConfigType();
$types[] = new NakwebTest007Type();
return $types;
}));
// Repository
// Service
// メッセージ登録
// $file = __DIR__ . '/../Resource/locale/message.' . $app['locale'] . '.yml';
// $app['translator']->addResource('yaml', $file, $app['locale']);
// load config
// プラグイン独自の定数はconfig.ymlの「const」パラメータに対して定義し、$app['[lower_code]config']['定数名']で利用可能
// if (isset($app['config']['NakwebTest007']['const'])) {
// $config = $app['config'];
// $app['[lower_code]config'] = $app->share(function () use ($config) {
// return $config['NakwebTest007']['const'];
// });
// }
// ログファイル設定
$app['monolog.logger.[lower_code]'] = $app->share(function ($app) {
$logger = new $app['monolog.logger.class']('[lower_code]');
$filename = $app['config']['root_dir'].'/app/log/[lower_code].log';
$RotateHandler = new RotatingFileHandler($filename, $app['config']['log']['max_files'], Logger::INFO);
$RotateHandler->setFilenameFormat(
'[lower_code]_{date}',
'Y-m-d'
);
$logger->pushHandler(
new FingersCrossedHandler(
$RotateHandler,
new ErrorLevelActivationStrategy(Logger::ERROR),
0,
true,
true,
Logger::INFO
)
);
return $logger;
});
}
public function boot(BaseApplication $app)
{
}
}
コンテンツジェネレーターにて作成されていたファイルへの追記になります。
ページ呼び出し部分とフォーム用のタイプ設定の追記ですね。
サンプルコードが記載されているため解りやすいです。
実施にプラグインを作成する際には不要なサンプルコードを作成する必要があるかとは思いますが、とりあえず放置しておきます。(笑)
▼/Controller/NakwebTest007Controller.php
<?php
/*
* This file is part of the NakwebTest007
*
* Copyright (C) 2017 ナックウェブテスト007
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\NakwebTest007\Controller;
use Eccube\Application;
use Symfony\Component\HttpFoundation\Request;
class NakwebTest007Controller
{
/**
* NakwebTest007画面
*
* @param Application $app
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function index(Application $app, Request $request)
{
$form = $app['form.factory']->createBuilder('nakwebtest007')->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
}
return $app->render('NakwebTest007/Resource/template/admin/index.twig', array(
'form' => $form->createView(),
));
}
}
実際の処理を行うコントローラーです。
新規ファイルの作成となります。
今回は管理画面での処理しか想定していないため、既存のコントロールを使用しています。
▼/Form/Type/NakwebTest007Type.php
<?php
/*
* This file is part of the NakwebTest007
*
* Copyright (C) 2017 ナックウェブテスト007
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\NakwebTest007\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
class NakwebTest007Type extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
'label' => '項目A',
'required' => true,
'constraints' => array(
new Assert\NotBlank(),
),
));
}
public function getName()
{
return 'nakwebtest007';
}
}
フォームの入力内容です。
管理画面を作成するにあたり管理画面でフォームを使用しないこともないかと思い導入しています。
実際にはジェネレーターで作成された NakwebTest007ConfigType.php をコピーして作成しています。
▼/Resource/template/admin/index.twig
{% extends 'default_frame.twig' %}
{% set menus = ['store', 'plugin', 'plugin_list'] %}
{% block title %}NakwebTest007画面{% endblock %}
{% block sub_title %}NakwebTest007設定{% endblock %}
{% form_theme form 'Form/bootstrap_3_horizontal_layout.html.twig' %}
{% block main %}
<form class="form-horizontal" method="post" action="{{ url('plugin_NakwebTest007_config') }}">
{{ form_widget(form._token) }}
<div class="row" id="aside_wrap">
<div class="col-md-9">
<div class="box">
<div class="box-header">
<h3 class="box-title">NakwebTest007設定</h3>
</div><!-- /.box-header -->
<div class="box-body form-horizontal">
{{ form_row(form.name) }}
</div><!-- /.box-body -->
</div><!-- /.box -->
</div><!-- /.col -->
<div class="col-md-3" id="aside_column">
<div id="common_box" class="col_inner">
<div id="common_button_box" class="box no-header">
<div id="common_button_box__body" class="box-body">
<div id="common_button_box__edit_button" class="row text-center">
<div class="col-sm-6 col-sm-offset-3 col-md-12 col-md-offset-0">
<button class="btn btn-primary btn-block btn-lg" type="submit">設定</button>
</div>
</div>
</div><!-- /.box-body -->
</div><!-- /.box -->
</div>
</div><!-- /.col -->
</div>
</form>
{% endblock main %}
実際に表示する際の twig ファイルです。
こちらもジェネレーターで作成された config.twig をコピーしています。
さすがジェネレーターですね、驚きました。
ほぼコピーで管理画面の入力フォーム画面が出来てしまいました。
非常に簡単です。
正直に云うと余り期待はしていませんでしたが中々良いものです。
次回以降も少し確認してみる項目を増やしてみようと思います。
・・・タイトルの内容も行いはしましたが実際にはプラグインジェネレーターの確認に終わってしまいました(笑)
とは云え今回はここまでとさせて頂きます。











