HHeLiBeXの日記 正道編

日々の記憶の記録とメモ‥

Zend FrameworkからLaminasに移行する話(1)

はじめに

お仕事で、Zend Frameworkのバージョンアップをしなければならなくなった・・と思ったら、Zend Frameworkはもうなくて、Laminas Projectに移って新たなフレームワークとして公開されている。

いちから勉強しないといけないじゃん、ということで、必要な要件を満たせるかどうかを一歩ずつ調査していく。

要件(1)

まず最初の要件は、「Smartyが使えること」。

元のソースコードSmartyをバリバリ使っているので、これを別のビューエンジンに移植するのは大変、ということで、Smartyが使えるならありがたい。プラグインなども非常に便利だし。

導入

まずはプロジェクトの作成。

composer create-project -sdev laminas/laminas-mvc-skeleton laminas-setup-1-smarty
cd laminas-setup-1-smarty

続いて、作成したプロジェクトに対して必要なコンポーネントをインストールしていく。

composer require divix1988/laminas-smarty-module

これで「smarty/smarty (v3.1)」もインストールされる。(最新はv5.3とかみたいだけど、とりあえず)

vendor/smarty/smarty/libs/sysplugins/smartycompilerexception.phpに1つバグがあるので、以下のように修正する。

    /**
     * The line number of the template error
     *
     * @type int|null
     */
    public $line = null;

    /**
     * The line number of the template error
     *
     * @type int
     */
    public int $line = 0;

に修正する。

設定

module/Application/config/module.config.phpview_managerを以下の通りに修正する。

参照するテンプレートファイルのファイル名を変更。

            'application/index/index' => __DIR__ . '/../view/application/index/index.tpl',

Strategyを追加。

        'strategies' => [
            'Smarty\\View\\Strategy',
        ],

修正後の内容は以下のような感じ。

  :
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.tpl',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
        'strategies' => [
            'Smarty\\View\\Strategy',
        ],
    ],
  :

config/modules.config.phpに以下を追加する。

    'Smarty',

追加後の内容は以下のような感じ。

 :
return [
    'Laminas\Router',
    'Laminas\Validator',
    'Application',
    'Smarty',
];

Smartyが使用するディレクトリの作成とパーミッション設定を行う。

mkdir -p data/smarty/templates_c/
chmod a+w data/smarty/templates_c/

コントローラーの追加

今回はSmartyのテンプレートファイルでビューが構築できることの確認が主目的なので、簡単なコントローラーを用意する。

もともとあるmodule/Application/src/Controller/IndexController.phpを以下のように修正する。

<?php

declare(strict_types=1);

namespace Application\Controller;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        $timeStr = date('Y/m/d H:i:s');
        return new ViewModel(['time_str' => $timeStr]);
    }
}

続いてテンプレートファイル。module/Application/view/application/index/index.tplを以下の内容で作成する。

PHP version: {$smarty.const.PHP_VERSION}
<br />
Time: {$time_str|escape:"html"}

確認

http://192.168.56.xxx/laminas-setup-1-smarty/public/にアクセスして、以下のような画面が出れば完成。

laminas-setup-1-smarty-1

まとめ

  • LaminasでSmartyは使える。
  • LaminasとSmartyエンジンをつなぐアダプターは既に用意されているので、導入は簡単。