21307 Posts in 5737 Topics by 2603 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1167 Views |
-
Intercepting Request Handling

6 March 2009 at 8:32am Last edited: 6 March 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,
BenIn 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;
} -
Re: Intercepting Request Handling

6 March 2009 at 9:20am Last edited: 6 March 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.
-
Re: Intercepting Request Handling

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
-
Re: Intercepting Request Handling

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 -
Re: Intercepting Request Handling

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.
-
Re: Intercepting Request Handling

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
| 1167 Views | ||
|
Page:
1
|
Go to Top |


