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.


$person = craft()->db->createCommand()
    ->select('id, 'firstName, lastName')
    ->where('email=:email', array(':email' => $model['email']))

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:


I see examples of using the request methods within twig, i'm trying to access it via php.

I've also tried this:


This will run without error, but it also returns nothing.

1 Answer 1


Have a look at the segments property (shortcut for getSegments() method) of the HttpRequestServices class, which returns an array of uri segments.


To get the last segment you could use php's end() or array_pop() method.

$segments = craft()->request->segments;
echo end($segments);
  • So close, yet so far. Thank you - this worked perfectly.
    – Damon
    Apr 24, 2015 at 21:53
  • Glad it worked! "So close, yet so far."?! The first of many challenges ahead? or what? :) Apr 25, 2015 at 0:25
  • 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
    Apr 25, 2015 at 0:40
  • 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 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. Apr 25, 2015 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.