Hi All,
I was just wondering if SIlverstripe had anything similar to the get_object_or_404 method in django?
Basically I want to return a 404 error if dataobject::get returns an empty object set.
Any help greatly appreciated :).
Mo
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
Hi All,
I was just wondering if SIlverstripe had anything similar to the get_object_or_404 method in django?
Basically I want to return a 404 error if dataobject::get returns an empty object set.
Any help greatly appreciated :).
Mo
Wow a whole year and I still haven't found an answer to this.
Anyone got any ideas??
Mo
I use something like this to return a 404:
function Article(){
$params = $this->getURLParams();
if($url_segment = $params['ID']){
$article = DataObject::get_one('Article',"URLSegment = '".(string)$url_segment."'");
if(!$article && !Director::redirected_to()){
$errorPage = DataObject::get_one('ErrorPage');
Director::redirect($errorPage->Link(),404);
} else{
return $article;
}
}
}
Hmm, yea, thats an idea. Hadn't thought of that.
Nice one Martijn!
Cheers
Mo
Or to return a 404 you can do the following in a controller.
$this->httpError(404);