<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>

CC BY-SA 3.0
2015-04-24T21:53:57.783Z
So close, yet so far. Thank you - this worked perfectly.
Damon
CC BY-SA 3.0
2015-04-25T00:25:03.237Z
Glad it worked! "So close, yet so far."?! The first of many challenges ahead? or what? :)
Douglas McDonald
CC BY-SA 3.0
2015-04-25T00:40:11.857Z
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.
Damon
CC BY-SA 3.0
2015-04-25T00:49:34.443Z
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.
Douglas McDonald
CC BY-SA 3.0
2015-04-24T18:00:36.123Z
2015-04-24T18:13:39.133Z
2015-04-24T18:13:39.133Z
Douglas McDonald
<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>

<plugin-development><url><validation>
How do I get the last uri segment from within a plugin (php)
# 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 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.  

---

