Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Dataobject as Page, and routing


Go to End


2 Posts   970 Views

Avatar
Dexy

Community Member, 1 Post

8 November 2016 at 12:48am

Edited: 08/11/2016 12:49am

Hello,

i'm creating some sort of 'Dataobject as Page' stuff, and i have URLs like this at the moment:

http://[domain]/dataobjects/[dataobject-urlsegment]

I would like to have it on this way:

http://[domain]/[dataobject-urlsegment]

but without breaking default SS routes ('admin', 'dev/build', 'about-us', etc).. Any tip (suggestion, where to look on) for me, how to achieve this?

Thank you

Avatar
WebSpilka

Community Member, 89 Posts

17 November 2016 at 4:40am

Hi
I solved it this way:
but my URL looks like : http://[domain]/[BlogCategory-Page-urlsegment]/[BlogEntry-dataobject-urlsegment]

in BlogCategory.php

class BlogCategory_Controller extends Page_Controller {
	private static $allowed_actions = array(
		'show'
	);

	private static $url_handlers = array(
		'$Slug!' => 'show',
	);

public function show()
	{
		if($item = $this->getCurrentItem($DOclass='BlogEntry'))
		{

			$MainImageURL = "";
			if ($item->MainImageID){
				$MainImage = File::get()->filter("ID", $item->MainImageID)->first();
				if ($MainImage->ClassName === 'Image'){
					$MainImageURL = $MainImage->AbsoluteURL;
				}
			}

			$data = array(
					'Item' => $item,
					'Title' => $item->Title,
					'MetaDescription' => StripbbTagParser::parseMetaDescriotion($item->IntroText),
					'MainImage' => $MainImageURL,
				);

				return $this->customise(new ArrayData($data));

		}
		else
		{
			return $this->httpError(404);
		}
	}

	public function getCurrentItem($DOclass = null, $itemURLSegment = null)
	{
		$params = $this->request->allParams();
		
		if (!$DOclass){ return false;}

		if($itemURLSegment)
		{
			return false;
			//return Reviews::get()->byID($itemID);
		}
		elseif(isset($params['Action']))
		{
			//Sanitize
			$itemURLSegment = Convert::raw2sql($params['Action']);
			if($itemURLSegment==="tag"){
				$itemURLSegment = Convert::raw2sql($params['ID']);
			}
			return $DOclass::get()->filter("URLSegment", $itemURLSegment)->first();
		}		
	}