SilverStripe 3.1+ Dynamically creating page redirects

空扰寡人 提交于 2020-01-07 02:01:13

问题


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


来源:https://stackoverflow.com/questions/36947205/silverstripe-3-1-dynamically-creating-page-redirects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!