CodeIgniter + Twig + Tank_auth

ci_logo プログラミング

ci_logo
ずっと構想を練っているアプリケーションをCodeIgniterで作ろうかなと思ってます

FuelPHPで行こうか、Cakephp2.1で行こうか迷いはあるのですが、ある程度安定したものが今は使いやすいのかなという後ろ向きな理由

ただ、それだけじゃつまらないので前々から興味があったTwigに手を出してみたいと思います

CodeIgniterの準備

インストール方法 : CodeIgniter ユーザガイド 日本語版
ほぼ置くだけでいいので楽ちんですね

URLからindex.phpは消したいのであとでhtaccessを書きます

tank_authの導入

これが定番のログイン認証系のライブラリーなのかな??
ログインはtwitter,facebookでの連携を考えていますがとりあえず入れておきます

Tank Auth authentication library for CodeIgniter

Download the latest version of the library.
Unzip the package.
Copy the application folder content to your CI application folder.
Copy the captcha folder to your CI folder. Make sure this folder is writable by web server.
Install database schema into your MySQL database.
Open the application/config/config.php file in your CI installation and change $config[‘sess_use_database’] value to TRUE.

これはこの通りにやれば動きました

Twigの導入

Twig-Codeigniter/application at master · bmatschullat/Twig-Codeigniter · GitHub
こういうのを見つけました
このままだと動かないので少し書き換えます

// set include path for twig
ini_set('include_path',
    'third_party/Twig/lib/Twig' . PATH_SEPARATOR . APPPATH . ini_get('include_path'));
require_once (string) 'third_party/Twig/lib/Twig/Autoloader.php';

Twigをthird_partyに突っ込みます

git submoduleで別リポジトリとして管理しておきます

git submodule add git://github.com/fabpot/Twig.git application/third_party/Twig

Twigを使ってみます

controllers/welcome.php

class Welcome extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->helper('url');
        $this->load->library('tank_auth');
        $this->load->library('twig');
    }

    function index()
    {
        if (!$this->tank_auth->is_logged_in()) {
            redirect('/auth/login/');
        } else {
            $data['user_id']    = $this->tank_auth->get_user_id();
            $data['username']   = $this->tank_auth->get_username();

            $this->twig->display('welcome.php', $data);
        }               
    }                   
}  

views/welcome.php

welcome!! <strong>{{ username }}</strong>
logout

ログイン後、ユーザー名が表示されれば成功です
参考サイトとちょっと違うことしたせいではまったはまった…

cakephpみたいにpluginとして、管理出来ると楽なんですがそこは仕方がないのかな〜

参考サイト: codeIgniter + twig + tank_auth – Sidestepism Blog

コメント

タイトルとURLをコピーしました