CakePHPのお勉強 8回目 Smartyの利用
お問い合わせサンプルを作りながら簡単にCakePHPの利用についてお勉強したので、少し別なお勉強。
今回はCakePHPとSmartyを連携させて利用する。
日本語公式を見たりしたがあまりいい情報がなかったので、一番参考になった「CakePHP2.xとSmartyを連携する方法。- CakePHP2.xで入門!簡単Webアプリ作成。番外編-」を参考に進める。
(1) Smartyライブラリの取得
Smarty公式サイトから最新版のライブラリを取得。
2013年11月27日現在 3.1.15 が最新版のもよう
(2) Smartライブラリの設置
取得したライブラリを展開しその中の「libs」フォルダを「smarty」にリネーム。
リネームした「smarty」フォルダを「app」フォルダ内の「Vendor」フォルダにアップロード。
(3) View関連ファイルの取得と設置
CakePHP2系用の「Smarty View class and helpers plugin」をコチラから取得。
「cakephp-smartyview-master」をダウンロードし展開すると「View」フォルダがあるので、それを「app」フォルダ内の「View」フォルダにアップロード。
(4) 動作確認
実際に簡単なテンプレートを利用して動作の確認。
利用するテンプレートファイル。
<h1>{$sampleValue}</h1> {$this->Html->link('Yahoo', 'http://www.yahoo.co.jp/')}
コントローラ。
<?php class SamplesController extends AppController { public $viewClass = 'Smarty'; public function index() { $this->set('sampleValue', 'さんぷるの値'); } }
で、「http://example.com/Samples/」にアクセスすると。
Smartyのタグもヘルパーもちゃんと動作している状態。
番外編 - その1
いちいちViewクラスを指定するのが面倒な場合は「AppController」側で対応。
具体的には「app」フォルダ内の「Controller」フォルダにある「AppController.php」を修正。
<?php /** * Application level Controller * * This file is application-wide controller file. You can put all * application-wide controller-related methods here. * * PHP 5 * * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice. * * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://cakephp.org CakePHP(tm) Project * @package app.Controller * @since CakePHP(tm) v 0.2.9 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ App::uses('Controller', 'Controller'); /** * Application Controller * * Add your application-wide methods in the class below, your controllers * will inherit them. * * @package app.Controller * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller */ class AppController extends Controller { public $viewClass = 'Smarty'; }
36行目の部分を追加。
番外編 - その2
Smartyを使った場合の各種設定は「SmartyView.php」にて設定済み。 例えば、Smartyのコンパイル済みのファイルは「app」フォルダ内の「tmp」フォルダの中に「smarty」フォルダが作成され、その中に「compile」フォルダが作成され管理される。
具体的な設定は以下の部分。
/** * SmartyView constructor * * @param $controller instance of calling controller */ function __construct (&$controller) { parent::__construct($controller); $this->Smarty = new Smarty(); $this->_smartyVersion(); $this->ext= '.tpl'; $this->Smarty->compile_dir = TMP.'smarty'.DS.'compile'.DS; $this->Smarty->cache_dir = TMP.'smarty'.DS.'cache'.DS; $this->Smarty->error_reporting = 'E_ALL & ~E_NOTICE'; $this->Smarty->debugging = true; $this->Smarty->caching = 0; switch ($this->smartyMajorVersion) { case 2: $this->Smarty->clear_compiled_tpl(); $this->Smarty->plugins_dir[] = APP . 'View' . DS.'smarty_plugins'.DS; break; case 3: $this->Smarty->clearCompiledTemplate(); $this->Smarty->setPluginsDir(array(APP . 'View' . DS.'smarty_plugins'.DS)); break; } // Loading base class of Smarty Helpers App::uses('SmartyBaseHelper', $this->pluginName.'.'.'View/Helper'); }
必要に応じて修正を行う。