<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
<title>Fun with ones and zeros - laravel</title>
<description><![CDATA[Barry's notes on computer software and hardware]]></description>
<link>/blog/tags/laravel</link>
<lastBuildDate>Fri, 17 Apr 2026 13:50:16 -0700</lastBuildDate>
<item>
<title>Named routes in Laravel 4</title>
<link>/blog/entries/named-routes-laravel-4</link>
<pubDate>Sat, 26 Jan 2013 14:51:00 -0800</pubDate>
<author>bp@barryp.org (Barry Pederson)</author>
<description><![CDATA[<body><p>I've been doing some work with PHP &amp; Laravel 4, and had an idea to make naming routes a bit nicer syntactically.  Currently, to name a route you wrap the 2nd parameter in an array and add an item with an 'as' key, as in:</p>
<div class="source"><pre><span></span><span class="x">Route::get('user/profile', array('as' =&gt; 'profile', function(){// code here..}));</span>
</pre></div>
<p>I think it'd be more natural to set the name using a method on the route (similar to adding a where() condition) , something like:</p>
<div class="source"><pre><span></span><span class="x">Route::get('user/profile', function(){// code here..})</span>
<span class="x">    -&gt;named('profile');</span>
</pre></div>
<p>After a bit of poking around, I found this could be done fairly easily with a couple
small additions to Route and Router to allow for renaming a route:</p>
<div class="source"><pre><span></span><span class="gd">--- a/vendor/laravel/framework/src/Illuminate/Routing/Route.php Fri Jan 25 15:35:04 2013 -0600</span>
<span class="gi">+++ b/vendor/laravel/framework/src/Illuminate/Routing/Route.php Sat Jan 26 16:37:08 2013 -0600</span>
<span class="gu">@@ -411,4 +411,16 @@</span>
                return $this;
        }

<span class="gd">-}</span>
<span class="gi">+    /**</span>
<span class="gi">+     * Change the name for this route.</span>
<span class="gi">+     *</span>
<span class="gi">+     * @param string $name New Name</span>
<span class="gi">+     * @return Illuminate\Routing\Route</span>
<span class="gi">+     */</span>
<span class="gi">+    public function named($name)</span>
<span class="gi">+    {</span>
<span class="gi">+        $this-&gt;router-&gt;rename($this, $name);</span>
<span class="gi">+</span>
<span class="gi">+        return $this;</span>
<span class="gi">+    }</span>
<span class="gi">+}</span>
\ No newline at end of file
<span class="gh">diff -r 50adf81e2f0f vendor/laravel/framework/src/Illuminate/Routing/Router.php</span>
<span class="gd">--- a/vendor/laravel/framework/src/Illuminate/Routing/Router.php        Fri Jan 25 15:35:04 2013 -0600</span>
<span class="gi">+++ b/vendor/laravel/framework/src/Illuminate/Routing/Router.php        Sat Jan 26 16:37:08 2013 -0600</span>
<span class="gu">@@ -1159,4 +1159,21 @@</span>
                $this-&gt;container = $container;
        }

<span class="gi">+    /**</span>
<span class="gi">+     * Change the name of a route</span>
<span class="gi">+     *</span>
<span class="gi">+     * @param $route</span>
<span class="gi">+     * @param $name</span>
<span class="gi">+     * @return void</span>
<span class="gi">+     */</span>
<span class="gi">+    public function rename($route, $name)</span>
<span class="gi">+    {</span>
<span class="gi">+        foreach($this-&gt;routes-&gt;getIterator() as $n =&gt; $r) {</span>
<span class="gi">+            if ($r === $route) {</span>
<span class="gi">+                $this-&gt;routes-&gt;remove($n);</span>
<span class="gi">+                $this-&gt;routes-&gt;add($name, $r);</span>
<span class="gi">+                return;</span>
<span class="gi">+            }</span>
<span class="gi">+        }</span>
<span class="gi">+    }</span>
 }
</pre></div>
<p>It's not the most efficient thing in the world to be iterating over all the previous routes.  A small optimization would be to check the last-added route first, in the example usage that'd be the one you're renaming.  But to do that would require also patching Symfony's <code>RouteCollection</code> class.</p></body>]]></description>
</item>
</channel>
</rss>