Answers.Body: <p>Have a look at the <a href="http://buildwithcraft.com/classreference/services/HttpRequestService#segments-detail" rel="nofollow">segments</a> property (shortcut for <code>getSegments()</code> method) of the HttpRequestServices class, which returns an array of uri segments.</p>
  
  <pre><code>craft()-&gt;request-&gt;segments;
  </code></pre>
  
  <p>To get the last segment you could use php's <code>end()</code> or <code>array_pop()</code> method.</p>
  
  <pre><code>$segments = craft()-&gt;request-&gt;segments;
  echo end($segments);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-24T21:53:57.783Z
Answers.Text: So close, yet so far. Thank you - this worked perfectly.
Answers.UserDisplayName: Damon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-25T00:25:03.237Z
Answers.Text: Glad it worked! "So close, yet so far."?! The first of many challenges ahead? or what? :)
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-25T00:40:11.857Z
Answers.Text: That - yes. :) And I was close to getting the above to work. On the right track anyway. Coming from CodeIgniter I knew what I was looking for, just not sure how to play by Craft's rules just yet.
Answers.UserDisplayName: Damon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-25T00:49:34.443Z
Answers.Text: Most of the time I don't know these answers off the top of my head either — but getting much better at finding answers. Most of this kind of functionality will be in the services class (so [requestServices](http://buildwithcraft.com/classreference/services/HttpRequestService) in this case). Then look through the properties and methods until something catches your eye that looks like it might do the trick — or just `var_dump()` the object and go from there.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-24T18:00:36.123Z
Answers.LastActivityDate: 2015-04-24T18:13:39.133Z
Answers.LastEditDate: 2015-04-24T18:13:39.133Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>I have some validation that checks for a unique email when creating a new person. Works great.</p>
  
  <p>When I am updating the person, it's no surprise the system won't let me save because the email I am saving already exists.</p>
  
  <p>I am working on a condition that compares the id to the existing person to who is getting updated. </p>
  
  <p><strong>Plugin_EmailValidator.php</strong></p>
  
  <pre><code>...
  $person = craft()-&gt;db-&gt;createCommand()
      -&gt;select('id, 'firstName, lastName')
      -&gt;from('people')
      -&gt;where('email=:email', array(':email' =&gt; $model['email']))
      -&gt;queryRow();
  
  if(!empty($person)) {
      error_log('MATCH FOUND');
  
     // I have the id of the existing account. Need to compare against uri segment?
    $existingId = $person['id];
    $segment = craft()-&gt;request-&gt;getLastSegment();
  
    error_log($segment); // error!
  
     $message = Craft::t("Email already exists.");
     $this-&gt;addError($model, $attribute, $message);
  }
  </code></pre>
  
  <p>This is what my url looks like:</p>
  
  <pre><code>/admin/plugin/controller/update/123
  </code></pre>
  
  <p>I see examples of using the request methods within twig, i'm trying to access it via php.</p>
  
  <p>I've also tried this:</p>
  
  <pre><code>craft()-&gt;request-&gt;getQuery('update');
  </code></pre>
  
  <p>This will run without error, but it also returns nothing.</p>
  
Tags: <plugin-development><url><validation>
Title: How do I get the last uri segment from within a plugin (php)
ThreadText: # How do I get the last uri segment from within a plugin (php)
  
  (Asked by: username_0 on 2015-04-24)
  
  I have some validation that checks for a unique email when creating a new person. Works great.
  
  When I am updating the person, it's no surprise the system won't let me save because the email I am saving already exists.
  
  I am working on a condition that compares the id to the existing person to who is getting updated.
  
  **Plugin\_EmailValidator.php**
  
  ```
  ...
  $person = craft()->db->createCommand()
      ->select('id, 'firstName, lastName')
      ->from('people')
      ->where('email=:email', array(':email' => $model['email']))
      ->queryRow();
  
  if(!empty($person)) {
      error_log('MATCH FOUND');
  
     // I have the id of the existing account. Need to compare against uri segment?
    $existingId = $person['id];
    $segment = craft()->request->getLastSegment();
  
    error_log($segment); // error!
  
     $message = Craft::t("Email already exists.");
     $this->addError($model, $attribute, $message);
  }
  
  ```
  
  This is what my url looks like:
  
  ```
  /admin/plugin/controller/update/123
  
  ```
  
  I see examples of using the request methods within twig, i'm trying to access it via php.
  
  I've also tried this:
  
  ```
  craft()->request->getQuery('update');
  
  ```
  
  This will run without error, but it also returns nothing.
  
  **Tags:** plugin-development, url, validation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-04-24. Score: 3 | Accepted answer
  
  Have a look at the [segments](http://buildwithcraft.com/classreference/services/HttpRequestService#segments-detail) property (shortcut for `getSegments()` method) of the HttpRequestServices class, which returns an array of uri segments.
  
  ```
  craft()->request->segments;
  
  ```
  
  To get the last segment you could use php's `end()` or `array_pop()` method.
  
  ```
  $segments = craft()->request->segments;
  echo end($segments);
  
  ```
  
  > **Comments:**
  > **username_0**: So close, yet so far. Thank you - this worked perfectly.  
  > **username_1**: Glad it worked! "So close, yet so far."?! The first of many challenges ahead? or what? :)  
  > **username_0**: That - yes. :) And I was close to getting the above to work. On the right track anyway. Coming from CodeIgniter I knew what I was looking for, just not sure how to play by Craft's rules just yet.  
  > **username_1**: Most of the time I don't know these answers off the top of my head either — but getting much better at finding answers. Most of this kind of functionality will be in the services class (so [requestServices](http://buildwithcraft.com/classreference/services/HttpRequestService) in this case). Then look through the properties and methods until something catches your eye that looks like it might do the trick — or just `var_dump()` the object and go from there.  
  
  ---
  
  