问题
How can I change the url of an dataobject? I can get the dataobject under the following url with this function.
www.domain.tld/articles/art?=1234
public function ArticleByID() {
$articleID = isset($_GET['art']) ? $_GET['art'] : false;
return $articleID ? Articles::get()->filter(array('ShortNumber' => $articleID))->First() : false;
}
But what I want is for example this www.domain.tld/articles/1234 or www.domain.tld/members/member-name
回答1:
You can create a show()
function that you would call on your ArticleHolder
to get and return the Articles
page you want with a URL like www.domain.tld/articles/show/1234
ArticleHolder.php
...
class ArticleHolder_Controller extends Page_Controller {
...
public function show(SS_HTTPRequest $request) {
if ($request->param('ID') && $article = Articles::get()->filter(array('ShortNumber' => $page->param('ID')))->First()) {
return $this->customise(array(
'Title' => $article->Title,
'Content' => $article->Content,
'MetaTitle' => $article->MetaTitle,
'MetaDescription' => $article->MetaDescription,
'MetaKeywords' => $article->MetaKeywords
))->renderWith(
array('ArticlesPage', 'Page')
);
}
return $this->httpError(404);
}
...
}
Or, better yet, use URLSegment
to get your articles. For this you need to the URLSegment
in your Article
class.
There is an excellent tutorial for this at ssbits.com: http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-2-using-model-admin-and-url-segments-to-create-a-product-catalogue/
The tutorial is for Silverstripe 2.4, but the code should work with minor tweaks in Silverstripe 3.1.
There is also a module based off this tutorial called DataObjectAsPage: https://github.com/arambalakjian/DataObjects-as-Pages
You could use this a base for your code.
来源:https://stackoverflow.com/questions/19477858/define-the-url-of-an-dataobject-silverstripe-3-1