<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>想当摄影师的程序员 &#187; dynamic</title>
	<atom:link href="http://blog.iworm.net/tag/dynamic/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.iworm.net</link>
	<description>争取不惑之年成为一名自由摄影师</description>
	<lastBuildDate>Tue, 10 Jan 2012 04:09:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-nd/2.5/cn/</creativeCommons:license>
		<item>
		<title>How to: Implement Dynamic Route in CakePHP</title>
		<link>http://blog.iworm.net/2009/12/17/how-to-implement-dynamic-route-in-cakephp/</link>
		<comments>http://blog.iworm.net/2009/12/17/how-to-implement-dynamic-route-in-cakephp/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 16:53:30 +0000</pubDate>
		<dc:creator>iworm</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[route]]></category>

		<guid isPermaLink="false">http://blog.iworm.net/?p=668</guid>
		<description><![CDATA[I&#8217;ve found a method to implement Dynamic Route in CakePHP. If we develop a CMS program using CakePHP, we often let people to define their menu code. The menu code basically is the URL path. For example: An admin create &#8230; <a href="http://blog.iworm.net/2009/12/17/how-to-implement-dynamic-route-in-cakephp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve found a method to implement Dynamic Route in CakePHP.<br />
If we develop a CMS program using CakePHP, we often let people to define their menu code. The menu code basically is the URL path.<br />
For example: An admin create a menu named &#8216;Milk Product&#8217;, and give this menu a code &#8216;product&#8217;.<br />
He wish to access this menu via /product</p>
<p>So we can add a static route in routes.php<br />
Router::connect(&#8216;/product&#8217;, array(&#8216;controller&#8217; => &#8216;product&#8217;, &#8216;action&#8217; => &#8216;index&#8217;));</p>
<p>But one day, he want to change the menu code from &#8216;product&#8217; to &#8216;milkproduct&#8217;? What to do then? Ask the site admin to edit the routes.php? Of course not.</p>
<p>We can use the code below to resolve this problem.</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: #000088;">$menus</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
Cache<span style="color: #339933;">::</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'routemenus'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menus</span> <span style="color: #339933;">=</span> Cache<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'routemenus'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'load from db'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$menusModel</span> <span style="color: #339933;">=</span> ClassRegistry<span style="color: #339933;">::</span><span style="color: #004000;">init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Menu'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$menus</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$menusModel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'parent_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Cache<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'routemenus'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$menus</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menus</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$menuitem</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    Router<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$menuitem</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Menu'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'code'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/:action/*'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$menuitem</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'MenuType'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'code'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Router<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'homepage'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We have 2 menu related tables: menus and menu_types.<br />
Their structure as belows:</p>
<pre>menus
    id
    menu_type_id
    parent_id
    lft
    rght
    code
    name
    created
    modified

menu_types
    id
    code
    name
    created
    modified</pre>
<p>The code in menu_types means the controller name, and the code in menus means the path.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.iworm.net%2F2009%2F12%2F17%2Fhow-to-implement-dynamic-route-in-cakephp%2F&amp;title=How%20to%3A%20Implement%20Dynamic%20Route%20in%20CakePHP" id="wpa2a_2"><img src="http://blog.iworm.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.iworm.net/2009/12/17/how-to-implement-dynamic-route-in-cakephp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-nd/2.5/cn/</creativeCommons:license>
	</item>
	</channel>
</rss>

