Answers.Body: <p>You can get an entry’s URI via <code>$entry-&gt;uri</code>.</p>
  
  <p>If you want to be able to tell if an entry’s URI is actually changing, you will also need to listen to the <code>onBeforeSafeEntry</code> event, and store its original URI:</p>
  
  <pre class="lang-php prettyprint-override"><code>private $_oldUris;
  
  public function init()
  {
      craft()-&gt;on('entries.beforeSaveEntry', function(Event $event) {
          /** @var EntryModel $entry */
          $entry = $event-&gt;params['entry'];
  
          // Store the entry's old URI
          if ($entry-&gt;id)
          {
              $this-&gt;_oldUris[$entry-&gt;id] = $entry-&gt;uri;
          }
      });
  
      craft()-&gt;on('entries.saveEntry', function(Event $event) {
          /** @var EntryModel $entry */
          $entry = $event-&gt;params['entry'];
  
          // Did the URI just change?
          if (isset($this-&gt;_oldUris[$entry-&gt;id]) &amp;&amp; $this-&gt;_oldUris[$entry-&gt;id] != $entry-&gt;uri)
          {
              // Yes it did. The new URI is available at $entry-&gt;uri.
              // ...
          }
      });
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-05T10:51:37.513Z
Answers.LastActivityDate: 2016-04-05T10:51:37.513Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I'm trying to create a dynamic rerouter based on <a href="https://github.com/davist11/craft-reroute" rel="nofollow">reroute plugin</a>, but with the improvement to automatically create a reroute when a new entry is saved. So I listen for the <code>entries.onSaveEntry</code> event, but how can I find its route ?</p>
  
  <p>Thanks a lot !</p>
  
Tags: <plugin-development><routing>
Title: Get entry route
ThreadText: # Get entry route
  
  (Asked by: username_0 on 2016-04-04)
  
  I'm trying to create a dynamic rerouter based on [reroute plugin](https://github.com/davist11/craft-reroute), but with the improvement to automatically create a reroute when a new entry is saved. So I listen for the `entries.onSaveEntry` event, but how can I find its route ?
  
  Thanks a lot !
  
  **Tags:** plugin-development, routing
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-04-05. Score: 2 | Accepted answer
  
  You can get an entry’s URI via `$entry->uri`.
  
  If you want to be able to tell if an entry’s URI is actually changing, you will also need to listen to the `onBeforeSafeEntry` event, and store its original URI:
  
  ```php
  private $_oldUris;
  
  public function init()
  {
      craft()->on('entries.beforeSaveEntry', function(Event $event) {
          /** @var EntryModel $entry */
          $entry = $event->params['entry'];
  
          // Store the entry's old URI
          if ($entry->id)
          {
              $this->_oldUris[$entry->id] = $entry->uri;
          }
      });
  
      craft()->on('entries.saveEntry', function(Event $event) {
          /** @var EntryModel $entry */
          $entry = $event->params['entry'];
  
          // Did the URI just change?
          if (isset($this->_oldUris[$entry->id]) && $this->_oldUris[$entry->id] != $entry->uri)
          {
              // Yes it did. The new URI is available at $entry->uri.
              // ...
          }
      });
  }
  
  ```
  
  
  ---
  
  