CodeIgniterの仕様とSmarty導入
先日まで暫しSlimというフレームワークを使っていましたが、
今後CodeIgniter2を使うことになります。
基本的な仕様で普通に詰まったのが2点(´・ω・`)
(1)ControllerとModelのクラス名はかぶっちゃいけない!
考えてみたら、まぁ当たり前なのかもしれないのですが、
class Hello extends CI_Controller {
で始まるコントローラーに
class Hello extends CI_Model {
で始めたモデルを読みこませようとしたら、画面真っ白けでした。
何でもいいので
class HelloModel extends CI_Model{
とか、
class Hellos extends CI_Model{
とかいうクラス名に変えとけばおkです。
(2)Controllerのfunction名がそのまんまURIに反映される。
例えば、こんなコントローラーだと、
class Hello extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('parser');
$this->load->model('Hellos');
}
public function index()
{
$result_array = $this->Hellos->findMember();
foreach ($result_array as $row)
{
echo $row['id'].'<br />';
}
}
public function smartyTest()
{
$data['title'] = "The Smarty parser works!";
$data['body'] = "Smarty!";
$this->parser->parse("hello.tpl", $data);
}
URIはそれぞれClassとfunctionに対応して、
http://www.~~~/hello/
$row['id]が書き出される
(function index()の中身)
http://www.~~~/hello/smartyTest/
hello.tplが呼び出される(このsmarty連携については下記)
(function smartyTest()の中身)
===========================
という感じの、基礎の基礎の仕様が分かってなかったので無駄足を踏みました(ヽ'ω`)
そして、CodeIgniter2とsmarty3の連携についてですが、
これは結論からいうと、素晴らしいライブラリ様があるので、
そちらを使わせてもらうと早いです。
Using Smarty 3 in Codeigniter 2 (a really tiny CI library)
http://ilikekillnerds.com/2010/11/using-smarty-3-in-codeigniter-2-a-really-tiny-ci-library/
1.こちらさんのgithubからzipでファイルをダウンロードし、既存のCodeIgniterのapplicationディレクトリに上書き。
2.readmeに書いてある通り、config/autoload.php に ライブラリ perser を読みこませれば、
Controller+view(tpl) の smartytest が動きます。
詳しく解説して下さってるブログは他にあるのでぐぐってくだしあ。