SilverStripe 3.1+ Dynamically creating page redire

2019-09-04 04:06发布

问题:

I have a page type 'ProductPage', it has tabs that are navigated to like so:

/ProductPageUrlSegment/?tab=video

/ProductPageUrlSegment/?tab=audio

/ProductPageUrlSegment/?tab=photos

I'd like for redirects to be created when each new product page is created so that if you navigated /ProductPageUrlSegment/video it goes to /ProductPageUrlSegment/?tab=video

and the same for all tabs. I'm not clear if I should be using Routing https://docs.silverstripe.org/en/3.3/developer_guides/controllers/routing/ or redirection https://docs.silverstripe.org/en/3.3/developer_guides/controllers/redirection/

I have a link function for another project which goes to the parent page

public function Link() {
    return $this->Parent()->Link() . '#' . $this->URLSegment;
}

Mine would be something like:

public function LinkVideo() {
    return $this->Link()->'/?=video' . '#' . $this->URLSegment->'/video';
}

I don't have the knowledge to work this out so any guidance appreciated.

回答1:

This will achieve the above, by handling the URL as an action and then redirecting to the same page, but with the get variable set...

class ProductPage extends Page {

}

class ProductPage_Controller extends Page_Controller {

    private static $allowed_actions = array(
        'video',
        'audio',
        'photos',
    );

    public function video() {
         $this->redirect($this->Link().'?tab=video');
    }
    public function audio() {
        $this->redirect($this->Link().'?tab=audio');
    }
    public function photos() {
        $this->redirect($this->Link().'?tab=photos');
    }
}