EC-CUBE独自プラグイン内で競合したController.phpを統合

# 基本環境
EC-CUBE 4.1.2-p2

独自プラグイン
OriginalPlugin

２つのRepository → １つの　Controller
OriginalPluginRepository
CustomPluginRepository
→
OriginalPluginController

# 競合してエラー

OriginalPluginController.php

```:root/app/Plugin/OriginalPlugin/Controller/Admin/OriginalPluginController.php
<?php

namespace Plugin\OriginalPlugin\Controller\Admin;

use Eccube\Controller\AbstractController;
use Plugin\OriginalPlugin\Form\Type\Admin\OriginalPluginType;
use Plugin\OriginalPlugin\Repository\OriginalPluginRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class OriginalPluginController extends AbstractController
{
    

    private $originalPluginRepository;

   
    public function __construct(
        OriginalPluginRepository $originalPluginRepository)
    {
        $this->originalPluginRepository = $originalPluginRepository;
    }

    /**
     * @Route("/%eccube_admin_route%/original_plugin", name="admin_setting_shop_original_plugin")
     * @Template("@originalPlugin/admin/OriginalPlugin.twig")
     */
    public function index(Request $request)
    {
        $OriginalPlugin = $this->originalPluginRepository->find(1);
        $builder = $this->formFactory->createBuilder(OriginalPluginType::class,$OriginalPlugin);
        $form = $builder->getForm();
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            log_info('送料編集開始');
            $OriginalPlugin = $form->getData();

            log_info('送料登録開始');
            try {
                $this->entityManager->persist($OriginalPlugin);
                $this->entityManager->flush($OriginalPlugin);
                log_info('送料登録完了');
            } catch(\Exception $e) {
                log_error('送料登録エラー',[
                    'err' => $e
                ]);
            }

            $this->addSuccess('admin.common.save_complete', 'admin');

            return $this->redirectToRoute('admin_setting_shop_original_plugin');

        }

        return [
            'form' => $form->createView(),
            'OriginalPlugin' => $OriginalPlugin,
        ];
    }
}
```
CustomPluginController.php

```:root/app/Plugin/OriginalPlugin/Controller/Admin/CustomPluginController.php
<?php

namespace Plugin\OriginalPlugin\Controller\Admin;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Plugin\OriginalPlugin\Repository\CustomPluginRepository;



class CustomPluginController extends AbstractController
{
   /**
   * @var CustomPluginRepository
   */
  protected $customPluginRepository;

  /**
   * @param CustomPluginRepository $customPluginRepository
   */
    
public function __construct(CustomPluginRepository $customPluginRepository) 
      {

        $this->CustomPluginRepository = $customPluginRepository;

      }

    /**
     * @Route("/%eccube_admin_route%/original_plugin", name="admin_setting_shop_original_plugin")
     * @Template("@originalPlugin/admin/OriginalPlugin.twig")
     */
    // プラグイン設定データ
        
public function customindex(Request $request)
      {
        $customPlugin = $this->CustomPluginRepository->findData();
        return $this->render('CustomPlugin/Resource/template/admin/CustomPlugin.twig', ['customPlugin' => $customPlugin ]);
      }
}
```

# 統合する

### useの不足分をコピペ

```
use Eccube\Controller\AbstractController;
use Plugin\OriginalPlugin\Form\Type\Admin\OriginalPluginType;
use Plugin\OriginalPlugin\Repository\OriginalPluginRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
```
プラス

```
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use use Plugin\OriginalPlugin\Repository\CustomPluginRepository;
```

### class内にRepository設定を統合

```
private $originalPluginRepository;
```
プラス

```
/**
   * @var CustomPluginRepository
   */
  protected $customPluginRepository;

  /**
   * @param CustomPluginRepository $customPluginRepository
   */
```

### public function __construct　を統合

```
public function __construct(
        OriginalPluginRepository $originalPluginRepository)
    {
        $this->originalPluginRepository = $originalPluginRepository;
    }
```
プラス

```
public function __construct(CustomPluginRepository $customPluginRepository) 
      {
        $this->CustomPluginRepository = $customPluginRepository;
      }
```
統合すると

```
public function __construct(
        OriginalPluginRepository $originalPluginRepository, CustomPluginRepository $customPluginRepository)
    {
        $this->originalPluginRepository = $originalPluginRepository;
        $this->CustomPluginRepository = $customPluginRepository;
    }
```

### @Route は共用

```
/**
     * @Route("/%eccube_admin_route%/original_plugin", name="admin_setting_shop_original_plugin")
     * @Template("@originalPlugin/admin/OriginalPlugin.twig")
     */
```

### public function index()を統合

```
public function index(Request $request)
    {
        $OriginalPlugin = $this->originalPluginRepository->find(1);
        $builder = $this->formFactory->createBuilder(OriginalPluginType::class,$OriginalPlugin);
        $form = $builder->getForm();
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            log_info('送料編集開始');
            $OriginalPlugin = $form->getData();

            log_info('送料登録開始');
            try {
                $this->entityManager->persist($OriginalPlugin);
                $this->entityManager->flush($OriginalPlugin);
                log_info('送料登録完了');
            } catch(\Exception $e) {
                log_error('送料登録エラー',[
                    'err' => $e
                ]);
            }

            $this->addSuccess('admin.common.save_complete', 'admin');

            return $this->redirectToRoute('admin_setting_shop_original_plugin');

        }

        return [
            'form' => $form->createView(),
            'OriginalPlugin' => $OriginalPlugin,
        ];
    }
}
```
プラス

```
public function customindex(Request $request)
      {
        $customPlugin = $this->CustomPluginRepository->findData();
        return $this->render('CustomPlugin/Resource/template/admin/CustomPlugin.twig', ['customPlugin' => $customPlugin ]);
      }
}
```
統合すると

```
public function index(Request $request)
    {
    $OriginalPlugin = $this->originalPluginRepository->find(1);
    $customPlugin = $this->CustomPluginRepository->findData();
        $builder = $this->formFactory->createBuilder(OriginalPluginType::class,$OriginalPlugin);
        $form = $builder->getForm();
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            log_info('送料編集開始');
            $OriginalPlugin = $form->getData();

            log_info('送料登録開始');
            try {
                $this->entityManager->persist($OriginalPlugin);
                $this->entityManager->flush($OriginalPlugin);
                log_info('送料登録完了');
            } catch(\Exception $e) {
                log_error('送料登録エラー',[
                    'err' => $e
                ]);
            }

            $this->addSuccess('admin.common.save_complete', 'admin');

            return $this->redirectToRoute('admin_setting_shop_original_plugin');

        }

        return $this->render('CustomPlugin/Resource/template/admin/CustomPlugin.twig', [
            'form' => $form->createView(),
            'OriginalPlugin' => $OriginalPlugin,
            'customPlugin' => $customPlugin
        ]);
    }
}
```

### 無事機能！