Jump to:

23552 Posts in 19349 Topics by 2891 members

General Questions

SilverStripe Forums » General Questions » Intercepting Request Handling

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

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

Page: 1
Go to End
Author Topic: 1452 Views
  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Intercepting Request Handling Link to this post

    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;
       }

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Intercepting Request Handling Link to this post

    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.

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Intercepting Request Handling Link to this post

    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

  • Lucas
    Avatar
    Community Member
    10 Posts

    Re: Intercepting Request Handling Link to this post

    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

  • Chris_Bryer
    Avatar
    Community Member
    35 Posts

    Re: Intercepting Request Handling Link to this post

    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.

  • Lucas
    Avatar
    Community Member
    10 Posts

    Re: Intercepting Request Handling Link to this post

    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

    1452 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.