EC-CUBE3でページ管理から画面作成時にURLからuser_dataを無くす方法

EC-CUBE3にあるページ管理より新しいページを作成した場合、URLには必ずuser_dataが含まれます。

http://hogehoge/user_data/hoge

というようになります。

user_dataは別にあっても問題はありませんが、中には無くしてしまいたいという方もいると思います。以下のカスタマイズを行うことで実現可能となります。

EC-CUBE3.0.14を対象に説明します。

まず、FrontControllerProviderにあるuser_dataを定義しているルーティングの箇所をコメントアウトします。

  • src/Eccube/ControllerProvider/FrontControllerProvider.php.php
// user定義
// $c->match('/'.$app['config']['user_data_route'].'/{route}', '\Eccube\Controller\UserDataController::index')->assert('route', '([0-9a-zA-Z_\-]+\/?)+(?<!\/)')->bind('user_data');

ページ作成時にuser_dataの定義がなくても動作するように、PageControllerとPageLayoutRepositoryを修正します。

  • src/Eccube/Controller/Admin/Content/PageController.php
・120行目
// ファイル生成・更新
$templatePath = $app['eccube.repository.page_layout']->getWriteTemplatePath($editable);
↓
$templatePath = $app['eccube.repository.page_layout']->getWriteTemplatePath(false);


・156行目
$templatePath = $app['eccube.repository.page_layout']->getWriteTemplatePath($editable);
↓
$templatePath = $app['eccube.repository.page_layout']->getWriteTemplatePath(false);


・187行目
$templatePath = $app['eccube.repository.page_layout']->getWriteTemplatePath(true);
↓
$templatePath = $app['eccube.repository.page_layout']->getWriteTemplatePath(false);

というようにそれぞれ$app['eccube.repository.page_layout']->getWriteTemplatePath(false);と変更します。

  • src/Eccube/Repository/PageLayoutRepository.php
public function getReadTemplateFile($fileName, $isUser = false)
{
    if ($isUser) {
        $readPaths = array(
            $this->app['config']['user_data_realdir'],
        );
    } else {
        $readPaths = array(
            $this->app['config']['template_realdir'],
            $this->app['config']['template_default_realdir'],
        );
    }
・
・
・

と記述されている箇所を

public function getReadTemplateFile($fileName, $isUser = false)
{
    if ($isUser) {
        $readPaths = array(
            $this->app['config']['template_realdir'],
            $this->app['config']['user_data_realdir'],
        );
    } else {
        $readPaths = array(
            $this->app['config']['template_realdir'],
            $this->app['config']['template_default_realdir'],
        );
    }
・
・
・

$this->app['config']['template_realdir'],を263行目に追加します。

また、管理画面でのURL表記を変更するため、page_edit.twigも修正します。

  • app/template/admin/Content/page_edit.twig
・59行目
{{ url('top') }}{{ app.config.user_data_route }}/{{ form_widget(form.url) }}
↓
{{ url('homepage') }}{{ form_widget(form.url) }}

こちらはuser_dataを表示上からなくしているだけです。

ここまで対応すればほぼ完了なのですが、FrontControllerProviderからuser_dataへルーティング定義を削除してしまったため、新たに定義してあげる必要があります。どこに定義するのかというと、index.phpで定義してあげます。

  • html/index.php
// インストールされてなければインストーラにリダイレクト
if (isset($app['config']['eccube_install']) && $app['config']['eccube_install']) {
    $app->initialize();
    $app->initializePlugin();
    if ($app['config']['http_cache']['enabled']) {
        $app['http_cache']->run();
    } else {
        $app->run();
    }
} else {
・
・
・

と記載されている箇所を

// インストールされてなければインストーラにリダイレクト
if (isset($app['config']['eccube_install']) && $app['config']['eccube_install']) {
    $app->initialize();
    $app->initializePlugin();
 
    // ページレイアウトのカスタマイズに伴う修正
    $front = $app['controllers_factory'];
    // 強制SSL
    if ($app['config']['force_ssl'] == \Eccube\Common\Constant::ENABLED) {
        $front->requireHttps();
    }
 
    $front->match('/{route}', '\Eccube\Controller\UserDataController::index')->assert('route', '([0-9a-zA-Z_\-]+\/?)+(?<!\/)')->bind('user_data');
    $app->mount('', $front);
 
    if ($app['config']['http_cache']['enabled']) {
        $app['http_cache']->run();
    } else {
        $app->run();
    }
} else {
・
・
・

というように新たにuser_data用のルーティング定義を行います。

以上の作業を行うことで、ページ管理から新たなページを作成した場合、URLにはuser_dataはなくなります。

必要な方には必要なカスタマイズだと思いますので、user_dataをURLから無くしてしまいたい!という方は是非ご利用ください。