<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>I@laoer.com &#187; Kohana</title>
	<atom:link href="http://i.laoer.com/tag/kohana/feed" rel="self" type="application/rss+xml" />
	<link>http://i.laoer.com</link>
	<description>技术、生活、感悟 -- Laoer的博客</description>
	<lastBuildDate>Mon, 15 Feb 2010 08:36:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Kohana的Cache</title>
		<link>http://i.laoer.com/kohana-cache-memcached.html</link>
		<comments>http://i.laoer.com/kohana-cache-memcached.html#comments</comments>
		<pubDate>Wed, 15 Apr 2009 09:42:25 +0000</pubDate>
		<dc:creator>Laoer</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Kohana]]></category>
		<category><![CDATA[Memcached]]></category>

		<guid isPermaLink="false">http://i.laoer.com/?p=274</guid>
		<description><![CDATA[Kohana里有个Cache Library，我今天就测试了一下。
首先要配置，将system/config下的cache.php复制到application/config下，打开cache.php文件，我们看一下内容

1
2
3
4
5
6
7
$config&#91;'default'&#93; = array
&#40;
    'driver'   =&#62; 'file',
    'params'   =&#62; APPPATH.'cache',
    'lifetime' =&#62; 1800,
    'requests' =&#62; 1000
&#41;;

这是一个默认配置，&#8217;driver&#8217;为驱动的缓存方式，Kohana支持6种不同的驱动，分别是File、SQlite、Memcache、APC、Eaccelerator、Xcache，配置文件默认使用的file，其原理是，写缓存时把对象序列化写入文件，读缓存时从文件读出文本反序列化，所以在文件方式下，缓存是基于I/O的，在文件多而且大的时候，性能会有下降， &#8216;params&#8217;是驱动参数，在file驱动模式下，就是cache文件路径，&#8217;lifetime&#8217;是cache的生命周期，单位为秒，超过这个时间，内容将被清除（设置为0代表不自动清除），&#8217;requests&#8217;为在达到请求数量之前自动垃圾回收。
在应用中，可能需要不止一个缓存，所以可以配置多个缓存，增加$config数组即可，还有在file方式下可以为每个缓存设置单独文件路径，但前提是文件路径要存在，例如我们增加一个

1
2
3
4
5
6
7
$config&#91;'my'&#93; = array
&#40;
    'driver'   =&#62; 'file',
    'params'   =&#62; APPPATH.'cache/my',
    'lifetime' [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Kohana" href="http://kohanaphp.com/" target="_blank">Kohana</a>里有个Cache Library，我今天就测试了一下。</p>
<p>首先要配置，将system/config下的cache.php复制到application/config下，打开cache.php文件，我们看一下内容</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'default'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'driver'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'file'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'params'</span>   <span style="color: #339933;">=&gt;</span> APPPATH<span style="color: #339933;">.</span><span style="color: #0000ff;">'cache'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'lifetime'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1800</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'requests'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1000</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>这是一个默认配置，&#8217;driver&#8217;为驱动的缓存方式，Kohana支持6种不同的驱动，分别是File、SQlite、Memcache、APC、Eaccelerator、Xcache，配置文件默认使用的file，其原理是，写缓存时把对象序列化写入文件，读缓存时从文件读出文本反序列化，所以在文件方式下，缓存是基于I/O的，在文件多而且大的时候，性能会有下降， &#8216;params&#8217;是驱动参数，在file驱动模式下，就是cache文件路径，&#8217;lifetime&#8217;是cache的生命周期，单位为秒，超过这个时间，内容将被清除（设置为0代表不自动清除），&#8217;requests&#8217;为在达到请求数量之前自动垃圾回收。</p>
<p>在应用中，可能需要不止一个缓存，所以可以配置多个缓存，增加$config数组即可，还有在file方式下可以为每个缓存设置单独文件路径，但前提是文件路径要存在，例如我们增加一个</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'my'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'driver'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'file'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'params'</span>   <span style="color: #339933;">=&gt;</span> APPPATH<span style="color: #339933;">.</span><span style="color: #0000ff;">'cache/my'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'lifetime'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1800</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'requests'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1000</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>接下来我们在Controller里调用cache</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Mycache_Controller <span style="color: #000000; font-weight: bold;">extends</span> Controller <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$mychache</span> <span style="color: #339933;">=</span> Cache<span style="color: #339933;">::</span><span style="color: #004000;">instance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$mychache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;laoer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;OK&quot;</span><span style="color: #339933;">;</span>   
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> name<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$mychache</span><span style="color: #339933;">=</span> Cache<span style="color: #339933;">::</span><span style="color: #004000;">instance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mychache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>   
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Cache::instance(&#8220;my&#8221;)实例化配置里$config['my']的cache，如果用Cache::instance()就是实例化配置里$config['default']的cache，在浏览器里执行，已经可以存取了，在application/cache/my/文件下可以看到一个名为&#8221;name~~0&#8243;的文件，里面就是序列化的数据。</p>
<p>文件cache还是有一定的局限性，现在越来越的网站开始使用<a href="http://www.danga.com/memcached/" target="_blank">Memcached</a>所谓缓存的解决方案，Kohana的缓存驱动里，有Memcached的支持，但它的文档却没有给出Memcached的配置例子，看来要自己摸索一下。</p>
<p>将system/config下的cache_memcache.php复制到application/config下，cache_memcache.php的内容如下</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @package  Cache:Memcache
 *
 * memcache server configuration.
 */</span>
<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'servers'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span>
<span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span>
	<span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'host'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'port'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">11211</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'persistent'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Enable cache data compression.
 */</span>
<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'compression'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>根据你自己的情况修改Memcached的服务地址和端口，在application/config/cache.php里再加一段</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mem'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span>
<span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'driver'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'memcache'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'params'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'lifetime'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1800</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'requests'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1000</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>在Controller里把Cache::instance(&#8220;my&#8221;)改为Cache::instance(&#8220;mem&#8221;)，运行看看结果，已经可以从Memcahced里存取了。</p>
<p>Kohana的Memcached驱动还是有些缺陷，现在只能使用一组Memcached，即$config['servers']这个参数，我觉得Memcacahed组也应该是多个，因为从业务角度会根据功能对cache做划分，我大概看了一下system/libraries/drivers/Cache/Memcache.php文件，应该是可以改造的，还有一点，编译PHP的时候要安装Memcached的支持。</p>
]]></content:encoded>
			<wfw:commentRss>http://i.laoer.com/kohana-cache-memcached.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Kohana的Events和Hooks研究</title>
		<link>http://i.laoer.com/kohana-events-and-hooks.html</link>
		<comments>http://i.laoer.com/kohana-events-and-hooks.html#comments</comments>
		<pubDate>Wed, 25 Feb 2009 07:55:30 +0000</pubDate>
		<dc:creator>Laoer</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Kohana]]></category>

		<guid isPermaLink="false">http://i.laoer.com/?p=109</guid>
		<description><![CDATA[Kohana是一个使用PHP5的面向对象的MVC框架，是从CodeIgniter衍生出来，研究了几天，感觉还不错，就是它的文档实在太简陋了，在看到它的Events机制和Hooks机制时，确实有点不明白，文档上说的实在太简单了，后来我在Google上搜索了一下，找到两篇文章《Events and hooks in Kohana》和《Practical Kohana Hooks example: PHPIDS》，上面讲了一下Kohana的Events和Hooks，并举了例子，我也开始慢慢理解了它的含义。
在Kohana的Events中，默认定义了很多的Events（例如system.ready、system.pre_controller等等），中文理解就是事件，也就是触发点，在程序在运行到某个位置时，会被触发，具体就是调用Event::run方法，我们在Kohana.php里可以看到在不同的位置执行了不同的Event::run方法，那么Hooks的意义在于当一个事件被触发之前，可以通过已加载的Hooks来修改事件的回调，有点绕口，说白了就是在事件发生之前，做点事情，举个例子，你安排了今天的日程，下午3点要开会，那么在3点开会就是一个Event（事件），那么到3点的时刻，你希望提醒你一下，你在你的手机里定了一个闹钟，在3点的时候会响，这就是一个Hook，可以看出Hook是基于Event的。
我们可以创建自己的Hook程序，并加载如默认的Events里，但是默认的Events不一定能满足我们的需要，比如我们在Web应用中经常使用的权限校验，判断这个用户是否登陆过，我们虽然可以使用默认Events里的system.pre_controller，但是这个Event是针对所有的Controller的，有些Controller是不需要校验用户的，还好Kohana允许自己定义Event，下面我们还是举例说明吧。
要使用Hooks，首先要在application/config/config.php里将$config['enable_hooks']置为TRUE。
之后我们定义自己的Event，我们在application/controllers目录下建立base.php，代码如下：

1
2
3
4
5
6
7
class Base_Controller extends Controller  &#123;
&#160;
	public function __construct&#40;&#41; &#123;
		parent::__construct&#40;&#41;;
		Event::run&#40;&#34;base.construct&#34;&#41;;
	&#125;	
&#125;

我们继承了Kohana的Controller，在构造函数里定义了Event，名字叫“base.construct”，以后我们的Controller都继承自Base_Controller，那么在对象创建的时候都会触发base.construct事件。
Hook的文件放在application/hooks下，我们就创建一个hook文件sessioncheck.php，代码如下：

1
2
3
4
5
6
7
8
9
class SessionCheck &#123;
&#160;
	public function check&#40;&#41; &#123;
		echo &#34;check session ...&#34;;
	&#125;
&#160;
&#125;
&#160;
Event::add&#40;'base.construct', array&#40;'SessionCheck','check'&#41;&#41;;

我们将SessionCheck的check方法加载到了base.construct事件上，也就是在触发base.construct事件之前，会执行SessionCheck的check方法。
接下来我们写一个Controller，在application/controllers目录下建立first.php，代码如下：

1
2
3
4
5
6
7
class First_Controller extends Base_Controller  &#123;
&#160;
	public function index&#40;&#41; &#123;
		echo &#34;First - index&#34;;
		exit&#40;&#41;;
	&#125;
&#125;

我们执行一下看看会有什么提示，http://localhost/kohana/first，显示
check session &#8230;First &#8211; index
没问题了，在Controller构造时，执行了Hook里的方法。
]]></description>
			<content:encoded><![CDATA[<p><a href="http://kohanaphp.com/" target="_blank">Kohana</a>是一个使用PHP5的面向对象的MVC框架，是从<a href="http://codeigniter.com/" target="_blank">CodeIgniter</a>衍生出来，研究了几天，感觉还不错，就是它的文档实在太简陋了，在看到它的Events机制和Hooks机制时，确实有点不明白，文档上说的实在太简单了，后来我在Google上搜索了一下，找到两篇文章<a href="http://learn.kohanaphp.com/2008/06/13/events-and-hooks-in-kohana/" target="_blank">《Events and hooks in Kohana》</a>和<a href="http://www.ninjapenguin.co.uk/blog/2008/06/29/practical-kohana-hooks-example-phpids/" target="_blank">《Practical Kohana Hooks example: PHPIDS》</a>，上面讲了一下Kohana的Events和Hooks，并举了例子，我也开始慢慢理解了它的含义。</p>
<p>在Kohana的<a href="http://docs.kohanaphp.com/general/events" target="_blank">Events</a>中，默认定义了很多的Events（例如system.ready、system.pre_controller等等），中文理解就是事件，也就是触发点，在程序在运行到某个位置时，会被触发，具体就是调用Event::run方法，我们在Kohana.php里可以看到在不同的位置执行了不同的Event::run方法，那么Hooks的意义在于当一个事件被触发之前，可以通过已加载的Hooks来修改事件的回调，有点绕口，说白了就是在事件发生之前，做点事情，举个例子，你安排了今天的日程，下午3点要开会，那么在3点开会就是一个Event（事件），那么到3点的时刻，你希望提醒你一下，你在你的手机里定了一个闹钟，在3点的时候会响，这就是一个Hook，可以看出Hook是基于Event的。</p>
<p>我们可以创建自己的Hook程序，并加载如默认的Events里，但是默认的Events不一定能满足我们的需要，比如我们在Web应用中经常使用的权限校验，判断这个用户是否登陆过，我们虽然可以使用默认Events里的system.pre_controller，但是这个Event是针对所有的Controller的，有些Controller是不需要校验用户的，还好Kohana允许自己定义Event，下面我们还是举例说明吧。</p>
<p>要使用Hooks，首先要在application/config/config.php里将$config['enable_hooks']置为TRUE。</p>
<p>之后我们定义自己的Event，我们在application/controllers目录下建立base.php，代码如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Base_Controller <span style="color: #000000; font-weight: bold;">extends</span> Controller  <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Event<span style="color: #339933;">::</span><span style="color: #004000;">run</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;base.construct&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>我们继承了Kohana的Controller，在构造函数里定义了Event，名字叫“base.construct”，以后我们的Controller都继承自Base_Controller，那么在对象创建的时候都会触发base.construct事件。</p>
<p>Hook的文件放在application/hooks下，我们就创建一个hook文件sessioncheck.php，代码如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> SessionCheck <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> check<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;check session ...&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
Event<span style="color: #339933;">::</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'base.construct'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SessionCheck'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'check'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>我们将SessionCheck的check方法加载到了base.construct事件上，也就是在触发base.construct事件之前，会执行SessionCheck的check方法。</p>
<p>接下来我们写一个Controller，在application/controllers目录下建立first.php，代码如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> First_Controller <span style="color: #000000; font-weight: bold;">extends</span> Base_Controller  <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;First - index&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>我们执行一下看看会有什么提示，http://localhost/kohana/first，显示</p>
<p>check session &#8230;First &#8211; index</p>
<p>没问题了，在Controller构造时，执行了Hook里的方法。</p>
]]></content:encoded>
			<wfw:commentRss>http://i.laoer.com/kohana-events-and-hooks.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP框架选择</title>
		<link>http://i.laoer.com/php-framework.html</link>
		<comments>http://i.laoer.com/php-framework.html#comments</comments>
		<pubDate>Mon, 16 Feb 2009 06:49:07 +0000</pubDate>
		<dc:creator>Laoer</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[Kohana]]></category>

		<guid isPermaLink="false">http://i.laoer.com/?p=70</guid>
		<description><![CDATA[离最初用PHP编程序已经有8、9年时间了，后来这6、7年的时间一直研究Java，对PHP有些生疏了，但PHP的生命力却依旧顽强，对于面向Web开发时Java的繁琐，我最近又将注意力集中到了PHP上，但已经习惯了Struts这样的MVC框架，我也要寻找一个适合的PHP MVC框架，选择的标准有几个：1、性能；2、易用性；3、文档；4、长期支持度
我最开始看了Zend Framework，Zend的东西，毕竟带有官方特性，他的framework应该是代表着主流，看了之后，Zend Framework可以说是纷繁复杂，但是面面俱到，Web应用方面的问题基本都可以解决，我唯一担心的就是性能，虽没有做过测试，但也确实担心。
后来有一天在JavaEye上逛，看到一篇帖子《PHP框架的繁荣是正确的发展方向吗？》，讨论了PHP的运行机制、与ROR的比较、性能等等，非常热闹，同时也列举出了一些PHP的框架，特别是一些性能比较，让我很吃惊，CakePHP、Symfony可以不用考虑了。
接下来我看了看CodeIgniter，感觉不错，简单，相比Zend Framework要简单得多，大多数问题也都能解决，性能在一些资料描述中也表现的尚可（比Zend Framework要快几倍），而且其文档比较细，学习起来不难，后来又发现了Kohana，Kohana是从CodeIgniter衍生出来，由于CodeIgniter是兼容PHP4和5的，而Kohana只支持PHP5，是完全的OO方式，其文档并还没有仔细研究，看到了一个比较的文章《Notes on Choosing a PHP Framework: A Quick Comparison of CodeIgniter and Kohana》，看上去Kohana有些特性还是很优秀的，但不知道Kohana社区对于这个开源产品的支持有多好。
后来又看到文章《Performance of Yii》，发现Yii这个框架的性能更强劲啊，比CodeIgniter还要好几倍，不可思议，看了看Yii的文档，它也是完全OO的，要PHP5以上，核心应该也比较简单，能保持比较好的性能，但我觉得它的Guide文档比较粗，学习起来似乎要费点功夫，其性能应该是我最感兴趣的地方。
再说说国内的PHP框架，在JavaEye的文章里，QeePHP的作者也在推荐自己的框架，简单测试下比Yii还要快，好NB啊，但从社区反应出来其文档不够详细，其代码我也没有细看，似乎和Yii有很多相近的地方，另一个国内的PHP框架ThinkPHP文档比较详尽，但没有测试报告，不知道性能如何，而且在PHPChina的社区里和QeePHP有激烈的争论，挺有意思的。
看了一大圈，我也没有决定采用何种PHP的框架，他们各有长处，也各有缺陷，但综合考虑，我还是应该会在CodeIgniter、Kohana和Yii中选择最终的方案。
]]></description>
			<content:encoded><![CDATA[<p>离最初用PHP编程序已经有8、9年时间了，后来这6、7年的时间一直研究Java，对PHP有些生疏了，但PHP的生命力却依旧顽强，对于面向Web开发时Java的繁琐，我最近又将注意力集中到了PHP上，但已经习惯了Struts这样的MVC框架，我也要寻找一个适合的PHP MVC框架，选择的标准有几个：1、性能；2、易用性；3、文档；4、长期支持度</p>
<p>我最开始看了<a href="http://framework.zend.com/" target="_blank">Zend Framework</a>，Zend的东西，毕竟带有官方特性，他的framework应该是代表着主流，看了之后，Zend Framework可以说是纷繁复杂，但是面面俱到，Web应用方面的问题基本都可以解决，我唯一担心的就是性能，虽没有做过测试，但也确实担心。</p>
<p>后来有一天在JavaEye上逛，看到一篇帖子<a href="http://www.javaeye.com/topic/319039" target="_blank">《PHP框架的繁荣是正确的发展方向吗？》</a>，讨论了PHP的运行机制、与ROR的比较、性能等等，非常热闹，同时也列举出了一些PHP的框架，特别是一些性能比较，让我很吃惊，CakePHP、Symfony可以不用考虑了。</p>
<p>接下来我看了看<a href="http://codeigniter.com/" target="_blank">CodeIgniter</a>，感觉不错，简单，相比Zend Framework要简单得多，大多数问题也都能解决，性能在一些资料描述中也表现的尚可（比Zend Framework要快几倍），而且其文档比较细，学习起来不难，后来又发现了<a href="http://kohanaphp.com/" target="_blank">Kohana</a>，Kohana是从<a href="http://codeigniter.com/" target="_blank">CodeIgniter</a>衍生出来，由于<a href="http://codeigniter.com/" target="_blank">CodeIgniter</a>是兼容PHP4和5的，而Kohana只支持PHP5，是完全的OO方式，其文档并还没有仔细研究，看到了一个比较的文章<a href="http://www.beyondcoding.com/2008/02/23/notes-on-choosing-a-php-framework-a-quick-comparison-of-codeigniter-and-kohana/" target="_blank">《Notes on Choosing a PHP Framework: A Quick Comparison of CodeIgniter and Kohana》</a>，看上去Kohana有些特性还是很优秀的，但不知道Kohana社区对于这个开源产品的支持有多好。</p>
<p>后来又看到文章<a href="http://www.yiiframework.com/performance/" target="_blank">《Performance of Yii》</a>，发现Yii这个框架的性能更强劲啊，比CodeIgniter还要好几倍，不可思议，看了看Yii的文档，它也是完全OO的，要PHP5以上，核心应该也比较简单，能保持比较好的性能，但我觉得它的Guide文档比较粗，学习起来似乎要费点功夫，其性能应该是我最感兴趣的地方。</p>
<p>再说说国内的PHP框架，在JavaEye的文章里，<a href="http://qeephp.com/" target="_blank">QeePHP</a>的作者也在推荐自己的框架，简单测试下比Yii还要快，好NB啊，但从社区反应出来其文档不够详细，其代码我也没有细看，似乎和Yii有很多相近的地方，另一个国内的PHP框架<a href="http://thinkphp.cn/" target="_blank">ThinkPHP</a>文档比较详尽，但没有测试报告，不知道性能如何，而且在PHPChina的社区里和QeePHP有激烈的<a href="http://bbs.phpchina.com/viewthread.php?tid=100306" target="_blank">争论</a>，挺有意思的。</p>
<p>看了一大圈，我也没有决定采用何种PHP的框架，他们各有长处，也各有缺陷，但综合考虑，我还是应该会在CodeIgniter、Kohana和Yii中选择最终的方案。</p>
]]></content:encoded>
			<wfw:commentRss>http://i.laoer.com/php-framework.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
