Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Throwing an Exception

I am currently building a dynamic page (with custom url slug)

E.g. http://www.mydomain.com/place/helloword

helloword is my custom slug. the slug is retrieved from db.

If user go to http://www.mydomain.com/place/**hellowords**, it should be an 404 page.

In this page, i am querying my db for certain objects.

If the db query does not return me an object, i will throw an exception and redirect user to the 404 page.

If this the way you guys will approach this as well? Looking for some advices and insights.

In Phalcon, is there a way we can catch db exception? I am using mongodb. Can't manage to catch exception.



114.7k
Accepted
answer

Phalcon just return false if record don't exist. You can just do:

if(empty($model))
{
    throw new NotFoundException()
}

And handle it in dispatch listener. Im doing it this way at least. Well it could be nice idea to add some event perhaps on modelsManager or something.