We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Throwing an Exception

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

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

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

If user go to https://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.



145.0k
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.