Kinopyo Blog

プログラマとしてRuby, Rails, iPhone, iPad,Macなどなどと向き合う日々のログポース

Posts Tagged ‘kohana’

Kohana3にSmartyを利用するサンプル

2010年09月25日

Smarty templateの作成

application/viewsにhello.tplというファイルを作成します。

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>{$title}</title>
</head>
<body>
	hello, {$name}
</body>
</html>

Controllerの作成

hello.phpというControllerを作成します。

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Hello extends Controller {    

	public function action_index()
	{
		$view = View::factory("smarty:hello");
		$view->name = "kinopyo!";
		$view->title = "Smarty & Kohana Sample";
		$this->request->response = $view;
	}
}

View::factoryに”smarty:”というプリフィックスを書くことでSmartyテンプレートとして認識してくれます。

動作確認

http://127.0.0.1/myapp/helloにアクセスし、”hello, kinopyo!”が表示されれば成功です。

Controller_Templateの場合

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Hello extends Controller_Template {    

	public $template = 'smarty:hello';

	public function action_index()
	{
		$this->template->name = 'kinopyo!';
		$this->template->title = "Hello Title";
	}                             

}

Kohana3にSmarty moduleをインストール

ダウンロード

http://wiki.github.com/MrAnchovy/kohana-module-smarty/

インストール

githubでも手順がとても分かりやすいので直接引用させていただきます。

1. Download the latest version from the links above

2. Unpack the downloaded file

3. Move the smarty directory into the Kohana modules directory

4. Enable the module in your application’s bootstrap.php

Kohana::modules(array(

‘auth’ => MODPATH.‘auth’, // Basic authentication

// ‘cache’ => MODPATH.‘cache’, // Caching with multiple backends

// ‘codebench’ => MODPATH.‘codebench’, // Benchmarking tool

‘database’ => MODPATH.‘database’, // Database access

// ‘image’ => MODPATH.‘image’, // Image manipulation

‘orm’ => MODPATH.‘orm’, // Object Relationship Mapping

‘pagination’ => MODPATH.‘pagination’, // Paging of results

‘userguide’ => MODPATH.‘userguide’, // User guide and API documentation

‘smarty’ => MODPATH.’smarty’, // smarty template module.

));

5. Visit the page www.yoursite.com/smarty to confirm all is OK

http://wiki.github.com/MrAnchovy/kohana-module-smarty/

その他リンク

このモジュールのファイル構造: http://github.com/MrAnchovy/kohana-module-smarty/wiki/file-structure

Kohana3でmemcacheを利用する方法

2010年09月24日

前提条件

memcacheとphp_memcache extensionがインストールされたこと。

Mac XAMPP環境でmemcacheとPHP extensionのインストール | Kinopyo Blogを参考してください。

手順

1. application/bootstrap.phpを編集

Kohana::modulesのcacheのコメントを外します。

Kohana active cache in bootstrap.php

2. 下記の内容でconfig/cache.phpを作成

場所はapplication/configでもいいしsystem/config、modules/configでもいいです。

<?php defined('SYSPATH') or die('No direct script access.');
return array
(
	'default'  => array
	(
		'driver'             => 'memcache',
		'default_expire'     => 3600,
		// Use Zlib compression (can cause issues with integers)
		'compression'        => FALSE,
		'servers'            => array
		(
			array
			(
				// Memcache Server
				'host'             => '127.0.0.1',
				// Memcache port number
				'port'             => 11211,
				// Persistent connection
				'persistent'       => FALSE,
			),
		),
	),

);
3. 動作確認

まずはmemcacheを立ち上がってください。(ターミナルでmemcached -m 8 -l 127.0.0.1 -p 11211 -d )

そしてbootstrap.phpの最後にテスト用のコードを追記します。

$cache = Cache::instance();
$cache->set('hello','world');
die(var_dump($cache->get('hello')));

これで任意のページを開いて下記の内容が表示されれば成功ってことです。

string(5) “world”

テスト成功したらbootstrap.phpからテスト用のコードを削除してください。

Kohana 3.x :: URL Redirect

2010年09月14日

kohanaバージョン3.xで、リダイレクトするには

Request::instance()->redirect('/foo/bar/1');

kohana userguideモジュールを利用する

2010年09月13日

kohanaというphpフレームワークを触り始めました。MVCパターンなのでいろんなところで結構Railsと似ていますが、コード量が明らかにRailsより多くてまだ慣れてません。さすがにRailsやってから他の言語やフレームワークを触るとキツイです。

kohanaにはデフォルトでmodulesにuserguideが付いてます。ローカルでもガイドのドキュメントやAPIを参照できるため結構便利です。

userguideモジュールをアクティブする方法

bootstrap.phpの中に’userguide’を検索してコメントアウトします。

アクセスURL

例えばbase_urlが’myapp’の場合は:

http://localhost/myapp/userguide/docs

になります。

内容としては公式サイトのKohana | Kohana User Guideと同じになります。