Looking up the name and event for a ticket This code is from my Kohana project. How can I make it more beautiful? Should I use try-catch instead? How could I avoid writing exit() twice?

public function action_index()
{
    $id = $this-&gt;request-&gt;query('id');
    if (!empty($id)) {
        $ticket = ORM::factory('ticket')
            -&gt;where('id', '=', $id)
            -&gt;find();
        if (!empty($ticket)) {
            $event = ORM::factory('event')
                -&gt;where('id', '=', $ticket-&gt;event_id)
                -&gt;find();
            if (!empty($event)) {
                $this-&gt;template-&gt;ticket = $ticket-&gt;id;
                $this-&gt;template-&gt;name = $ticket-&gt;name;
                $this-&gt;template-&gt;event = $event-&gt;title;
            } else {
                exit();
            }
        } else {
            exit();
        }
    }
}
You can avoid writing exit() twice by adding a return statement at the deepest level, after passing all the checks. This should be equivalent to your code:

public function action_index()
{
    $id = $this-&gt;request-&gt;query('id');
    if (!empty($id)) {
        $ticket = ORM::factory('ticket')
            -&gt;where('id', '=', $id)
            -&gt;find();
        if (!empty($ticket)) {
            $event = ORM::factory('event')
                -&gt;where('id', '=', $ticket-&gt;event_id)
                -&gt;find();
            if (!empty($event)) {
                $this-&gt;template-&gt;ticket = $ticket-&gt;id;
                $this-&gt;template-&gt;name = $ticket-&gt;name;
                $this-&gt;template-&gt;event = $event-&gt;title;
                return;
            }
        }
        exit();
    }
}


That said, I find it a bit strange to exit() in the middle of a function.
It's an abrupt behavior in a program in general,
this kind of control is usually done at higher levels,
in order to do any necessary cleanup, such as releasing resources,
closing connections.