<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Fun with ones and zeros - laravel</title>
<subtitle>Barry&#039;s notes on computer software and hardware</subtitle>
<link href="/blog/tags/laravel"></link>
<updated>2026-04-17T13:47:42-07:00</updated>
<id>urn:uuid:d3e8e664-0d8a-c57f-a31a-85289a620f1e</id>
<entry>
<title>Named routes in Laravel 4</title>
<link href="/blog/entries/named-routes-laravel-4"></link>
<id>urn:uuid:ef37bbe4-91c7-1335-c252-959feca6da4a</id>
<updated>2013-01-26T14:51:00-08:00</updated>
<author><name>Barry Pederson</name>
<email>bp@barryp.org</email>
</author>
<content type="html">&lt;body&gt;&lt;p&gt;I&#039;ve been doing some work with PHP &amp;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 &#039;as&#039; key, as in:&lt;/p&gt;
&lt;div class=&quot;source&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;Route::get(&#039;user/profile&#039;, array(&#039;as&#039; =&amp;gt; &#039;profile&#039;, function(){// code here..}));&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I think it&#039;d be more natural to set the name using a method on the route (similar to adding a where() condition) , something like:&lt;/p&gt;
&lt;div class=&quot;source&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;Route::get(&#039;user/profile&#039;, function(){// code here..})&lt;/span&gt;
&lt;span class=&quot;x&quot;&gt;    -&amp;gt;named(&#039;profile&#039;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;div class=&quot;source&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gd&quot;&gt;--- a/vendor/laravel/framework/src/Illuminate/Routing/Route.php Fri Jan 25 15:35:04 2013 -0600&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+++ b/vendor/laravel/framework/src/Illuminate/Routing/Route.php Sat Jan 26 16:37:08 2013 -0600&lt;/span&gt;
&lt;span class=&quot;gu&quot;&gt;@@ -411,4 +411,16 @@&lt;/span&gt;
                return $this;
        }

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

&lt;span class=&quot;gi&quot;&gt;+    /**&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+     * Change the name of a route&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+     *&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+     * @param $route&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+     * @param $name&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+     * @return void&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+     */&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+    public function rename($route, $name)&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+    {&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+        foreach($this-&amp;gt;routes-&amp;gt;getIterator() as $n =&amp;gt; $r) {&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+            if ($r === $route) {&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+                $this-&amp;gt;routes-&amp;gt;remove($n);&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+                $this-&amp;gt;routes-&amp;gt;add($name, $r);&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+                return;&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+            }&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+        }&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+    }&lt;/span&gt;
 }
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It&#039;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&#039;d be the one you&#039;re renaming.  But to do that would require also patching Symfony&#039;s &lt;code&gt;RouteCollection&lt;/code&gt; class.&lt;/p&gt;&lt;/body&gt;</content>
</entry>
</feed>