<?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-&#62;php(); &#187; CakePHP</title>
	<atom:link href="http://i-php.net/tag/cakephp/feed/" rel="self" type="application/rss+xml" />
	<link>http://i-php.net</link>
	<description>Hãy tiết kiệm điện và sử dụng opensource để bảo vệ môi trường</description>
	<lastBuildDate>Fri, 26 Feb 2010 07:29:32 +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>Sử dụng plugin chat trong cakephp 1.2</title>
		<link>http://i-php.net/2009/10/s%e1%bb%ad-d%e1%bb%a5ng-plugin-chat-trong-cakephp-12/</link>
		<comments>http://i-php.net/2009/10/s%e1%bb%ad-d%e1%bb%a5ng-plugin-chat-trong-cakephp-12/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 14:07:35 +0000</pubDate>
		<dc:creator>hoanbn</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[chat]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://i-php.net/?p=545</guid>
		<description><![CDATA[Xin chào các bạn!
Đây là môt plugin do tôi tự viết dựa trên các tính năng chat của livechat. Chương trình livechat tôi tham khảo được viết bằng php, để có thể quản trị được các chức năng tôi thấy có nhiều cái thừa  không cần thiết trong trường hợp của tôi. Tôi đã dựa [...]]]></description>
			<content:encoded><![CDATA[<p>Xin chào các bạn!</p>
<p>Đây là môt plugin do tôi tự viết dựa trên các tính năng chat của livechat. Chương trình livechat tôi tham khảo được viết bằng php, để có thể quản trị được các chức năng tôi thấy có nhiều cái thừa  không cần thiết trong trường hợp của tôi. Tôi đã dựa vào livechat để viết plugin với tính năng tương tự. Plugin này có sử dụng jquery.</p>
<p><strong>Các chức năng chính:</strong></p>
<p>1)  Hiển thị danh sách người chat và danh sách người quản trị đang chat. Tôi tạm gọi là guests và operators.</p>
<p>2) Guest chỉ việc nhập tên và email, sau đó sẽ hiển thị màn hình chat. Operators sẽ thấy trạng thái chát của guest và bắt đầu chat. Một guest có thể do nhiều operators chat.</p>
<p>3) Operators sử dụng luôn bảng users.</p>
<p>3) Các chức năng hoặc động trong cửa sổ window riêng biệt (popup window).</p>
<p>Để sử dụng các bạn <a href="http://i-php.net/chat.zip">download plugin tại đây</a>. (Để hểu rõ hơn về plugin bạn có thể tham khảo tại <a href="http://i-php.net/2009/08/s%E1%BB%AD-d%E1%BB%A5ng-plugin-trong-cakephp-12/">đây</a>)</p>
<p><strong>Cài đặt:</strong></p>
<p>1) Đưa toàn bộ nội dung của plugin chat vào trong app/plugin</p>
<p>2) Bạn chạy file sqlChat trong thu mục app/plugins/chat/config/sql .</p>
<p>3) Copy file chat.ctp trong thu muc app/plugins/chat/views/layout vào trong thu mục app/views/layout.</p>
<p><strong>Sử dụng:</strong></p>
<p>Do các chức năng chat đểu hiển thị ra các cửa sổ riêng biệt. Do vậy ta chỉ việc tạo ra link gọi đến là ok.</p>
<p>Ví dụ:</p>
<pre class="php">&lt;?php
//Hiển thị link để guest mở cửa sổ chat với admin
echo $html-&gt;link('Chat with Amdmin', '/chat/chats/form_guest', array('onclick' =&gt; "window.open('{$html-&gt;url('/chat/chats/form_guest')}','mywin','left=20,top=20,width=460,height=440,toolbar=0,resizable=0'); return false;"));
echo '&lt;br /&gt;';
//Hiển thị link quản trị chat
echo $html-&gt;link('Manager chatting', '/chat/chats/chat_users', array('onclick' =&gt; "window.open('{$html-&gt;url('/chat/chats/chat_users')}','mywin','left=20,top=20,width=460,height=440,toolbar=0,resizable=0'); return false;"));
?&gt;</pre>
<p>Vậy là xong, chúc các bạn vui vẻ <img src='http://i-php.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://i-php.net/2009/10/s%e1%bb%ad-d%e1%bb%a5ng-plugin-chat-trong-cakephp-12/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Phân trang tìm kiếm đầy đủ với cakephp 1.2</title>
		<link>http://i-php.net/2009/07/phan-trang-tim-ki%e1%ba%bfm-d%e1%ba%a7y-d%e1%bb%a7-v%e1%bb%9bi-cakephp-12/</link>
		<comments>http://i-php.net/2009/07/phan-trang-tim-ki%e1%ba%bfm-d%e1%ba%a7y-d%e1%bb%a7-v%e1%bb%9bi-cakephp-12/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 01:12:12 +0000</pubDate>
		<dc:creator>hoanbn</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[web server]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[paginator]]></category>

		<guid isPermaLink="false">http://i-php.net/?p=465</guid>
		<description><![CDATA[Chào cả nhà!
Thật không nhớ nổi đây là lần thứ mấy tôi viết bài với tiêu đề phân trang tìm kiếm cho cakephp nữa. Vậy tại sao lại còn có bài viết này nữa. Việc phân trang tìm kiếm với cakephp đã không biết bao nhiêu lần làm tôi đâu đầu rồi. Sau nhiều lần [...]]]></description>
			<content:encoded><![CDATA[<p>Chào cả nhà!</p>
<p>Thật không nhớ nổi đây là lần thứ mấy tôi viết bài với tiêu đề phân trang tìm kiếm cho cakephp nữa. Vậy tại sao lại còn có bài viết này nữa. Việc phân trang tìm kiếm với cakephp đã không biết bao nhiêu lần làm tôi đâu đầu rồi. Sau nhiều lần tìm kiếm các giải pháp, nay tôi cũng đa tìm ra một giải pháp ứng ý trong việc tìm kiếm phân trang với cakephp. Sau đây tôi sẽ chia sẽ với mọi người giải pháp ấy, được minh họa thông qua ví dụ tìm kiếm sản phẩm dưới đây.</p>
<p>Việc đâu tiên là tôi phải tạo csdl. Trong ví dụ này tôi tạo csdl có tên là phantrang:</p>
<p>CREATE DATABASE phantrang;</p>
<p>CREATE TABLE IF NOT EXISTS `products` (<br />
`id` int(4) NOT NULL AUTO_INCREMENT,<br />
`name` varchar(255) DEFAULT NULL,<br />
`model` varchar(255) NOT NULL,<br />
`price` int(16) DEFAULT NULL,<br />
`created` datetime DEFAULT NULL,<br />
`type` enum(&#8216;Loai 1&#8242;,&#8217;Loai 2&#8242;,&#8217;Loai 3&#8242;) DEFAULT NULL,<br />
`description` text NOT NULL,<br />
PRIMARY KEY (`id`)<br />
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;</p>
<p>Vậy là tôi đã tạo xong CSDL, còn việc config để kết nối vào CSDL vừa tạo thì mọi người tự làm nhé. Dữ liệu trong đây thì mọi người tự thêm vào nhé.(^_^).</p>
<p>Công việc tiếp theo là tôi sẽ tạo ra một /app/controllers/products_controllers.php như sau:</p>
<blockquote><p>/*  app/controllers/products_controller.php */</p>
<p>&lt;?php<br />
class ProductsController extends AppController {</p>
<p>var $name = &#8216;Products&#8217;;<br />
var $scaffold;</p>
<p>}<br />
?&gt;</p></blockquote>
<p>Với các chức năng như add, edit, delete trong ví dụ này tôi sẽ dùng chức năng scaffold của cakephp, và quả thật những trường hợp như thế này thì scaffold quá tuyệt vời. Trong bài viết này tôi sẽ chỉ chú trọng vào chức năng tìm kiếm mà thôi. Tiếp đến tôi sẽ tạo một function search trong products_controller như sau:</p>
<blockquote><p>/*  app/controllers/products_controller.php */</p>
<p>&lt;?php<br />
class ProductsController extends AppController {</p>
<p>var $name = &#8216;Products&#8217;;<br />
var $scaffold;</p>
<p><strong> function search() {</strong></p>
<p><strong> }</strong></p>
<p>}<br />
?&gt;</p></blockquote>
<p>Cả nhà chú ý nhé, khi tôi thêm dữ liệu vào thì tôi sẽ copy lại nguyên nội dung của cả trang để mọi người dễ hiểu hơn, chỉ phần nào thêm mới tôi sẽ in đậm lên thôi. Bây giờ tôi sẽ tạo một trang view search như sau app/views/products/search.ctp</p>
<blockquote><p>//  app/view/products/search.ctp</p>
<p>&lt;?php<br />
echo $form-&gt;create(&#8216;Search&#8217;, array(&#8216;url&#8217; =&gt; &#8216;/products/dataSearch&#8217;));<br />
echo $form-&gt;input(&#8216;name&#8217;);<br />
echo $form-&gt;input(&#8216;price&#8217;);<br />
echo $form-&gt;input(&#8216;model&#8217;);<br />
echo $form-&gt;input(&#8216;type&#8217;, array(&#8216;options&#8217; =&gt; array(&#8221; =&gt; &#8221;,&#8217;Loai 1&#8242; =&gt; &#8216;Loại 1&#8242;, &#8216;Loai 2&#8242; =&gt; &#8216;Loại 2&#8242;, &#8216;Loai 3&#8242; =&gt; &#8216;Loai 3&#8242;)));<br />
echo $form-&gt;input(&#8216;description&#8217;);<br />
echo $form-&gt;end(&#8216;Search&#8217;);</p>
<p>?&gt;</p></blockquote>
<p>Phần form search vừa rồi tôi đã đề &#8216;url&#8217; trỏ đến action &#8216;dataSearch&#8217;. Action &#8216;dataSearch&#8217; này trong đây sẽ thực hiện công việc xử lý dữ liệu trong form tìm kiếm và chuyển lên url. Tôi thêm function <strong>dataSearch</strong> trong <strong>products_controller</strong> như sau:</p>
<blockquote><p>/*  app/controllers/products_controller.php */</p>
<p>&lt;?php<br />
class ProductsController extends AppController {</p>
<p>var $name = &#8216;Products&#8217;;<br />
var $scaffold;</p>
<p>function search()<br />
{<br />
}</p>
<p><strong> function dataSearch()<br />
{<br />
// the page we will redirect to<br />
$url['controller'] = &#8216;products&#8217;;<br />
$url['action'] = &#8217;search&#8217;;</strong></p>
<p><strong>// build a URL will all the search elements in it<br />
// the resulting URL will be<br />
// example.com/cake/posts/index/Search.keywords:mykeyword/Search.tag_id:3<br />
foreach ($this-&gt;data as $k=&gt;$v){<br />
foreach ($v as $kk=&gt;$vv){<br />
$url[$k.'.'.$kk]=$vv;<br />
}<br />
}</strong></p>
<p><strong>// redirect the user to the url<br />
$this-&gt;redirect($url, null, true);<br />
}</strong><br />
}<br />
?&gt;</p></blockquote>
<p>Hàm dataSearch sẽ thực hiện chức năng chuyển tòan bộ dữ liệu vừa được gửi (submit) vào trong một mảng và sau đó thông qua hàm &#8216;redirect&#8217; chuyến sang một trang mới. Việc chuyển đến trang nào sẽ được quyết định thông qua 2 biến &#8220;<strong>$url['controller']&#8221; </strong>và &#8220;<strong>$url['action']&#8221; </strong>. Nếu bạn nào chưa hiểu hàm này thực hiện những công việc gì thì các bạn cũng không cần tìm hiểu đâu, vì hàm này sẽ không bào giờ thay đổi cả, chỉ cần thay đổi giá trị 2 biến đó thôi. Cả nhà có thể tạm hiểu là hàm này thực hiện việc chuyển tất cả dữ liệu được submit lên trên url.</p>
<p>Vậy là sau khi hàm dataSearch xử lý xong, sẽ chuyển dữ liệu về action &#8220;search&#8221; trong products_controller. Lúc này sẽ tồn tại một biến mảng &#8220;$this-&gt;passedArgs&#8221; chứa dữ liệu của trang search khi submit. Hay nói đúng hơn biến &#8220;$this-&gt;passedArgs&#8221; chứa các dữ liệu được truyền ở phía trên url và đưa vào thành các mảng tương ứng. Ta sẽ xử lý dữ liệu truyền vào để tạo điều kiện lọc như sau:</p>
<blockquote><p>/*  app/controllers/products_controller.php */</p>
<p>&lt;?php<br />
class ProductsController extends AppController {</p>
<p>var $name = &#8216;Products&#8217;;<br />
var $scaffold;</p>
<p>function search()<br />
{<br />
<strong> $conditions = array();<br />
if (!empty($this-&gt;passedArgs))<br />
{<br />
if (isset($this-&gt;passedArgs['Search.name']))<br />
{<br />
$conditions[]["Product.name LIKE"] = &#8220;%{$this-&gt;passedArgs['Search.name']}%&#8221;;<br />
$this-&gt;data['Search']['name'] = $this-&gt;passedArgs['Search.name'];<br />
}</strong></p>
<p><strong>if (isset($this-&gt;passedArgs['Search.price']))<br />
{<br />
$conditions[]["Product.price "] = $this-&gt;passedArgs['Search.price'];<br />
$this-&gt;data['Search']['price'] = $this-&gt;passedArgs['Search.price'];<br />
}</strong></p>
<p><strong>if (isset($this-&gt;passedArgs['Search.model']))<br />
{<br />
$conditions[]["Product.model"] = $this-&gt;passedArgs['Search.model'];<br />
$this-&gt;data['Search']['model'] = $this-&gt;passedArgs['Search.model'];<br />
}</strong></p>
<p><strong>if (isset($this-&gt;passedArgs['Search.type']))<br />
{<br />
$conditions[]["Product.type"] = $this-&gt;passedArgs['Search.type'];<br />
$this-&gt;data['Search']['type'] = $this-&gt;passedArgs['Search.type'];<br />
}<br />
}</strong></p>
<p><strong>$this-&gt;paginate = array(&#8216;limit&#8217; =&gt; &#8216;1&#8242;, &#8216;order&#8217; =&gt; &#8216;Product.created DESC&#8217;);<br />
$this-&gt;set(&#8216;products&#8217;, $this-&gt;paginate(&#8216;Product&#8217;, $conditions));</strong><br />
}</p>
<p>function dataSearch()<br />
{<br />
// the page we will redirect to<br />
$url['controller'] = &#8216;products&#8217;;<br />
$url['action'] = &#8217;search&#8217;;</p>
<p>// build a URL will all the search elements in it<br />
// the resulting URL will be<br />
// example.com/cake/posts/index/Search.keywords:mykeyword/Search.tag_id:3<br />
foreach ($this-&gt;data as $k=&gt;$v){<br />
foreach ($v as $kk=&gt;$vv){<br />
$url[$k.'.'.$kk]=$vv;<br />
}<br />
}</p>
<p>// redirect the user to the url<br />
$this-&gt;redirect($url, null, true);<br />
}<br />
}<br />
?&gt;</p></blockquote>
<p>Trong phần controller này ta sẽ thưc hiện phân trang như bình thường trong cakephp, chỉ khác là sẽ có thêm điều kiện tìm kiếm như ở phía trên thôi.</p>
<p>Việc tiếp theo sẽ là hiển thị kết quả tìm kiếm ra trang search. Ta sẽ thêm vào view search như sau:</p>
<blockquote><p>/*  app/views/products/search.ctp */</p>
<p>&lt;?php<br />
echo $form-&gt;create(&#8216;Search&#8217;, array(&#8216;url&#8217; =&gt; &#8216;/products/dataSearch&#8217;));<br />
echo $form-&gt;input(&#8216;name&#8217;);<br />
echo $form-&gt;input(&#8216;price&#8217;);<br />
echo $form-&gt;input(&#8216;model&#8217;);<br />
echo $form-&gt;input(&#8216;type&#8217;, array(&#8216;options&#8217; =&gt; array(&#8221; =&gt; &#8221;,&#8217;Loai 1&#8242; =&gt; &#8216;Loại 1&#8242;, &#8216;Loai 2&#8242; =&gt; &#8216;Loại 2&#8242;, &#8216;Loai 3&#8242; =&gt; &#8216;Loai 3&#8242;)));<br />
echo $form-&gt;input(&#8216;description&#8217;);<br />
echo $form-&gt;end(&#8216;Search&#8217;);</p>
<p>?&gt;</p>
<p><strong>&lt;?php<br />
echo $paginator-&gt;counter(array(<br />
&#8216;format&#8217; =&gt; &#8216;Page %page% of %pages%, showing %current% records out of<br />
%count% total, starting on record %start%, ending on %end%&#8217;<br />
));<br />
?&gt;<br />
&lt;table cellpadding=&#8221;1&#8243; cellspacing=&#8221;0&#8243;&gt;<br />
&lt;tr&gt;<br />
&lt;th&gt;ID&lt;/th&gt;<br />
&lt;th&gt;Name&lt;/th&gt;<br />
&lt;th&gt;Model&lt;/th&gt;<br />
&lt;th&gt;Price&lt;/th&gt;<br />
&lt;th&gt;Created&lt;/th&gt;<br />
&lt;th&gt;Type&lt;/th&gt;<br />
&lt;th&gt;Description&lt;/th&gt;<br />
&lt;/tr&gt;<br />
&lt;?php<br />
foreach ($products as $product)<br />
{<br />
?&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;&lt;?=$product['Product']['id']?&gt;&lt;/td&gt;<br />
&lt;td&gt;&lt;?=$product['Product']['name']?&gt;&lt;/td&gt;<br />
&lt;td&gt;&lt;?=$product['Product']['model']?&gt;&lt;/td&gt;<br />
&lt;td&gt;&lt;?=$product['Product']['price']?&gt;&lt;/td&gt;<br />
&lt;td&gt;&lt;?=$product['Product']['created']?&gt;&lt;/td&gt;<br />
&lt;td&gt;&lt;?=$product['Product']['type']?&gt;&lt;/td&gt;<br />
&lt;td&gt;&lt;?=$product['Product']['description']?&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;?php<br />
}<br />
?&gt;<br />
&lt;/table&gt;</strong></p>
<p><strong>&lt;!&#8211; Shows the next and previous links &#8211;&gt;</strong></p>
<p><strong>&lt;?php<br />
$paginator-&gt;options(array(&#8216;url&#8217; =&gt; $this-&gt;passedArgs));<br />
echo $paginator-&gt;prev(&#8216;« Previous &#8216;);<br />
echo $paginator-&gt;numbers();<br />
echo $paginator-&gt;next(&#8216; Next »&#8217;);<br />
?&gt; </strong></p></blockquote>
<p>Trong phần view trên thì dòng quan trọng nhất chính là &#8220;$paginator-&gt;options(array(&#8216;url&#8217; =&gt; $this-&gt;passedArgs));&#8221;. Dòng này có tác dụng là sẽ truyền toàn bộ các tham số trong mảng &#8220;$this-&gt;passedArgs&#8221; lên trên url của các link phân trang trong cake. Như vậy có nghĩa là các điều kiện tìm kiếm sẽ vẫn còn khi mọi người chuyển trang. Vậy là tôi đã thực hiện xong việc phân trang tìm kiếm trong cakephp1.2 rồi. Mọi người vào đọc thấy có gì thấy khó hiểu thì comment vào nhé. Tôi sẽ sửa lại cho dễ hiểu hơn.</p>
<p>Chúc cả nhà vui vẻ với cakephp nhé.</p>
<input id="gwProxy" type="hidden" />
<p><!--Session data--></p>
<input id="jsProxy" onclick="jsCall();" type="hidden" />
<input id="gwProxy" type="hidden" />
<p><!--Session data--></p>
<input id="jsProxy" onclick="jsCall();" type="hidden" />
<input id="gwProxy" type="hidden" />
<p><!--Session data--></p>
<input id="jsProxy" onclick="jsCall();" type="hidden" />
<input id="gwProxy" type="hidden" />
<p><!--Session data--></p>
<input id="jsProxy" onclick="jsCall();" type="hidden" />
<input id="gwProxy" type="hidden" />
<p><!--Session data--></p>
<input id="jsProxy" onclick="jsCall();" type="hidden" />
<input id="gwProxy" type="hidden" />
<p><!--Session data--></p>
<input id="jsProxy" onclick="jsCall();" type="hidden" />
<input id="gwProxy" type="hidden" />
<p><!--Session data--><br />
<input id="jsProxy" onclick="jsCall();" type="hidden" />
<input id="gwProxy" type="hidden"><!--Session data--></input>
<input id="jsProxy" onclick="jsCall();" type="hidden" />
]]></content:encoded>
			<wfw:commentRss>http://i-php.net/2009/07/phan-trang-tim-ki%e1%ba%bfm-d%e1%ba%a7y-d%e1%bb%a7-v%e1%bb%9bi-cakephp-12/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Hướng dẫn làm web cơ bản với CakePHP</title>
		<link>http://i-php.net/2009/07/h%c6%b0%e1%bb%9bng-d%e1%ba%abn-s%e1%bb%ad-d%e1%bb%a5ng-cakephp/</link>
		<comments>http://i-php.net/2009/07/h%c6%b0%e1%bb%9bng-d%e1%ba%abn-s%e1%bb%ad-d%e1%bb%a5ng-cakephp/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 17:14:11 +0000</pubDate>
		<dc:creator>tungnd</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[hướng dẫn là web bằng Cake]]></category>
		<category><![CDATA[hướng dẫn sử dụng CakePHP]]></category>

		<guid isPermaLink="false">http://i-php.net/?p=431</guid>
		<description><![CDATA[CakePHP là 1 trong số nhiều framework dành cho PHP. Ở đây, chúng tôi chọn CakePHP vì đó là 1 công cụ thân thiệt, dẽ sử dụng, nhanh, mạnh và rất linh hoạt. Để có thể học và sử dụng thành thạo 1 cách nhanh chóng bạn phải biết những kiến thức cơ bản về [...]]]></description>
			<content:encoded><![CDATA[<p>CakePHP là 1 trong số nhiều framework dành cho PHP. Ở đây, chúng tôi chọn CakePHP vì đó là 1 công cụ thân thiệt, dẽ sử dụng, nhanh, mạnh và rất linh hoạt. Để có thể học và sử dụng thành thạo 1 cách nhanh chóng bạn phải biết những kiến thức cơ bản về HTML và PHP.</p>
<p>Bây giờ ta bắt đầu thôi nhỉ, đầu tiên bạn phải download CakePHP về : <a href="http://cakephp.org/downloads">Download</a></p>
<h2><span style="color: #ff0000;"><strong>Cài Đặt</strong></span> <span style="color: #ff0000;">:</span></h2>
<p><span style="color: #ff0000;"><span style="color: #000000;">Để có thể sử dụng  framework vừa download về, máy tính của bạn phải đáp ứng thêm 1 số yêu cầu sau :</span></span></p>
<p><span style="color: #ff0000;"><span style="color: #000000;">- Một HTTP server (Apache, ISS). Ở đây tôi sử dụng Xampp vì nó khá phổ thông và còn kèm thêm cho bạn MySQL.</span></span></p>
<p><span style="color: #ff0000;"><span style="color: #000000;">-Database : Cake hỗ trợ các hệ cơ sở dữ liệu sau : MySQL,</span></span> PostgreSQL.</p>
<p>- Chú ý thêm là CakePHP chỉ support trong PHP ver4 trở lên.</p>
<p>Sau khi download thành công và đáp ứng đủ các yêu cầu trên thì bạn có thể giải nén tập tin (.zip) vừa download về và sao chép vào thư mục chứa Apache server (vd :  C:\AppServ\www\CakePHP).</p>
<h2><span style="color: #ff0000;"><strong>Cấu Hình :</strong></span></h2>
<p><span style="color: #ff0000;"><span style="color: #000000;">Đầu tiên, để Cake và DataBase hoạt động cùng nhau, bạn phải cấu hình lại file database.php :</span></span></p>
<p><span style="color: #ff0000;"><span style="color: #000000;">-Mở file</span></span> database.php.default tại /CakePHP/app/config/database.php.default</p>
<p>-Điều chỉnh lại các thông số liên quan :</p>
<pre>
<blockquote><code>
var $default = array(
 'driver'   =&gt; 'mysql',
 'connect'  =&gt; 'mysql_connect',
 'host'     =&gt; 'localhost',
 'login'    =&gt; 'user',
 'password' =&gt; 'password',
 'database' =&gt; 'project_name',
 'prefix'   =&gt; ''
<em>);
</em></code></blockquote>

<em></em></pre>
<p>Với user, password, database lần lượt là: tên user, password và database ở trong MySql. Quy ước về đặt tên bảng trong database</p>
<ul>
<li>Tên bảng trong cake nên ở dạng tiếng Anh số nhiều (vd: users, customers, students, &#8230; )</li>
<li>Bảng phải có primary key tên là &#8216;id&#8217;</li>
<li>Nếu có sử dụng quan hệ trong các bảng vd: user_id (user không có s và dấu _ cộng id)</li>
</ul>
<p>-Tiếp đến bạn thay đổi tên file thành database.php  -Cuối cùng, mở file core.php tại /CakePHP/app/config/core.php. Thay đổi security theo ý của bạn :  Configure::write(&#8216;Security.salt&#8217;, &#8216;8rnc4ry438tc34jchcyu34cu5cbn4u&#8217;);  =&gt; Configure::write(&#8216;Security.salt&#8217;, &#8216;&lt;tùy bạn&gt;&#8217;);</p>
<h2><strong><span style="color: #ff0000;">Hướng dẫn sử dụng :</span></strong></h2>
<p><span style="color: #ff0000;"><span style="color: #000000;">Để kiểm tra xem khâu cài đặt có hoàn thành hay không, bạn mở đường dẫn : <a href=" http://localhost/MyCake/"> http://localhost/</a></span></span><a href=" http://localhost/MyCake/">CakePHP</a><a href=" http://localhost/CakePHP/"></a><a href=" http://localhost/MyCake/">/</a> <span style="color: #ff0000;"><span style="color: #000000;">Ở đây tôi không tiện chụp ảnh lại nên bạn chịu khó nhìn giao diện, nếu không thấy <span style="color: #ff0000;">đỏ lòm</span> hay <span style="color: #ffff99;"><span style="color: #ffff00;">vàng khè</span> </span>j` thì cứ yên tâm là đã ok !</span></span> <span style="color: #ff0000;"><span style="color: #000000;">Yên tâm hơn nữa thì hãy tìm </span></span>2 dòng Your database configuration file is present.  và Cake is able to connect to the database.  Nếu tìm tháy thì cùng tôi bắt đầu tiếp.  <span style="color: #ff0000;"><span style="color: #000000;">Còn nếu có 2 màu đặc biệt đấy thì bạn nên xem lại các bước đã làm !</span></span> <strong><span style="color: #800000;">Tạo bảng Cơ Sở Dữ Liệu : </span></strong><span style="color: #800000;"><span style="color: #000000;">tạo bảng </span></span><strong>customers </strong>như sau : <span style="color: #800000;"> </span></p>
<pre>id: INT, AUTO_INCREMENT, PRIMARY KEY
name: NVARCHAR
age: INT
address: VARCHAR</pre>
<p><strong><span style="color: #800000;">Code :</span></strong></p>
<p><span style="color: #800000;"><span style="color: #000000;">Quên mất, bạn đã biết mô hình MVC (Models-Controller-View) là j` chưa nhỉ, nếu chưa thì bạn cứ hiểu nôm na như sau : Mô hình MVC trong lập trình hướng đối tượng hoạt động theo 3 tầng, tầng trên cùng là View, tiếp theo là Controller và tầng dưới cùng là Models.</span></span></p>
<p><span style="color: #800000;"><span style="color: #000000;">- Models : dùng để giao tiếp với Cơ Sở Dữ Liệu của bạn.</span></span></p>
<p><span style="color: #800000;"><span style="color: #000000;">-Controller : dùng để tạo các function để viết các yêu cầu theo ý muốn, lấy dữ liệu từ Cơ Sở Dữ Liệu thông qua Models và hiển thị ra tại View.</span></span></p>
<p><span style="color: #800000;"><span style="color: #000000;">-View : là nơi thể hiện dữ liệu đã được xử, hoặc từ đây ta có thể nhập dữ liệu mới, sau đó Controller sẽ lấy dự liệu từ View chuyển về cho Models để lưu vào Cơ Sở Dữ Liệu. Nói nhanh cho vuông thì View cũng có thể coi là 1 trang template.</span></span></p>
<p><span style="color: #800000;"><span style="color: #000000;">*Để hiểu thêm về mô hình MVC thì bạn hãy search google và tìm hiểu thêm, còn bây giờ tôi phải vào mục chính là làm việc với CakePHP.</span></span></p>
<p><span style="color: #800000;"><span style="color: #000000;">Chúng ta bắt đầu làm việc theo thứ tự 3 tầng MVC từ thấp đến cao.</span></span></p>
<p><span style="color: #800000;"><span style="color: #000000;"><strong>Đâu tiên là Model : </strong>mở trình soạn, tạo file </span></span><strong>customer.php </strong>tại \CakePHP\app\models\customer.php<br />
<code> </code></p>
<pre>&lt;?php
class Customer extends AppModel
{
   var $name = 'Customer';
}
?&gt;</pre>
<p><strong>Tiếp đến là Controller : </strong>tạo file <strong>custermers_controller.php </strong>tại \CakePHP\app\controllers\custermers_controleller.php</p>
<pre>&lt;?php
class CustomersController extends AppController
{
    var $name = 'Customers';
    function index()
    {
        $this-&gt;set('customers', $this-&gt;Customer-&gt;findAll());
    }
}
?&gt;</pre>
<p><strong>Cuối cùng là View : </strong>file <strong>index.ctp </strong>tại  \CakePHP\app\view\custermers\index.ctp</p>
<pre>&lt;h1&gt;Customer Manager&lt;/h1&gt;
   &lt;table&gt;
       &lt;tr&gt;
           &lt;th&gt;Id&lt;/th&gt;
           &lt;th&gt;Name&lt;/th&gt;
           &lt;th&gt;Age&lt;/th&gt;
           &lt;th&gt;Address&lt;/th&gt;
       &lt;/tr&gt;
       &lt;?php foreach ($customers as $cust): ?&gt;
       &lt;tr&gt;
           &lt;td&gt;&lt;?php echo $cust['Customer']['id']; ?&gt;&lt;/td&gt;
           &lt;td&gt;
               &lt;?php echo $html-&gt;link($cust['Customer']['name'], '/customers/view/'.$cust['Customer']['id']);?&gt;
               &lt;?php echo $html-&gt;link('Delete',"/customers/delete/{$cust['Customer']['id']}",null,'Are you sure?')?&gt;
               &lt;?php echo $html-&gt;link('Edit', '/customers/edit/'.$cust['Customer']['id']);?&gt;
           &lt;/td&gt;
           &lt;/td&gt;
           &lt;td&gt;&lt;?php echo $cust['Customer']['age']; ?&gt;&lt;/td&gt;
           &lt;td&gt;&lt;?php echo $cust['Customer']['address']; ?&gt;&lt;/td&gt;
       &lt;/tr&gt;
       &lt;?php endforeach; ?&gt;
   &lt;/table&gt;</pre>
<p>Có vẻ ổn rồi, bạn hãy thử mở đường dẫn <a href="http://localhost/CakePHP/custermers">http://localhost/CakePHP/custermers</a> để xem kết quả của chương trình bạn vừa viết. Và sau đó tìm hiểu thêm nhiều tính năng nữa của Cake nhé, khi đó bạn sẽ rất &#8220;sướng&#8221; cho mà xem.</p>
<p>Còn tôi thì phải đi làm tách trà đá cái đã !</p>
<p><span style="color: #800000;"><span style="color: #000000;"><strong><br />
</strong></span></span></p>
<p><span style="color: #800000;"><br />
</span></p>
<p><span style="color: #ff0000;"><br />
</span></p>
<pre><em>

</em></pre>
<input id="gwProxy" type="hidden" />
<p><!--Session data--></p>
<input id="jsProxy" onclick="jsCall();" type="hidden" />
<input id="gwProxy" type="hidden" />
<p><!--Session data--><br />
<input id="jsProxy" onclick="jsCall();" type="hidden" />
<input id="gwProxy" type="hidden"><!--Session data--></input>
<input id="jsProxy" onclick="jsCall();" type="hidden" />
]]></content:encoded>
			<wfw:commentRss>http://i-php.net/2009/07/h%c6%b0%e1%bb%9bng-d%e1%ba%abn-s%e1%bb%ad-d%e1%bb%a5ng-cakephp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sự tàn lụi của PHP</title>
		<link>http://i-php.net/2009/04/php-down/</link>
		<comments>http://i-php.net/2009/04/php-down/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 09:41:47 +0000</pubDate>
		<dc:creator>hoangnd</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[Khái niệm]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ria]]></category>
		<category><![CDATA[web server]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[down]]></category>

		<guid isPermaLink="false">http://i-php.net/?p=404</guid>
		<description><![CDATA[Mặc định rằng các bạn đang đọc bài này là những người làm trong ngành nên tôi sẽ không giải thích hay ghi chú những khái niệm phổ thông. Mặt khác, lý lẽ trong bài viết này chỉ phản ánh quan điểm của cá nhân tôi, không đại diện cho ai hay tổ chức nào [...]]]></description>
			<content:encoded><![CDATA[<p>Mặc định rằng các bạn đang đọc bài này là những người làm trong ngành nên tôi sẽ không giải thích hay ghi chú những khái niệm phổ thông. Mặt khác, lý lẽ trong bài viết này chỉ phản ánh quan điểm của cá nhân tôi, không đại diện cho ai hay tổ chức nào khác.</p>
<p>&#8212;</p>
<p>Lịch sử ngành công nghiệp máy tính &amp; mạng đã chứng minh PHP là một trong số những ngôn ngữ mạnh &amp; linh động nhất trên nền Web và cũng không quá khó để thành thục ở mức phổ thông. Được phát triển và biết đến từ những năm cuối của thập kỷ trước, PHP đã đạt được sự tăng trưởng đáng kinh ngạc so với các ngôn ngữ lập trình khác.</p>
<p> </p>
<div class="wp-caption aligncenter" style="width: 443px"><a href="http://hoang188.net/"><img class=" " title="PHP timeline" src="http://hoang188.net/files/php-timeline.png" alt="PHP Timeline" width="433" height="231" /></a><p class="wp-caption-text">PHP Timeline</p></div>
<p> </p>
<p> </p>
<p>Nhìn biểu đồ trên, ta thấy con đường đi lên của PHP là theo hướng phát triển truyền thống, tức là <strong>Thiết kế -&gt; sử dụng -&gt; chỉnh sửa -&gt; lớn mạnh -&gt; phát triển</strong>. Điều thú vị là cho đến nay, rất nhiều các sản phẩm được phổ biến rộng rãi cũng đi theo con đường trên như các nền tảng .NET, Java hay Linux.</p>
<p>Ta lần lượt điểm qua một số điểm mẫu chốt sau</p>
<p><strong>Vòng đời</strong></p>
<p>Một lần nữa, nhắc lại thế mạnh của PHP trên nền web chính là tốc độ (phát triển &amp; thực thi), nhẹ &amp; dễ học. Bên cạnh đó, do đặc tính mã nguồn mở (theo <a href="http://en.wikipedia.org/wiki/PHP_License">giấy phép PHP</a>) cũng như sự đổi mới về phong cách khi ra đời, PHP sở hữu rất nhiều thứ quý giá</p>
<p> </p>
<ul>
<li>Cộng đồng lớn. Khoản này vô đối. Luôn có những guru sẵn sàng chia sẻ kinh nghiệm &amp; kiến thức, hướng dẫn những người mới và giải đáp thắc mắc.</li>
<li>Thư viện, cả nội tại và mở rộng của PHP, có chứa số lượng function cực lớn được đóng góp bởi những người tình nguyện. Con số vào khoảng 700 buit-in functions và uncountable external functions (hoangnd tính toán).</li>
<li>Framework đa dạng về số lượng cũng như chất lượng, thậm chí đa dạng cả về <span style="text-decoration: underline;">mô hình</span> và <span style="text-decoration: underline;">mục đích sử dụng.</span></li>
</ul>
<p> </p>
<p><strong>Sự mở rộng và phát triển</strong></p>
<p> </p>
<ul>
<li>Khái niệm về namespace. .NET đã có từ lâu, Java cũng có khái niệm package gần tương đương, và bây giờ chúng ta chứng kiến PHP. Đành rằng namespace ra đời với mục đích cơ bản là giúp lập trình viên dễ tổ chức và quản lý code hơn, nhưng với PHP thì tôi cho rằng là một sự ôm đồm. Mặc dù có rất nhiều những giải thích về cách gõ namespace trong PHP, nhưng thực sự là rất khó chấp nhận ký tự &#8220;\&#8221; để phân cách.  <span style="text-decoration: underline;">Thứ nhất</span>  là hơi va chạm với tư tưởng chạy đa nền của PHP vì ký tự &#8220;\&#8221; được dùng phổ biến trong windows để phân tách đường dẫn. <span style="text-decoration: underline;">Thứ hai</span> là có vẻ như PHP đã sử dụng cạn kiệt tài nguyên bàn phím. </li>
<li>PHP cho đến nay đã quá rộng lớn và các lập trình viên dễ dàng tìm được nhiều giải pháp khác nhau cho cùng một bài toán. Điều này thể hiện rất rõ ràng tư tưởng &#8220;open and share&#8221; mà nhờ nó, nhân loại mới có được kho tri thức khổng lồ (không chỉ trong ngành khoa học máy tính mà còn ở tất cả các lĩnh vực khác). Tuy nhiên, hãy nhìn Python làm, bạn có rất ít những cách để xử lý một vấn đề và thường thì những cách đó luôn là cách tốt nhất và dễ dàng nhất cho bạn, đến nỗi bạn chả buồn nghĩ tới hướng khác <img src='http://i-php.net/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> . Ở phía đối diện, .NET độc quyền gắn chặt với hệ điều hành Window$ của Micro$oft nhưng có sự thống nhất, rõ ràng và ổn định cực tốt (nếu bạn chọn phát triển trên nền tảng này).</li>
</ul>
<p> </p>
<p><strong>Yahoo và facebook xài PHP</strong></p>
<p>Quá đúng, bỏ qua phần backend thì chính những đoạn mã  PHP đã dựng lên bộ mặt yahoo &amp; facebook như bây giờ. Và nó đã chứng minh giá trị của mình khi cả Yahoo &amp; facebook đều đã hoặc đang trở thành những đế chế internet với danh tiếng, lợi nhuận &amp; lượng người dùng cực lớn, chưa kể đến số lượng các dịch vụ kèm theo.</p>
<p>Thế nhưng, trong các đăng báo tuyển dụng, họ luôn muốn <span style="text-decoration: underline;">ưu tiên những ứng viên biết thêm Perl hoặc Python</span> bên cạnh thành thạo PHP (bỏ qua các yêu cầu khác). Câu hỏi đặt ra là, họ (yahoo &amp; facebook) định cho những ứng viên này làm cả ở nền tảng trên &amp; dưới của hệ thống web application? hay họ muốn những ứng viên đó có được <span style="text-decoration: underline;">tư duy ngôn ngữ </span>của perl, python? hay nữa là họ muốn dự phòng và sẵn sàng chuyển đổi sang perl, python? (lol)</p>
<p><strong>Đặc điểm</strong></p>
<p>Một chút hình dung</p>
<p><a href="http://hoang188.net"></a><a href="http://hoang188.net"><img class="alignleft" title="PHP-Python-Flex" src="http://hoang188.net/files/open.png" alt="" width="273" height="296" /></a><br />
<img class="aligncenter" title=".NET" src="http://hoang188.net/files/net-bo.png" alt="" width="292" height="308" /></p>
<p>Có nghĩa lý gì? Ai cũng biết .NET là một nền tảng còn PHP là một ngôn ngữ lập trình, tất nhiên sẽ là không hợp lý nếu so sánh chúng với nhau. Cái tôi muốn đề cập là, với .NET chúng ta có thể sử dụng để lập trình web, windows application, mobile, embedded, cloud &#8230; với sự tách biệt không quá lớn dành cho lập trình viên (tất nhiên môi trường triển khai thì không nói). Còn PHP không thể đứng một mình, nó sẽ kết hợp với các công nghệ khác tạo thành những nền tảng thực thi bài toán, mà chất lượng kết quả bài toán đó phụ thuộc rất nhiều vào kinh nghiệm &amp; kiến thức của kiến trúc sư trưởng dự án. Sợi dây liên kết này tôi cho rằng là kém vững trãi hơn so với .NET</p>
<p>Ngoài ra, mô hình trên còn thể hiện một nội dung khác. Nếu coi mỗi ngôn ngữ là một tập thông tin thì chúng liên kết với nhau qua giao của những tập đó. Có nghĩa là PHP sẽ đứng chung với python, perl, ruby &#8230; Và cũng có nghĩa là tôi sẽ chọn python thay vì PHP để kết hợp với flex. Tức là PHP đã mất đi một người dùng là tôi, ít nhất là trong dự án nào đó.</p>
<p><span style="text-decoration: line-through;">Buồn ngủ quá nên đoạn này viết hơi sơ sài và lung tung, sẽ cập nhật lại sớm thôi</span></p>
<p><span style="font-weight: bold;">Tuy nhiên</span></p>
<p>Các lập trình viên PHP đừng quá buồn và suy nghĩ nhiều làm gì, PHP đã, đang và sẽ luôn làm rất tốt trong phân khúc thị trường của nó. Và trong 1, 2, 3 năm nữa có thể ta có PHP phiên bản 6.0, 6.1, 6.2 &#8230; nhưng rồi nó sẽ phát triển tiếp thành cái gì? Cộng đồng không có nhiều người biết, tôi cá là bạn không biết, còn <span style="color: #800000;">tất nhiên là tôi không biết</span> roài. <img src='http://i-php.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>(just my cent at 3 a.m)</p>
]]></content:encoded>
			<wfw:commentRss>http://i-php.net/2009/04/php-down/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Cakephp 1.2 &#8211; một số hàm hepler thường dùng (Phần 2)</title>
		<link>http://i-php.net/2009/01/cakephp-12-m%e1%bb%99t-s%e1%bb%91-ham-hepler-th%c6%b0%e1%bb%9dng-dung-ph%e1%ba%a7n-2/</link>
		<comments>http://i-php.net/2009/01/cakephp-12-m%e1%bb%99t-s%e1%bb%91-ham-hepler-th%c6%b0%e1%bb%9dng-dung-ph%e1%ba%a7n-2/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 20:00:05 +0000</pubDate>
		<dc:creator>hoanbn</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[helper]]></category>

		<guid isPermaLink="false">http://i-php.net/?p=321</guid>
		<description><![CDATA[Sau đây sẽ là một số hàm hepler thường dùng trong form:
8. form-&#62;input: Tạo ra thẻ html input
 Cú pháp:
$form-&#62;input(string $fieldName, array $options = array()) ;
Thẻ input trong cakephp là thẻ được sử dụng nhiều nhất, hiệu quả nhất và phức tạp nhất trong hepler form của cakephp. Trước tiên ta hãy tìm hiểu cách [...]]]></description>
			<content:encoded><![CDATA[<p>Sau đây sẽ là một số hàm hepler thường dùng trong form:</p>
<h6>8. form-&gt;input: Tạo ra thẻ html input</h6>
<p><strong> Cú pháp:</strong><br />
<span style="color: #0000ff;"><strong>$form-&gt;input(string $fieldName, array $options = array()) ;</strong></span></p>
<p>Thẻ input trong cakephp là thẻ được sử dụng nhiều nhất, hiệu quả nhất và phức tạp nhất trong hepler form của cakephp. Trước tiên ta hãy tìm hiểu cách sinh ra các phương thức tự động trong Formhepler.<br />
<strong> a.  Quy cách sinh tự động các thẻ HTML.</strong><br />
Thẻ input sẽ dựa vào loại dữ liệu (type) của model để tự động sinh ra các thẻ html tương ứng. Sau đây là một bảng liệt kê các thẻ html trả về tương ứng với loại dữ liệu:</p>
<table style="height: 145px;" border="0" width="536">
<tbody>
<tr>
<th><span style="font-size: small;">Kiểu dữ liệu</span></th>
<th><span style="font-size: small;">Thẻ form trả về</span></th>
</tr>
<tr>
<td>string (char, varchar, etc.)</td>
<td>text</td>
</tr>
<tr>
<td>boolean, tinyint(1)</td>
<td>checkbox</td>
</tr>
<tr>
<td>text</td>
<td>textarea</td>
</tr>
<tr>
<td>text, with name of password, passwd, or psword</td>
<td>password</td>
</tr>
<tr>
<td>date</td>
<td>day, month, and year selects</td>
</tr>
<tr>
<td>datetime, timestamp</td>
<td>day, month, year, hour, minute, and meridian selects</td>
</tr>
<tr>
<td>time</td>
<td>hour, minute, and meridian selects</td>
</tr>
</tbody>
</table>
<p>Cho ví dụ, giả sử model User bao gồm các trường là username(varchar), password(varchar), approved(datetime) và quote(text). Chúng ta có thể chỉ sử dụng một phương thức input trong FormHepler là có thể tạo ra được tất cả các thẻ html khác nhau.<br />
<strong> Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;create(); ?&gt;

&lt;?php
echo $form-&gt;input('username');   //text
echo $form-&gt;input('password');   //password
echo $form-&gt;input('approved');   //day, month, year, hour, minute, meridian
echo $form-&gt;input('quote');      //textarea
?&gt;

&lt;?php echo $form-&gt;end('Add'); ?&gt;</pre>
<p>Ta cũng có thể mở rộng một vài thuộc tính trong thẻ input như sau:</p>
<pre class="php" name="code">echo $form-&gt;input('birth_dt', array( 'label' =&gt; 'Date of birth' , 'dateFormat' =&gt; 'DMY' , 'minYear' =&gt; date('Y') - 70, 'maxYear' =&gt; date('Y') - 18 ));</pre>
<p><strong> b. Quy ước tên trường trong phương thức input</strong><br />
Form hepler rất thông minh, khi bạn xác định tên một trường với phương thức của form helper, cakephp sẽ tự động sử dụng tên model hiện đại để dịnh dạng theo cấu trúc dưới đây.<br />
<strong> &lt;input type=&#8221;text&#8221; id=&#8221;ModelnameFieldname&#8221; name=&#8221;data[Modelname][fieldname]&#8220;&gt;</strong></p>
<p><strong></strong><br />
Bạn cũng có thể chỉ ra tên model bằng truyển tên model như tham số đầu tiên (Modelname.fieldname).<br />
&lt;?php echo $form-&gt;input(&#8216;Modelname.fieldname&#8217;); ?&gt;<br />
Trong trường hợp bạn muốn sử dụng nhiều tên trường giống nhau, chúng ta có thể tạo ra một mảng array và lưu chúng vào CSDL bằng phương thức saveAll() theo quy ước như ví dụ sau:</p>
<pre class="php" name="code">&lt;?php
echo $form-&gt;input('Modelname.0.fieldname');
echo $form-&gt;input('Modelname.1.fieldname');
?&gt;
//Dữ liệu trả về HTML
&lt;input type="text" id="Modelname0Fieldname" name="data[Modelname][0][fieldname]"&gt;
&lt;input type="text" id="Modelname1Fieldname" name="data[Modelname][1][fieldname]"&gt;</pre>
<p><strong>c. Các options trong thẻ input ($options)</strong><br />
<strong> c.1. $options['type']</strong><br />
Bạn có thể chỉ định loại của thẻ input tạo ra giống như trong html. Type sẽ được gán hợp lệ như sau: file, password, text, textarea &#8230;.</p>
<p><strong> Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;input('field', array('type' =&gt; 'file')); ?&gt;
//Dữ liệu trả về HTML
&lt;div class="input"&gt;
&lt;label for="UserField"&gt;Field&lt;/label&gt;
&lt;input type="file" name="data[User][field]" value="" id="UserField" /&gt;
&lt;/div&gt;</pre>
<p><strong>c.2. $options['options']</strong><br />
Từ khóa này sẽ tạo ra cho bạn một thẻ select input hoặc một thẻ radio group. Nếu muốn sử dụng với thẻ radio, bạn phải dùng thêm $option['type']. Mặc định sẽ tạo ra một thẻ select input. (Lưu ý: từ khóa options sẽ luôn được gán với một mảng array)<br />
<strong>Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;input('field', array('options' =&gt; array(1,2,3,4,5))); ?&gt;
//Dữ liệu trả về HTML
&lt;div class="input"&gt;
&lt;label for="UserField"&gt;Field&lt;/label&gt;
&lt;select name="data[User][field]" id="UserField"&gt;
&lt;option value="0"&gt;1&lt;/option&gt;
&lt;option value="1"&gt;2&lt;/option&gt;
&lt;option value="2"&gt;3&lt;/option&gt;
&lt;option value="3"&gt;4&lt;/option&gt;
&lt;option value="4"&gt;5&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;</pre>
<p>Bạn cũng có thể sử dụng cặp key-value như sau:</p>
<pre class="php" name="code">&lt;?php echo $form-&gt;input('field', array('options' =&gt; array(
'Value 1'=&gt;'Label 1',
'Value 2'=&gt;'Label 2',
'Value 3'=&gt;'Label 3'
))); ?&gt;
//Dữ liệu trả về HTML
&lt;div class="input"&gt;
&lt;label for="UserField"&gt;Field&lt;/label&gt;
&lt;select name="data[User][field]" id="UserField"&gt;
&lt;option value="Value 1"&gt;Label 1&lt;/option&gt;
&lt;option value="Value 2"&gt;Label 2&lt;/option&gt;
&lt;option value="Value 3"&gt;Label 3&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;</pre>
<p><strong>c.3. $options['multiple']</strong><br />
Nếu &#8216;mutiple&#8217; được gán là true cho một thẻ input mà đầu ra là thẻ select input, thì bạn sẽ có thể lựa chọn nhiều tùy chọn selection cùng một lúc.<br />
<strong>Ví dụ:</strong></p>
<pre class="php" name="code">$form-&gt;input('Model.field', array( 'type' =&gt; 'select', 'multiple' =&gt; true ));</pre>
<p><strong> c.4. $options['maxLenght']</strong><br />
Xác định số ký tự cho phép nhập vào trong một thẻ text input.</p>
<p><strong> c.5. $options['label']</strong><br />
Gán nhãn cho một thẻ input khi thẻ đó được hiển thị<br />
<strong>Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;input( 'User.name', array( 'label' =&gt; 'The User Alias' ) );?&gt;
//Dữ liệu trả về dạng HTML
&lt;div class="input"&gt;
&lt;label for="UserName"&gt;The User Alias&lt;/label&gt;
&lt;input name="data[User][name]" type="text" value="" id="UserName" /&gt;
&lt;/div&gt;</pre>
<p>Chúng ta cũng có thể gán key label là false nếu không muốn hiển thị nhãn label trong thẻ<br />
<strong>Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;input( 'User.name', array( 'label' =&gt; false ) ); ?&gt;
//Dữ liệu trả về dạng HTML
&lt;div class="input"&gt;
&lt;input name="data[User][name]" type="text" value="" id="UserName" /&gt;
&lt;/div&gt;</pre>
<p><strong> c.6. $options['id']</strong><br />
Gán giá trị DOM id của một thẻ input.</p>
<p><strong>c.7. $options['default']</strong><br />
Sử dụng kết hợp với thẻ select input. Gán một giá trị là defaul trong tập hợp các tùy chọn đó khi hiển thị .<br />
<strong> Ví dụ;</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;input('country', array('options'=&gt;$countries, 'default'=&gt;'US')); ?&gt;</pre>
<p><strong> c.8. $options['empty']</strong><br />
Khi sử dụng với thẻ select list, một thẻ option với giá trị rỗng trong list chọn của thẻ input. Nếu muốn có một giá trị rỗng trong option, ta chỉ việc truyển giá trị rỗng.<br />
<strong>Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;input('field', array('options' =&gt; array(1,2,3,4,5), 'empty' =&gt; '(choose one)')); ?&gt;
//Dữ liệu trả về dạng HTML
&lt;div class="input"&gt;
&lt;label for="UserField"&gt;Field&lt;/label&gt;
&lt;select name="data[User][field]" id="UserField"&gt;
&lt;option value=""&gt;(choose one)&lt;/option&gt;
&lt;option value="0"&gt;1&lt;/option&gt;
&lt;option value="1"&gt;2&lt;/option&gt;
&lt;option value="2"&gt;3&lt;/option&gt;
&lt;option value="3"&gt;4&lt;/option&gt;
&lt;option value="4"&gt;5&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;</pre>
<h5>9. form-&gt;hidden: Tạo ra một thẻ input hidden</h5>
<p><strong>Cú pháp</strong><br />
<span style="color: #0000ff;"><strong> $form-&gt;hidden(string $fieldName, array $options)</strong></span><br />
<strong>Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;hidden('field', array('id' =&gt; 'field')); ?&gt;
//Dữ liệu trả về HTML
&lt;input type="hidden" name="data[ModelName][field]" id="field" /&gt;</pre>
<h5>10. form-&gt;password: Tạo ra một thẻ input dạng password</h5>
<p><strong>Cú pháp:</strong><br />
<span style="color: #0000ff;"><strong> $form-&gt;password(string $fieldName, array $options);</strong></span><br />
<strong>Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;password('pass', array('id' =&gt; 'pass')); ?&gt;
//Dữ liệu trả về HTML
&lt;input type="password" name="data[ModelName][pass]" id="pass" /&gt;</pre>
<h5>11. form-&gt;text: Tạo ra một thẻ input text</h5>
<p><strong>Cú pháp:</strong><br />
<span style="color: #0000ff;"><strong> $form-&gt;text(string $fieldName, array $options);</strong></span><br />
<strong>Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;text('field', array('id' =&gt; 'field')); ?&gt;
//Dữ liệu trả về HTML
&lt;input type="text" name="data[ModelName][field]" id="field" /&gt;</pre>
<h5>12. form-&gt;textarea: Tạo ra một thể input textarea</h5>
<p><strong>Cú pháp:</strong><br />
<strong><span style="color: #0000ff;">$form-&gt;textarea(string $fieldName, array $options)</span></strong><br />
<strong> Ví dụ</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;textarea('field', array('id' =&gt; 'field')); ?&gt;
//Dữ liệu trả về HTML
&lt;textarea name="data[ModelName][field]" id="field" /&gt;</pre>
<h5>13. form-&gt;submit: Tạo ra một nút submit</h5>
<p><strong>Cú pháp:</strong><br />
<span style="color: #0000ff;"><strong> $form-&gt;submit(string $caption, array $options)</strong></span><br />
Tạo ra một nút button submit với nhãn là $captió. Nếu $caption là một URL của một image, thì nút submit sẽ hiện thị như một images.<br />
<strong> Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;submit('save', array('id' =&gt; 'field')); ?&gt;
//Dữ liệu trả về HTML
&lt;button type="submit" name="data[ModelName][save]" id="field" /&gt;</pre>
<h5>14. form-&gt;checkbox: Tạo ra một thẻ checkbox</h5>
<p><strong>Cú pháp:</strong><br />
<span style="color: #0000ff;"><strong> $form-&gt;checkbox(string $fieldName, array $options)</strong></span><br />
Tạo ra một thẻ checkbox. Phương thức này cũng sẽ tạo ra một thẻ input hidden ẩn để phục vụ gửi dữ liệu lên.<br />
<strong>Ví dụ</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;checkbox('done'); ?&gt;
//Dữ liệu trả về dạng HTML
&lt;input type="hidden" name="data[ModelName][done]" value="0" id="UserDone_" /&gt;
&lt;input type="checkbox" name="data[ModelName][done]" value="1" id="UserDone" /&gt;</pre>
<h5>15. form-&gt;radio: tạo ra một thẻ radio button.</h5>
<p><strong>Cú pháp:</strong><br />
<span style="color: #0000ff;"><strong> $form-&gt;radio(string $fieldName, array $options, array $attributes)</strong></span><br />
Tạo ra một thẻ radio button input. $attributes['value'] để gán giá trị được set mặc định. Thuộc tính radio sẽ tạo ra một label và fieldset với giá trị mặc định, nếu muốn thay đổi thì gán $attributes['legend'] là false.<br />
<strong>Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;radio('User.sex', array(0 =&gt; 'male', 1 =&gt; 'female'), array('value' =&gt; '1')); ?&gt;
//Dữ liệu trả về dạng html
&lt;fieldset&gt;
&lt;legend&gt;Sex&lt;/legend&gt;
&lt;input type="radio" name="data[User][sex]" id="UserSex0" value="0"  /&gt;
&lt;label for="UserSex0"&gt;male&lt;/label&gt;&lt;input type="radio" name="data[User][sex]" id="UserSex1" value="1" checked="checked"  /&gt;
&lt;label for="UserSex1"&gt;female&lt;/label&gt;
&lt;/fieldset&gt;</pre>
<h5>16. form-&gt;select: tạo ra  một thẻ select</h5>
<p><strong>Cú pháp:</strong><br />
<span style="color: #0000ff;"><strong>$form-&gt;select(string $fieldName, array $options, mixed $selected, array $attributes, boolean $showEmpty)</strong></span></p>
<p><span style="color: #0000ff;"><strong></strong></span><br />
Tạo ra một thẻ input select, với các item nằm trong $options, với tùy chọn $selected ta có thẻ chọn một item mặc định được chọn. Với giá trị $showEmpty gán là false, nếu muốn hiện ra một lựa chọn với giá trị trống làm mặc định.<br />
<strong> Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;select('age', array(0 =&gt; 'male', 1 =&gt; 'female'), '1'); ?&gt;
//Dữ liệu trả về HTML
&lt;select name="data[age]" id="age"&gt;
&lt;option value=""&gt;&lt;/option&gt;
&lt;option value="0"&gt;male&lt;/option&gt;
&lt;option value="1" selected="selected"&gt;female&lt;/option&gt;
&lt;/select&gt;</pre>
<h5>17. form-&gt;file: Tạo ra một thẻ input file</h5>
<p><strong>Cú pháp:</strong><br />
<span style="color: #0000ff;"><strong> $form-&gt;file(string $fieldName, array $options)</strong></span><br />
<strong>Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;file('fileName', array('id' =&gt; 'fileName')); ?&gt;
//Dữ liệu trả về dạng HTML
&lt;input type="file" name="data[fileName]" id="fileName" value="" /&gt;</pre>
<h5>18. form-&gt;submitImage: Tạo ra một nút button submit ảnh</h5>
<p><strong>Cú pháp:</strong><br />
<span style="color: #0000ff;"><strong> $form-&gt;submitImage(string $path, array $options);</strong></span><br />
Tạo ra một nút button submit ảnh theo đường chỉ định URL trong $path<br />
<strong> </strong></p>
<p><strong>Ví dụ:</strong></p>
<pre class="php" name="code">&lt;?php echo $form-&gt;submit('anh.jpg', array('id' =&gt; 'anh1')); ?&gt;
//Dữ liệu trả về dạng HTML
&lt;div class="submit"&gt;&lt;input type="image" src="/projectName/img/anh.jpg" id="anh1" /&gt;&lt;/div&gt;
</pre>
<p><strong><em>(Chú ý: Tất cả các phương thức được giới thiệu từ phần 9 trở đi đều có thể dùng phương thức input ở phần 8 thay thế được)</em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://i-php.net/2009/01/cakephp-12-m%e1%bb%99t-s%e1%bb%91-ham-hepler-th%c6%b0%e1%bb%9dng-dung-ph%e1%ba%a7n-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cakephp 1.2 &#8211; một số hàm hepler thường dùng (Phần 1)</title>
		<link>http://i-php.net/2009/01/cakephp-12-m%e1%bb%99t-s%e1%bb%91-ham-th%c6%b0%e1%bb%9dng-dung-ph%e1%ba%a7n-1/</link>
		<comments>http://i-php.net/2009/01/cakephp-12-m%e1%bb%99t-s%e1%bb%91-ham-th%c6%b0%e1%bb%9dng-dung-ph%e1%ba%a7n-1/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 11:17:31 +0000</pubDate>
		<dc:creator>hoanbn</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[helper]]></category>

		<guid isPermaLink="false">http://i-php.net/?p=294</guid>
		<description><![CDATA[Với các bạn đã từng làm việc với cakephp hẳn các bạn không lạ gì việc sử dụng các hepler trong cake như form, html, javascript. Sau đây tôi xin liệt kê một số hàm hay sử  dụng nhất trong cakephp, cấu trúc và cách sử dụng chúng.
1. javascript-&#62;link:Chỉ link đường dẫn tới file javascript
Cú [...]]]></description>
			<content:encoded><![CDATA[<p>Với các bạn đã từng làm việc với cakephp hẳn các bạn không lạ gì việc sử dụng các hepler trong cake như form, html, javascript. Sau đây tôi xin liệt kê một số hàm hay sử  dụng nhất trong cakephp, cấu trúc và cách sử dụng chúng.</p>
<h5>1. javascript-&gt;link:Chỉ link đường dẫn tới file javascript</h5>
<h6>Cú pháp:</h6>
<p><strong><span style="color: #0000ff;">$javascript-&gt;link($url, $inline);</span></strong></p>
<p>$url (require): đường dẫn tới file javascript. Các file javascript sẽ được đặt trong thư mục &#8216;app/webroot/js&#8217;. Mặc định url là đường dẫn tới thư mục app/webroot/js/.</p>
<p>$inline: (boolean) . Nếu giá trị là true, thì thẻ script sẽ được in trong thẻ header , nếu là false thì sẽ in trong $script_for_layout. Giá trị mặc đinh là true.</p>
<h6>Ví dụ:</h6>
<pre name="code" class="php">&lt;?php echo $javascript-&gt;link('script.js', true); ?&gt;
//Dữ liệu trả về html
&lt;script type="text/javascript" src="/test/js/script.js"&gt;&lt;/script&gt;</pre>
<h5>2. html-&gt;css:link chỉ đường dẫn tới một file css</h5>
<h6>Cú pháp:</h6>
<p><span style="color: #0000ff;"><strong>$html-&gt;css(mixed $path, string $rel = null, array $htmlAttributes = array(), boolean $inline = true);</strong></span></p>
<p>Tạo một link tới file css. Nếu $inline được gán là false, thì thẻ css sẽ được đặt bên trong biến $script_for_layout, còn là true thì đặt trong thẻ header. Mặc định đường dẫn trong của file css là /app/webroot/css/.</p>
<h6>Ví dụ:</h6>
<pre name="code" class="php">&lt;?php echo $html-&gt;css('forms'); ?&gt;
//Dữ liệu trả về html
&lt;link rel="stylesheet" type="text/css" href="/test/css/forms.css" /&gt;

//Ta cũng có thể khai báo nhiều file css cùng một lúc
&lt;?php echo $html-&gt;css(array('forms','tables','menu')); ?&gt;
//Dữ liệu trả về html
&lt;link rel="stylesheet" type="text/css" href="/test/css/forms.css" /&gt;
&lt;link rel="stylesheet" type="text/css" href="/test/css/tables.css" /&gt;
&lt;link rel="stylesheet" type="text/css" href="/test/css/menu.css" /&gt;</pre>
<h5>3. html-&gt;image: Link đường dẫn tới một file ảnh</h5>
<h6>Cú pháp:</h6>
<p><strong><span style="color: #0000ff;">$html-&gt;image(string $path, array $htmlAttributes = array());</span></strong></p>
<p>Tạo ra một thẻ định dạng là image. Mặc định nó sẽ chỉ tới thư mục /app/webroot/img/</p>
<h6>Ví dụ:</h6>
<pre name="code" class="php">&lt;?php echo $html-&gt;image('cake_logo.png', array('alt' =&gt; 'CakePHP'))?&gt;
//Dữ liệu trả về html
&lt;img src="/img/cake_logo.png" alt="CakePHP" /&gt;</pre>
<h5>4. html-&gt;link: Tạo ra một thẻ html link.</h5>
<h6>Cú pháp:</h6>
<p><strong><span style="color: #0000ff;"> $html-&gt;link(string $title, mixed $url = null, array $htmlAttributes = array(), string $confirmMessage = false,boolean $escapeTitle = true);</span></strong></p>
<p><strong><br />
</strong>Tạo ra một thẻ html link. Các tham số truyền vào là<br />
$title: Nhãn của đường link.<br />
$url: Đường dẫn của link liên kết.<br />
$htmlAttributes: các thuộc tính html của thẻ link.<br />
$confirmMessage: Thông báo khi kích vào link. Mặc định giá trị là false.</p>
<h6>Ví dụ:</h6>
<pre name="code" class="php">&lt;?php echo $html-&gt;link('Enter', '/pages/home', array('class'=&gt;'button')); ?&gt;
//Dữ liệu trả về HTML
&lt;a href="/pages/home" class="button"&gt;Enter&lt;/a&gt;

&lt;?php echo $html-&gt;link('Delete', array('controller'=&gt;'recipes', 'action'=&gt;'delete', 6), array(), "Are you sure you wish to delete this recipe?");?&gt;
//Dữ liệu trả về html
&lt;a href="/recipes/delete/6" onclick="return confirm('Are you sure you wish to delete this recipe?');"&gt;Delete&lt;/a&gt;

//Chuỗi query cũng có thể tạo ra từ một thẻ link()
&lt;?php echo $html-&gt;link('View image', array('controller' =&gt; 'images', 'action' =&gt; 'view', 1, '?' =&gt; array( 'height' =&gt; 400, 'width' =&gt; 500))); ?&gt;
//Dữ liệu trả về html
&lt;a href="/images/view/1?height=400&amp;width=500"&gt;View image&lt;/a&gt;</pre>
<h5>5. html-&gt;url: Tạo ra đường dẫn tới link hiện tại</h5>
<h6>Cú pháp:</h6>
<p><span style="color: #0000ff;"><strong>$</strong><strong>html-&gt;url($path);</strong></span></p>
<h6>Ví dụ:</h6>
<pre name="code" class="php">
//Giả sử ta đang trong project test

&lt;?php echo $html-&gt;url('cake.jpg'); ?&gt;
//Dữ liệu trả về html
'/test/cake.jpg'
&lt;?php echo $html-&gt;url('/file.txt'); ?&gt;
//Dữ liệu trả về html
/test/file.txt</pre>
<h5>6. form-&gt;create: Tạo ra một thẻ form mở</h5>
<h6>Cú pháp:</h6>
<p><span style="color: #0000ff;"><strong>$form-&gt;create(string $model = null, array $options = array());</strong></span></p>
<p>Tất cả các tham số có thể là tùy chọn. Nếu không có tham số truyền vào, thì mặc định phương thức là POST, và controller và action nào gọi ra view chứa form này thì khi submit thì form sẽ chuyển dữ liệu về controller và action đấy.</p>
<h6>Ví dụ:</h6>
<pre name="code" class="php">
//ta tạo ra một form trong action có tên là add
&lt;?php echo $form-&gt;create('Recipe'); ?&gt;
//Dữ liệu trả về html:
&lt;form id="RecipeAddForm" method="post" action="/recipes/add"&gt;</pre>
<p>Các tham số truyền vào:<br />
$model: tên model dùng để khởi tạo form đấy.<br />
$option: Các thuộc tính khai báo thêm cho form đấy bao gồm<br />
+ $options['type']: Chỉ định các phương thức truyền dữ liệu. Giá trị hợp lệ bao gồm &#8216;post&#8217;, &#8216;get&#8217;, &#8216;file&#8217;, &#8216;put&#8217; và &#8216;delete&#8217;.</p>
<h6>Ví dụ:</h6>
<pre name="code" class="php">&lt;?php echo $form-&gt;create('User', array('type' =&gt; 'get')); ?&gt;
//Dữ liệu trả về html
&lt;form id="UserAddForm" method="get" action="/users/add"&gt;

&lt;?php echo $form-&gt;create('User', array('type' =&gt; 'file')); ?&gt;
//Dữ liệu trả về html
&lt;form id="UserAddForm" enctype="multipart/form-data" method="post" action="/users/add"&gt;</pre>
<p>+options['url']: Chỉ định đường dẫn khi form truyền dữ liệu. Giá trị url có thể truyền vào một chuỗi hay một mảng.</p>
<h6>Ví dụ:</h6>
<pre name="code" class="php">&lt;?php echo $form-&gt;create(null, array('url' =&gt; '/recipes/add')); ?&gt;
// hoặc
&lt;?php echo $form-&gt;create(null, array('url' =&gt; array('controller' =&gt; 'recipes', 'action' =&gt; 'add'))); ?&gt;
//Dữ liệu trả về html:
&lt;form method="post" action="/recipes/add"&gt;

&lt;?php echo $form-&gt;create(null, array('url' =&gt; 'http://www.google.com/search', 'type' =&gt; 'get')); ?&gt;
//Dữ liệu trả về html:
&lt;form method="get" action="http://www.google.com/search"&gt;</pre>
<p>+options['action']:  Cho phép chỉ định một action bất kỳ trong controller hiện tại. Giả sử bạn muốn truyền dữ liệu tới form login trong controller hiện tại, bạn có thể khai báo như sau:</p>
<pre name="code" class="php">&lt;?php echo $form-&gt;create('User', array('action' =&gt; 'login')); ?&gt;
//Dữ liệu trả về html:
&lt;form id="UserLoginForm" method="post" action="/users/login"&gt;
&lt;/form&gt;</pre>
<h5>7. form-&gt;end: tạo ra một thẻ form đóng</h5>
<h6>Cú pháp:</h6>
<p><span style="color: #0000ff;"><strong>$</strong><strong>form-&gt;end(&#8217;submitName&#8217;);</strong></span></p>
<p>Tạo ra một thẻ form đóng trong trường hợp đã sử dụng $form-&gt;create.</p>
<h6>Ví dụ:</h6>
<pre name="code" class="php">&lt;?php echo $form-&gt;create(); ?&gt;

&lt;!-- Form elements go here --&gt;

&lt;?php echo $form-&gt;end(); ?&gt;
hoặc:
&lt;?php echo $form-&gt;end('Finish'); ?&gt;

//Dữ liệu trả về html

&lt;div class="submit"&gt;
&lt;input type="submit" value="Finish" /&gt;
&lt;/div&gt;
&lt;/form&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://i-php.net/2009/01/cakephp-12-m%e1%bb%99t-s%e1%bb%91-ham-th%c6%b0%e1%bb%9dng-dung-ph%e1%ba%a7n-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cakephp 1.2 final ra lò</title>
		<link>http://i-php.net/2008/12/cakephp-12-final-ra-lo/</link>
		<comments>http://i-php.net/2008/12/cakephp-12-final-ra-lo/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 07:17:00 +0000</pubDate>
		<dc:creator>ngocbd</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://i-php.net/?p=247</guid>
		<description><![CDATA[Cakephp 1.2 final ra lò
Today, the history of the CakePHP grows stronger. December 25, 2008 will be remembered as one of the most important points in this history. After exactly 2 yrs from the first development release, we can happily say we have the most stable and powerful web framework available. Please enjoy our big [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;">Cakephp 1.2 final ra lò</p>
<p>Today, the history of the CakePHP grows stronger. December 25, 2008 will be remembered as one of the most important points in this history. After exactly 2 yrs from the first development release, we can happily say we have the most stable and powerful web framework available. Please enjoy our big present to you, CakePHP 1.2 stable [1]. For this release, we have removed the test files from the build, and created a tag in SVN.</p>
<p>Through the last two years, we have been blessed by a dedicated, talented, and opinionated community[2]. We have shared disagreements[3] and triumphs. We have won popularity contests[4] and been hated on. We have seen CakePHP grow into a truly international community[5]. All of these events have generated an immense amount of passion for CakePHP.</p>
<p>No one is more passionate about CakePHP than the developers[6] who close tickets and fix bugs. We started out two years ago with a small team that dedicated countless hours to implementing new features into 1.2 and maintaining 1.1 stable. This team ensured the integrity of code and vision of the project. When we needed to grow, we found members of the community who showed the same amount of dedication and passion for CakePHP. And with the launch of CakeBOOK, on <a href="http://book.cakephp.org/">http://book.cakephp.org</a>, we have seen the dedication and passion further extend to all the contributors and translators[7] of the fantastic documentation that makes learning about the power of CakePHP a bit easier.</p>
<p>We have seen CakePHP adopted by large projects[8] and the growth of dedicated service companies[9]. We have held a workshop[10] to spread the knowledge and passion of CakePHP. And ultimately, we implemented a huge list of features&#8230;</p>
<ul>
<li>- Tests!</li>
<li>
<ul>
<li>- All classes are test-covered, with good code coverage</li>
<li>- Test suite now integrated into the framework</li>
<li>- test generation</li>
<li>- support for coverage analysis</li>
</ul>
</li>
<li>- Command-line infrastructure</li>
<li>
<ul>
<li>- with more shell scripts and ability to write custom ones easily</li>
</ul>
</li>
<li>- Plugin architecture</li>
<li>
<ul>
<li>- Plugins are now distributable as packaged collections of files</li>
<li>- Can be loaded from your main app with a dot syntax</li>
</ul>
</li>
<li>- Internationalization and Localization support</li>
<li>
<ul>
<li>- i18n and l10n classes</li>
<li>- Support for unicode strings</li>
</ul>
</li>
<li>- Auth component</li>
<li>
<ul>
<li>- automatically handles sessions for authenticated users</li>
<li>- ties into ACL for automatic denial of protected content or actions</li>
</ul>
</li>
<li>- Email component</li>
<li>
<ul>
<li>- for generation of text and html email</li>
</ul>
</li>
<li>- Security component</li>
<li>
<ul>
<li>- HTTP auth support, great for web services</li>
<li>- CSRF protection</li>
</ul>
</li>
<li>- Cookie component</li>
<li>
<ul>
<li>-  for secure cookie handling</li>
</ul>
</li>
<li>- Custom model finders</li>
<li>
<ul>
<li>- simplified syntax</li>
</ul>
</li>
<li>- powerful and extensible</li>
</ul>
<ul>
<li>- Join models</li>
<li>
<ul>
<li>- for modeling and accessing HABTM join tables</li>
</ul>
</li>
<li>- Behaviors, new way to extend models</li>
<li>
<ul>
<li>- Supports &#8220;mixing in&#8221; new functionality</li>
</ul>
</li>
<li>- Containable behavior</li>
<li>
<ul>
<li>-  simplified query optimization</li>
</ul>
</li>
<li>- Validation system extended</li>
<li>
<ul>
<li>-  with new Validation class, lots of rules</li>
</ul>
</li>
<li>- multiple rules and messages</li>
</ul>
<ul>
<li>- Database drivers</li>
<li>
<ul>
<li>- support for many more databases including DB2 and Oracle</li>
</ul>
</li>
<li>- Caching</li>
<li>
<ul>
<li>- Adapter-driven caching, with support for APC/XCache/Memcache</li>
</ul>
</li>
<li>- Set class,</li>
<li>
<ul>
<li>- for magical array hacking</li>
</ul>
</li>
<li>- Socket and HttpSocket classes</li>
<li>
<ul>
<li>-  for dealing with remote data and services</li>
</ul>
</li>
<li>- Debugger class, for detailed introspection of errors</li>
<li>
<ul>
<li>- Get stack traces anywhere in your code</li>
<li>- Introspected help on errors, with context information</li>
</ul>
</li>
<li>- Pagination</li>
<li>
<ul>
<li>- one of the first additions to the new version</li>
<li>- one of the simplest systems known</li>
</ul>
</li>
<li>- Proper Routing</li>
<li>
<ul>
<li>- mapResources() method for exposing controllers via REST</li>
<li>- Reverse routing support</li>
<li>- Named arguments</li>
<li>- Magic variables for common regexes</li>
<li>- Support for file extensions with custom content type mappings</li>
</ul>
</li>
<li>- View stuff</li>
<li>
<ul>
<li>- Separate templates for different content types</li>
<li>- automatic switching with RequestHandler</li>
<li>- New helper callbacks</li>
<li>- renderElement() replaced with element(), added built-in caching support</li>
</ul>
</li>
<li>- FormHelper</li>
<li>
<ul>
<li>- All form-related methods moved here</li>
<li>- New dot notation</li>
<li>- Support for associations and multiple records</li>
<li>- Huge automation and introspection support; form creation requires very little code</li>
</ul>
</li>
<li>- Configure and App classes</li>
<li>
<ul>
<li>- for configuration and loading</li>
</ul>
</li>
<li>- Replaces defines and global functions</li>
</ul>
<p>We hope that was a fun read. The changes since 1.1 have been dramatic, but to us this was the minimum set of features needed to a truly powerful framework and realize our vision for maintainability, flexibility, and extensibility.</p>
<p>Almost as dramatic as the feature set, was the growth of the community over this time, especially with its adoption of testing. We are proud of the fact that Cake is one of the most test covered frameworks. Test coverage allows us to fix more bugs and produce the most stable framework available. We believe that a feature is not truly a feature if there is even one known bug. With that in mind, each release comes with the expectation that no bugs are known at the time.</p>
<p>Many of you may remember the first release of 1.2. Back on Dec 25, 2006 we released at revision 4206. Many features had not been implemented or finalized, but we had a taste of what was to come. With this release at [7958], we have come a long way. But possibly the most exciting aspect of being where we are on Dec 25, 2008, is what we expect to see in the future.</p>
<p>CakePHP helps build amazingly powerful applications. We have a running list of examples[11]. Many of these applications were built with earlier versions of 1.2. With the release of CakePHP 1.2 stable, we expect these applications to enjoy a long history, just like the CakePHP project itself.</p>
<p>If you have made it this far, we would like to pass on a few extra goodies we have been working on. First is the all new Cake 1.2 cheatsheet. The old CakeSheet has proved to be a simple, quick reference to some of the power of CakePHP. This new version is the start of several more to come. Second, the gorgeous DebugKit plugin. This plugin helps you develop your application faster by providing quick, easy access to a lot of valuable debugging information. DebugKit also provides and excellent example of how you can build plugins to extend the functionality of you application. Finally, for all the TextMate users out there we have an updated CakePHP TextMate bundle. Joel Perras has put in a great amount of work and coding CakePHP in TextMate just got a lot easier thanks to him. For all these great resources and more, head on over to the downloads[12] page.</p>
<p>We hope you enjoy the big present and the few goodies. Have a great holiday season.<br />
- Gwoo, Nate, PhpNut and the rest of the CakePHP team</p>
<p>download bản final ở <a href="http://cakeforge.org/frs/?group_id=23&amp;release_id=433">đây </a></p>
]]></content:encoded>
			<wfw:commentRss>http://i-php.net/2008/12/cakephp-12-final-ra-lo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Sử dụng Helper TinyMCE trong Cakephp 1.2</title>
		<link>http://i-php.net/2008/11/s%e1%bb%ad-d%e1%bb%a5ng-helper-tinymce-trong-cakephp-12/</link>
		<comments>http://i-php.net/2008/11/s%e1%bb%ad-d%e1%bb%a5ng-helper-tinymce-trong-cakephp-12/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 11:06:12 +0000</pubDate>
		<dc:creator>hoanbn</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[tinyMCE]]></category>

		<guid isPermaLink="false">http://i-php.net/?p=192</guid>
		<description><![CDATA[Sử dụng tinyMCE trong cakephp đã được chaubl hướng dẫn ở bài trước (http://i-php.net/2008/10/cakephp-tinymce-ibrowser/). Tuy nhiên ở đây tôi sẽ hướng dẫn các bạn sử dụng TinyMCE thông qua một helper TinyMCE của cakephp.
Cài đặt TinyMCE
Bạn có thể download bản tinymce mới nhất tại địa chỉ  http://tinymce.moxiecode.com/download.php và copy thư mục /tinymce/jscripts/tiny_mce  vào [...]]]></description>
			<content:encoded><![CDATA[<p>Sử dụng tinyMCE trong cakephp đã được chaubl hướng dẫn ở bài trước (<a title="Sử dụng tinyMCE" href="http://i-php.net/2008/10/cakephp-tinymce-ibrowser/">http://i-php.net/2008/10/cakephp-tinymce-ibrowser/</a>). Tuy nhiên ở đây tôi sẽ hướng dẫn các bạn sử dụng TinyMCE thông qua một helper TinyMCE của cakephp.</p>
<h6>Cài đặt TinyMCE</h6>
<p>Bạn có thể download bản tinymce mới nhất tại địa chỉ  <a href="http://tinymce.moxiecode.com/download.php">http://tinymce.moxiecode.com/download.php</a> và copy thư mục /tinymce/jscripts/tiny_mce  vào thư mục /app/webroot/js.</p>
<h6>Tạo ra helper TinyMCE</h6>
<p>Bạn tạo một helper tinymce theo đường dẫn : app/views/helpers/tinymce.php . Bạn vào  <a title="Helper TinyMCE" href="http://i-php.net/2008/11/helper-tinymce/"><span id="sample-permalink">http://i-php.net/2008/11/<span id="editable-post-name-full">helper-tinymce</span>/</span></a> để lấy source nhé.</p>
<h6>Sử dụng trong controller</h6>
<p>Bạn khai báo sử dụng helper tinymce trong controller như sau:</p>
<pre class="php" name="code">var $helpers = Array('Form', 'Tinymce'); </pre>
<h6>Sử dụng trong view</h6>
<p>Sử dụng tinymce hepler trong view sẽ tương tự như sử dụng các hepler form, html trong cakephp. Để khai báo một ô textarea sử dụng tinymce bạn chỉ việc thêm đoạn code sau vào:</p>
<pre class="php">&lt;?php echo $tinymce-&gt;input('content') ?&gt;</pre>
<p>Sau đây là 2 trang view code mẫu để các bạn tham khảo:</p>
<p>đoạn code 1:</p>
<pre class="php" name="code">&lt;div class="form-container"&gt;
  &lt;?php echo $form-&gt;create('Page'); ?&gt;
    &lt;fieldset&gt;
      &lt;legend&gt;Page&lt;/legend&gt;
      &lt;?php
        echo $form-&gt;input('title');
        echo $tinymce-&gt;input('content');
      ?&gt;
    &lt;/fieldset&gt;
  &lt;?php echo $form-&gt;end('Save'); ?&gt;
&lt;/div&gt;</pre>
<p>đoạn code 2: ở đây sẽ cấu hình một vài option của tinyMCE</p>
<pre class="php" name="code">&lt;div class="form-container"&gt;
  &lt;?php echo $form-&gt;create('Page'); ?&gt;
    &lt;fieldset&gt;
      &lt;legend&gt;Page&lt;/legend&gt;
      &lt;?php
        echo $form-&gt;input('title');
        echo $tinymce-&gt;input('content', null, array(
          'theme'                             =&gt; 'advanced',
          'theme_advanced_toolbar_location'   =&gt; 'top',
          'theme_advanced_toolbar_align'      =&gt; 'left',
          'theme_advanced_statusbar_location' =&gt; 'bottom',
        ));
      ?&gt;
    &lt;/fieldset&gt;
  &lt;?php echo $form-&gt;end('Save'); ?&gt;
&lt;/div&gt;</pre>
<p>Sự tiện ích khi sử dụng helper TinyMCE này là các bạn có thể tuỳ ý chọn ô textarea nào sử dụng tinyMCE cũng được, ta có thể tuỳ ý cấu hình từng ô nhập textarea cho phù hợp với ý đồ của mình. Good luck.</p>
]]></content:encoded>
			<wfw:commentRss>http://i-php.net/2008/11/s%e1%bb%ad-d%e1%bb%a5ng-helper-tinymce-trong-cakephp-12/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Helper TinyMCE</title>
		<link>http://i-php.net/2008/11/helper-tinymce/</link>
		<comments>http://i-php.net/2008/11/helper-tinymce/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 09:46:27 +0000</pubDate>
		<dc:creator>hoanbn</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tinyMCE]]></category>

		<guid isPermaLink="false">http://i-php.net/?p=180</guid>
		<description><![CDATA[Bạn có thể tham khảo hướng dẫn sử dụng tại: http://i-php.net/2008/11/sử-dụng-helper-tinymce-trong-cakephp-12/
Tạo file app/views/helpers/tinymce.php.
&#60;?php
class TinyMceHelper extends AppHelper
{

    // Take advantage of other helpers
    var $helpers = array('Javascript', 'Form');
    // Check if the tiny_mce.js file has been added or not
    var $_script = false;

  [...]]]></description>
			<content:encoded><![CDATA[<p>Bạn có thể tham khảo hướng dẫn sử dụng tại: <a title="Hướng dẫn sử dụng helper tinyMCE" href="http://i-php.net/2008/11/s%E1%BB%AD-d%E1%BB%A5ng-helper-tinymce-trong-cakephp-12/">http://i-php.net/2008/11/sử-dụng-helper-tinymce-trong-cakephp-12/</a></p>
<p>Tạo file app/views/helpers/tinymce.php.</p>
<pre class="php" name="code">&lt;?php
class TinyMceHelper extends AppHelper
{

    // Take advantage of other helpers
    var $helpers = array('Javascript', 'Form');
    // Check if the tiny_mce.js file has been added or not
    var $_script = false;

    /**
     * Adds the tiny_mce.js file and constructs the options
     *
     * @param string $fieldName Name of a field, like this "Modelname.fieldname", "Modelname/fieldname" is deprecated
     * @param array $tinyoptions Array of TinyMCE attributes for this textarea
     * @return string JavaScript code to initialise the TinyMCE area
     */
    function _build($fieldName, $tinyoptions = array())
    {
        if(!$this-&gt;_script)
        {
            // We don't want to add this every time, it's only needed once
            $this-&gt;_script = true;
            $this-&gt;Javascript-&gt;link('tiny_mce/tiny_mce.js', false);
        }
        // Ties the options to the field
        $tinyoptions['mode'] = 'exact';
        $tinyoptions['elements'] = $this-&gt;domId($fieldName);
        return $this-&gt;Javascript-&gt;codeBlock('tinyMCE.init(' . $this-&gt;Javascript-&gt;object($tinyoptions) . ');');
    }

    /**
     * Creates a TinyMCE textarea.
     *
     * @param string $fieldName Name of a field, like this "Modelname.fieldname", "Modelname/fieldname" is deprecated
     * @param array $options Array of HTML attributes.
     * @param array $tinyoptions Array of TinyMCE attributes for this textarea
     * @return string An HTML textarea element with TinyMCE
     */
    function textarea($fieldName, $options = array(), $tinyoptions = array(), $preset = null)
    {
        // If a preset is defined
        if(!empty($preset))
        {
            $preset_options = $this-&gt;preset($preset);

            // If $preset_options &amp;&amp; $tinyoptions are an array
            if(is_array($preset_options) &amp;&amp; is_array($tinyoptions))
            {
                $tinyoptions = array_merge($preset_options, $tinyoptions);
            }
            else
            {
                $tinyoptions = $preset_options;
            }
        }

        return $this-&gt;Form-&gt;textarea($fieldName, $options) . $this-&gt;_build($fieldName, $tinyoptions);
    }

    /**
     * Creates a TinyMCE textarea.
     *
     * @param string $fieldName Name of a field, like this "Modelname.fieldname", "Modelname/fieldname" is deprecated
     * @param array $options Array of HTML attributes.
     * @param array $tinyoptions Array of TinyMCE attributes for this textarea
     * @return string An HTML textarea element with TinyMCE
     */
    function input($fieldName, $options = array(), $tinyoptions = array(), $preset = null)
    {
        // If a preset is defined
        if(!empty($preset))
        {
            $preset_options = $this-&gt;preset($preset);

            // If $preset_options &amp;&amp; $tinyoptions are an array
            if(is_array($preset_options) &amp;&amp; is_array($tinyoptions))
            {
                $tinyoptions = array_merge($preset_options, $tinyoptions);
            }
            else
            {
                $tinyoptions = $preset_options;
            }
        }

        $options['type'] = 'textarea';
        return $this-&gt;Form-&gt;input($fieldName, $options) . $this-&gt;_build($fieldName, $tinyoptions);
    }

    /**
     * Creates a preset for TinyOptions
     *
     * @param string $name
     * @return array
     */
    private function preset($name)
    {
        // Full Feature
        if($name == 'full')
        {
            return array(
                'theme' =&gt; 'advanced',
                'plugins' =&gt; 'safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template',
                'theme_advanced_buttons1' =&gt; 'save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect',
                'theme_advanced_buttons2' =&gt; 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor',
                'theme_advanced_buttons3' =&gt; 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen',
                'theme_advanced_buttons4' =&gt; 'insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak',
                'theme_advanced_toolbar_location' =&gt; 'top',
                'theme_advanced_toolbar_align' =&gt; 'left',
                'theme_advanced_statusbar_location' =&gt; 'bottom',
                'theme_advanced_resizing' =&gt; true,
                'theme_advanced_resize_horizontal' =&gt; false,
                'convert_fonts_to_spans' =&gt; true
            );
        }

        // Basic
        if($name == 'basic')
        {
            return array(
                'theme' =&gt; 'advanced',
                'theme_advanced_toolbar_location' =&gt; 'top',
                'theme_advanced_toolbar_align' =&gt; 'left',
                'theme_advanced_statusbar_location' =&gt; 'bottom',
                'theme_advanced_resizing' =&gt; true,
                'theme_advanced_resize_horizontal' =&gt; false,
                'convert_fonts_to_spans' =&gt; true
            );
        }

        // BBCode
        if($name == 'bbcode')
        {
            return array(
                'theme' =&gt; 'advanced',
                'plugins' =&gt; 'bbcode',
                'theme_advanced_buttons1' =&gt; 'bold,italic,underline,undo,redo,link,unlink,image,forecolor,styleselect,removeformat,cleanup,code',
                'theme_advanced_buttons2' =&gt; '',
                'theme_advanced_buttons3' =&gt; '',
                'theme_advanced_toolbar_location' =&gt; 'top',
                'theme_advanced_toolbar_align' =&gt; 'left',
                'theme_advanced_styles' =&gt; 'Code=codeStyle;Quote=quoteStyle',
                'theme_advanced_statusbar_location' =&gt; 'bottom',
                'theme_advanced_resizing' =&gt; true,
                'theme_advanced_resize_horizontal' =&gt; false,
                'entity_encoding' =&gt; 'raw',
                'add_unload_trigger' =&gt; false,
                'remove_linebreaks' =&gt; false,
                'inline_styles' =&gt; false
            );
        }

        return null;
    }
}
?&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://i-php.net/2008/11/helper-tinymce/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sử dụng Element trong Cakephp</title>
		<link>http://i-php.net/2008/11/s%e1%bb%ad-d%e1%bb%a5ng-element-trong-cakephp/</link>
		<comments>http://i-php.net/2008/11/s%e1%bb%ad-d%e1%bb%a5ng-element-trong-cakephp/#comments</comments>
		<pubDate>Fri, 28 Nov 2008 04:29:31 +0000</pubDate>
		<dc:creator>hoanbn</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[element]]></category>

		<guid isPermaLink="false">http://i-php.net/?p=168</guid>
		<description><![CDATA[Element thật sự là cần thiết khi chúng ta muốn sử dụng một đoạn code nhiều lần trong view hoặc layout. Thay vì chúng ta viết đi viết lại nhiều lần trong các view với một đoạn source giống nhau thì chúng ta chỉ việc viết một lần trong element và sử dụng chúng nhiều [...]]]></description>
			<content:encoded><![CDATA[<p>Element thật sự là cần thiết khi chúng ta muốn sử dụng một đoạn code nhiều lần trong view hoặc layout. Thay vì chúng ta viết đi viết lại nhiều lần trong các view với một đoạn source giống nhau thì chúng ta chỉ việc viết một lần trong element và sử dụng chúng nhiều lần. Việc tạo một element sử dụng nhiều lần trong cakephp sẽ rất dễ dàng khi chúng ta sử dụng hàm requestAction. Sau đây tôi sẽ hướng dẫn các bạn sử dụng element mà dùng được trong cả cakephp 1.1 lẫn cakephp 1.2.</p>
<p>Đầu tiên chúng ta sẽ tạo ra controller Post như sau:</p>
<blockquote><p><code><span style="color: #000000;"> <span style="color: #0000bb;">&lt;?php<br />
</span><span style="color: #007700;">class </span><span style="color: #0000bb;">PostsController </span><span style="color: #007700;">extends </span><span style="color: #0000bb;">AppController </span><span style="color: #007700;">{<br />
var </span><span style="color: #0000bb;">$name </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'Posts'</span><span style="color: #007700;">;</span></span></code></p>
<p>function <span style="color: #0000bb;">index</span><span style="color: #007700;">() {<br />
</span><span style="color: #0000bb;">$posts </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">Post</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">findAll</span><span style="color: #007700;">(); </span></p>
<p><code><span style="color: #000000;"><span style="color: #007700;"> //Kiểm tra xem hàm requestAction có được gọi hay không<br />
if(isset(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">params</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'requested'</span><span style="color: #007700;">])) {<br />
return </span><span style="color: #0000bb;">$posts</span><span style="color: #007700;">;<br />
}<br />
</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">set</span><span style="color: #007700;">(</span><span style="color: #dd0000;">'posts'</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$posts</span><span style="color: #007700;">);<br />
}<br />
}<br />
</span><span style="color: #0000bb;">?&gt;</span></span></code></p></blockquote>
<p>Trong hàm index trên ta sẽ kiểm tra xem nó có được gọi bằng hàm requestAciton hay không. Nếu có thì hàm sẽ trả về một mảng $posts. Bây giờ chúng ta sẽ tạo ra một element post.ctp (hay post.thml trong cakephp 1.1) có đường dẫn như sau:  /app/elements/posts.ctp.</p>
<blockquote><p><span style="color: #0000ff;">&lt;?php</span></p>
<p><code><span style="color: #000000;"><span style="color: #0000ff;">$posts = $this-&gt;requestAction('<span style="color: #ff0000;">posts/index'</span>); </span><br />
<span style="color: #0000ff;"><span style="color: #008000;">foreach</span>($posts as $post):<br />
<span style="color: #008000;">echo</span> $post['<span style="color: #ff0000;">Post</span>']['<span style="color: #ff0000;">title</span>'];<br />
<span style="color: #008000;">endforeach</span>; </span></span></code></p>
<p><span style="color: #0000ff;">?&gt;</span></p></blockquote>
<p>Để sử dụng element trên, chúng ta chỉ việc thêm đoạn code sau vào các view hoặc layout là xong.</p>
<blockquote><p><code><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php </span><span style="color: #007700;">echo </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">renderElement</span><span style="color: #007700;">(</span><span style="color: #dd0000;">'posts'</span><span style="color: #007700;">);</span><span style="color: #0000bb;">?&gt; </span></span></code></p></blockquote>
<p>Vậy là xong.</p>
]]></content:encoded>
			<wfw:commentRss>http://i-php.net/2008/11/s%e1%bb%ad-d%e1%bb%a5ng-element-trong-cakephp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
