CakePHP 2.xでminify

やりたいこと

CakePHP 2.Xでcssとjsをひとまとめにして圧縮したい。

 

方法

Minify plugin for CakePHPを使ってみた。

Readmeに従い、今回はcomposerを使ってインストールした。

composer install

で警告が出たので

Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.

一旦、updateをし、改めてinstallを実行した。

composer update --lock
composer install

 

 テンプレートにはどう記述するか?

マニュアルの通り、

echo $this->Minify->css(array('default', 'global'));
echo $this->Minify->script(array('jquery', 'interface'));

と書けば良いのだけど、レイアウトファイルを使用している場合はどうしたら良いのか?

こう書いていたのを、

/app/View/Layouts/default.ctp

<!DOCTYPE html>
<html lang="ja">
<head>
<?php echo $this->fetch('script'); ?>
</head>
</html>

/app/View/Hoge/index.ctp

<?php $this->Html->script('hoge', array('inline'=>false)); ?> 

 

こうしてみた。

/app/View/Layouts/default.ctp

<!DOCTYPE html>
<html lang="ja">
<head>
<?php echo $this->fetch('minifyScript'); ?>
</head>
</html>

/app/View/Hoge/index.ctp

<?php
$this->assign('minifyScript', $this -> Minify ->script(array('hoge'))); ?>

これで解決。