aaaiiuie

学習(備忘録)のため運用

Cakephp4でcakelteを適用する方法

CakePHP4環境でAdminLTEを利用したい場合、CakeLTEをインストールすることで、レイアウト構築が楽になるのでその手順を記載。 ※環境はVagrant

GitHub CakeLTE https://github.com/arodu/cakelte

まず初めにcomposerを使用して、cakelteをインストールします。

$ vagrant up
$ vagrant ssh
[vagrant@localhost]$ cd /var/www/html/myapp
[vagrant@localhost]$ composer require arodu/cakelte

シェル コマンドを使用してプラグインをロードできます。

[vagrant@localhost]$ bin/cake plugin load CakeLte

src/Application.php または、アプリケーションのファイルに手動でロード ステートメントを追加することもできます。

public function bootstrap(){
    parent::bootstrap();
    $this->addPlugin('CakeLte');
}

vendor/arodu/cakelte/config/cakelte.phpconfig/cakelte.phpにコピーします。

[vagrant@localhost]$ cp vendor/arodu/cakelte/config/cakelte.php config/cakelte.php

コピーした後にsrc/View/AppView.phpを修正します。

namespace App\View;

use Cake\View\View;
use CakeLte\View\CakeLteTrait;

class AppView extends View{
  use CakeLteTrait;

  public $layout = 'CakeLte.default';

  public function initialize(): void{
      parent::initialize();
      $this->initializeCakeLte();
      //...
  }
}

オプションのレイアウトは以下の通りです。 bakeを利用してレイアウトを自動生成します。

[vagrant@localhost]$ bin/cake bake all [command] -t CakeLte

[vagrant@localhost]$ bin/cake bake template [command] -t CakeLte login
[vagrant@localhost]$ bin/cake bake template [command] -t CakeLte register
[vagrant@localhost]$ bin/cake bake template [command] -t CakeLte recovery

テンプレートを変更するには、vendor/arobu/templates/element/ 以下のファイルを修正します。

レイアウト
src/templates/layout/default.php
src/templates/layout/login.php
src/templates/layout/top-nav.php
コンテンツ
src/templates/element/content/header.php
ヘッダーナビゲーションバー
src/templates/element/header/main.php
src/templates/element/header/menu.php
src/templates/element/header/messages.php
src/templates/element/header/notifications.php
src/templates/element/header/search-default.php
src/templates/element/header/search-block.php
フッター
src/templates/element/footer/main.php

■左サイドバー

src/templates/element/sidebar/main.php
src/templates/element/sidebar/menu.php
src/templates/element/sidebar/search.php
src/templates/element/sidebar/user.php
右サイドバー
src/templates/element/aside/main.php
デバッグへのリンクは以下の通り
echo $this->Html->link(__('CakeLTE debug page'), '/cake_lte/debug' );
// {your-url}/cake_lte/debug