21294 Posts in 5734 Topics by 2602 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1404 Views |
-
Get Object or 404

16 October 2009 at 9:55pm
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
-
Re: Get Object or 404

28 October 2010 at 4:18am
Wow a whole year and I still haven't found an answer to this.
Anyone got any ideas??
Mo
-
Re: Get Object or 404

28 October 2010 at 5:11am
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;
}
}
} -
Re: Get Object or 404

28 October 2010 at 5:13am
Hmm, yea, thats an idea. Hadn't thought of that.
Nice one Martijn!
Cheers
Mo
-
Re: Get Object or 404

28 October 2010 at 12:16pm
Or to return a 404 you can do the following in a controller.
$this->httpError(404);
| 1404 Views | ||
|
Page:
1
|
Go to Top |



