This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
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?
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);
}
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!
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.
You can use the following
return App::make('Cms\Classes\Controller')->setStatusCode(404)->run('/404');
1-7 of 7