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

Intercepting Request Handling


Go to End


6 Posts   2345 Views

Avatar
Ben Gribaudo

Community Member, 181 Posts

6 March 2009 at 8:32am

Edited: 06/03/2009 8:33am

Hello,

My Goal: intercept requests for top-level URLs (/something, not /parent/child) that do not exist in SS. When one is found, have SS query a different data store to see if content exists for the given URL. If it does, render the content. If not, resume normal SS request processing (i.e. a file not found error should be displayed).

Question: is this (below) a good way to implement this functionality?

Thank you,
Ben

In Page.php's Page_Controller:

	public function handleRequest(HTTPRequest $request) {
		$this->pushCurrent();
		$params = $request->allParams();
		$urlsegment = $params['URLSegment'];
		$response = parent::handleRequest($request);
		
		if ($response->getStatusCode() == 404 && (!empty($urlsegment) && $consultant = DataObject::get_one('ConsultantModel', 'URLKey = "' . Convert::raw2SQL($urlsegment) . '"'))) {
			$response = $this->renderWith(array("Consultant", "Page"), $consultant);
		}
		
		$this->popCurrent();
		return $response;
	}

Avatar
Hamish

Community Member, 712 Posts

6 March 2009 at 9:20am

Edited: 06/03/2009 9:21am

Have a look at:

http://doc.silverstripe.com/doku.php?id=execution-pipeline

A better way is to add a new controller to handle this logic. You new controller will:

1. Detect if the page exists, if so, handle the request on to the usual controller

2. Otherwise, try to find the external content

Then you'll need to add a director rule that takes precendence over the normal site tree controller, that directs requests to your new controller.

Avatar
Ben Gribaudo

Community Member, 181 Posts

7 March 2009 at 3:14am

Hamish,

Thank you for suggesting that I introduce another controller. I like the idea. It will keep Page_Controller, which is extended by multiple classes, free from specific behavior that only relates to one use case.

Ben

Avatar
Lucas

Community Member, 10 Posts

18 February 2012 at 7:17am

I've been trying to work this one out as well. I'm trying out a sapphire install without the CMS, which probably makes things more complicated (and unstable).

I have this in my _config.php:

Director::addRules(100, array(
	'$Action/$ID/$OtherID' => 'TestController'
));

I have two questions:

1. Is this correct? It doesn't seem to be hooking up right.
2. Can I still take advantage of the default template selection? (eg. TestController.ss, TestController_myaction.ss)

Cheers,
- Luke

Avatar
Chris_Bryer

Community Member, 35 Posts

25 February 2012 at 6:36pm

Lucas, you'll probably want to do something like this in your _config.php file:

Director::addRules(100, array(
	'the-url-segment' => 'TestController'
));

from there you can pick up the requests in the controller's index() function. also make sure include all functions in the $allowed_actions array.

i've never tried running sapphire separate from the CMS so im not sure about #2.

Avatar
Lucas

Community Member, 10 Posts

25 February 2012 at 7:37pm

Thanks Chris. I ended up using:

'urlsegment//$Action/$ID/$OtherID' => 'NGVHController'

I'd hoped to replace the root of the site with my controller, but couldn't work out how to do so, while still getting the default template magic, and admin urls, etc.

Anyway, apart from a few minor glitches, sapphire works quite well alone.

Cheers,
- L