This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Blackpig Creative
Blackpig Creative

In my component I receive the record id and I want to use the findorFail() method. The Laravel documentation http://laravel.com/docs/4.2/eloquent says I need to add an event listener for the ModelNotFoundException. e.g.

use Illuminate\Database\Eloquent\ModelNotFoundException;
App::error(function(ModelNotFoundException $e)
{
    return Response::make('Not Found', 404);
});

Where do I need to place this code to trigger a 404?

Blackpig Creative
Blackpig Creative

I'd still like to know where to place the above code so that can be applied globally across the site but as a quick fix I just use a standard try/catch block

use Illuminate\Database\Eloquent\ModelNotFoundException;
try {
        $record = Model::findOrFail( $this->param('id') );
    } catch ( ModelNotFoundException $e ) {
        return \Response::make($this->controller->run('404'), 404);
    }
axomat
axomat

I am just looking into this for you.

Every time I need to do it, it takes me ages to remember, then I remember it's really simple!

axomat
axomat

I think this is what you want

Blackpig Creative
Blackpig Creative

Thanks, but that's not what I'm after. I want to raise a 404 if a record can't be found.

My solution above is fine and I can add it to each component but I'd rather have a global listener to redirect to a 404 if an invalid record id is passed.

noureddine.ballouki20872
noureddine.ballouki20872

put your code in plugin.php on boot function

walidh9326228
walidh9326228

You can use the following

return App::make('Cms\Classes\Controller')->setStatusCode(404)->run('/404');

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.