<?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>Promet CakePHP Source &#187; route</title>
	<atom:link href="http://cakephp.prometsupport.com/tag/route/feed/" rel="self" type="application/rss+xml" />
	<link>http://cakephp.prometsupport.com</link>
	<description></description>
	<lastBuildDate>Mon, 23 Feb 2009 08:03:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Tip: use CakePHP Route::url() for your URLs</title>
		<link>http://cakephp.prometsupport.com/2008/tip-use-cakephp-routeurl-for-your-urls/</link>
		<comments>http://cakephp.prometsupport.com/2008/tip-use-cakephp-routeurl-for-your-urls/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 07:29:24 +0000</pubDate>
		<dc:creator>rachel</dc:creator>
				<category><![CDATA[CakePHP 1.2]]></category>
		<category><![CDATA[Tips & Tutorials]]></category>
		<category><![CDATA[route]]></category>
		<category><![CDATA[router]]></category>

		<guid isPermaLink="false">http://cakephp.prometsupport.com/2008/07/11/tip-use-cakephp-routeurl-for-your-urls/</guid>
		<description><![CDATA[Routing is one of the amazing features of CakePHP because it makes the urls beautiful. We use this most of the time and last time, I blogged about the helper to shortcut ones typing when using the HTML helper. Overtime, I realize how helpful it can be. Read on. Always use array when passing parameters [...]]]></description>
			<content:encoded><![CDATA[<p>Routing is one of the amazing features of CakePHP because it makes the urls beautiful. We use this most of the time and last time, I <a href="/2008/05/28/convenient-html-link/">blogged</a> about the helper to shortcut ones typing when using the HTML helper. Overtime, I realize how helpful it can be. Read on.</p>
<p>Always use array when passing parameters when creating a link or using Router::url(), it will save you time in the future. Why? Consider this scenario:</p>
<p>You created a controller <i>/users/register</i> and in your code and you added this<br />
<code lang="php"><br />
<?php echo $html->link('User Registration', '/users/register')?><br />
</code></p>
<p>Then your group or your boss decided to use a better name <i>/register</i>. Your initial thought would be to replace all those texts to those new ones, isn&#8217;t it? There is a better approach. Try the ffg:</p>
<p>In /config/router.php<br />
<code lang="php"><br />
<?php Router::connect('/register',	array('controller'=>'users','action'=>'register'));  ?><br />
</code></p>
<p>In your view:<br />
<code lang="php"><br />
<?php echo $html->link('Registration', array('controller'=>'users','action'=>'register'))?><br />
</code></p>
<p>the HTML helper uses Router::url() in its link function and the Router class magically converts array parameters to what you stated in your config.  In writing urls, we have to be consistent because it can be bad for SEO when you have too many urls pointing to the same location.</p>
<p>Although the above works beautifully but it can be a tedious job. I wrote not long ago about the <a href="/2008/05/28/convenient-html-link/">html helper</a> that could make this approach easier so you could write the ffg in your view. </p>
<p><code lang="php"><br />
<?php echo $html->link('Registration', url( 'users', 'register', null, 'teacher' ))?><br />
</code></p>
<p>It saves a few characters!</p>
<p>From the last post, I refactored it  and placed it in the bootstrap so I can call it anywhere (got that idea from the google groups). </p>
<p>Here it is:<br />
<code lang="php"><br />
    function url() {<br />
        $args = func_get_args();<br />
        $count = func_num_args();</p>
<p>        if ( $count == 1 ) {<br />
            if ( is_array( $args[0] ) ) {<br />
                $args = $args[0];<br />
                $count = count($args);<br />
            } else {<br />
                return $args[0];<br />
            }<br />
        }</p>
<p>        $short_keys = array(<br />
            0 => 'controller',<br />
            1 => 'action',<br />
            2 => 'plugin',<br />
            'c' => 'controller',<br />
            'a' => 'action',<br />
            'p' => 'plugin',<br />
        );</p>
<p>        foreach ( $short_keys as $short_name => $long_name ) {</p>
<p>            if ( isset( $args[$short_name] ) ) {<br />
                $args[$long_name] = $args[$short_name];<br />
                unset( $args[$short_name] );<br />
            }</p>
<p>        }</p>
<p>        // Need to explicitly assign plugin key<br />
        if ( !isset( $args['plugin'] ) ) $args['plugin'] = null;<br />
        if ( !isset( $args['action'] ) ) $args['action'] = 'index';<br />
        if ( !isset( $args['admin'] ) ) $args['admin'] = false;</p>
<p>        $routing = Configure::read('Routing.admin');<br />
        if ( preg_match( "/^{$routing}_/", $args['action'] ) &#038;&#038; $routing ) {<br />
            $args['admin'] = true;<br />
            $args['action'] = substr( $args['action'], strlen($routing) + 1 );<br />
        }</p>
<p>        return $args;<br />
    }<br />
</code></p>
<p>The function above could take an array OR any number of string parameters.<br />
<code lang="php"><br />
url ( 'controller', 'action', 'plugin_name', ...[more params here] )<br />
url ( array( 'c' => 'controller', 'a' => 'action', 'p' => 'plugin' ) )<br />
</code></p>
<p>The first one is strict for the first 3 parameters. The latter can be written in any order as you like. I also added support for <a href="http://www.sanisoft.com/blog/2008/04/04/the-prefix-automagic-in-cakephp-routing/">prefix routing</a> such as admin_*. So you can write:<br />
<code lang="php"><br />
url ( 'controller', 'admin_action', 'plugin_name', ...[more params here] )<br />
url ( array( 'c' => 'controller', 'a' => 'admin_action', 'p' => 'plugin' ) )<br />
</code></p>
<p><i><a href="http://api.cakephp.org/class_router.html#73cfb34ccc4dae2932041a130711ac05">Router::parse()</a></i> can also be used instead of <i>url()</i> function but the latter is faster. </p>
<p>That&#8217;s it. Happy Baking!</p>
]]></content:encoded>
			<wfw:commentRss>http://cakephp.prometsupport.com/2008/tip-use-cakephp-routeurl-for-your-urls/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Convenient $html-&gt;link()</title>
		<link>http://cakephp.prometsupport.com/2008/convenient-html-link/</link>
		<comments>http://cakephp.prometsupport.com/2008/convenient-html-link/#comments</comments>
		<pubDate>Wed, 28 May 2008 11:41:48 +0000</pubDate>
		<dc:creator>rachel</dc:creator>
				<category><![CDATA[CakePHP 1.2]]></category>
		<category><![CDATA[Helpers]]></category>
		<category><![CDATA[html helper]]></category>
		<category><![CDATA[route]]></category>

		<guid isPermaLink="false">http://cakephp.prometsupport.com/2008/05/28/convenient-html-link/</guid>
		<description><![CDATA[I made this quick helper that extends Html Helper. While creating my very first plugin for cakephp 1.2, I decided to use Route so that I can save some space and time typing long controller names. Here is the sample code in my views: echo $html->link( 'Link Test', array( 'controller' => 'long_controller_name', 'actions' => 'index', [...]]]></description>
			<content:encoded><![CDATA[<p>I made this quick helper that extends Html Helper. While creating my very first plugin for cakephp 1.2, I decided to use Route so that I can save some space and time typing long controller names. Here is the sample code in my views:</p>
<p><code lang="php"><br />
echo $html->link( 'Link Test',<br />
                  array(<br />
                    'controller' => 'long_controller_name',<br />
                    'actions' => 'index',<br />
                    'plugin' => 'long_plugin_name'<br />
                  )<br />
                );</code></p>
<p>I am a lazy typist and so I wanted to be able to use shortcut names for the array keys such as the following:</p>
<pre>
c = controller
a = action
p = plugin

OR use numbers

0 = controller
1 = action
2 = plugin
</pre>
<p>I couldn&#8217;t find in the <a href="http://bakery.cakephp.org">bakery</a> any solution so I decided to create my own Html helper. Here is how you can install it:</p>
<p>First, add <a href='http://cakephp.prometsupport.com/wp-content/uploads/2008/05/xhtmlphphelper.txt' title='XHTML Helper'>this file</a> to your helpers folder located in<br />
<code>/cakedir/app/views/helpers/</code><br />
<a href='http://cakephp.prometsupport.com/wp-content/uploads/2008/05/xhtmlphphelper.txt' title='XHTML Helper'>Download XHTML Helper</a></p>
<p>Then, in your controller file, include the <strong>Xhtml</strong> to your helpers list. Example</p>
<p><code lang="php"><br />
class MyController extends AppController {<br />
  ...<br />
  var $helpers = array( 'Xhtml' );<br />
  ...<br />
}<br />
</code></p>
<p>That&#8217;s it. You can use it by doing the following:</p>
<p><code lang="php"><br />
echo $xhtml->link( 'Link Test',<br />
                    array(<br />
                        'c' => 'long_controller_name',<br />
                        'a' => 'index',<br />
                        'p' => 'long_plugin_name'<br />
                    )<br />
                 );</code></p>
<p>OR</p>
<p><code lang="php"><br />
echo $xhtml->link( 'Link Test',<br />
                    array( 'long_controller_name',<br />
	                       'index',<br />
	                       'long_plugin_name'<br />
	                )<br />
                 );</code></p>
<p>Take note of using <strong>xhtml</strong> instead of <strong>html</strong>. I hope I don&#8217;t mislead anyone for using the name XHTML <img src='http://cakephp.prometsupport.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> . I just want an extended HTML helper.</p>
<div class="notice">
<strong>[Update]:</strong> update code <a href="/2008/07/11/tip-use-cakephp-routeurl-for-your-urls/">here</a>
</div>
]]></content:encoded>
			<wfw:commentRss>http://cakephp.prometsupport.com/2008/convenient-html-link/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
