dscuu86620 2015-07-12 15:22
浏览 57
已采纳

Silverstripe扩展控制器操作

I'm trying to add a Controller Action to the Page_Controller class via an extension.

The desired result is to be able to go to www.mysite.com/setlanguage/spanish for instance and have that update the site's language to spanish via a browser cookie.

However I'm failry new to Extensions in SilverStripe and so far when I visit the link to the controller action all I get is a 404 error.

Please take a look at my code...

class Page_ControllerLanguageExtension extends Extension {

    private static $allowed_actions = array(
        'setLanguage'
    );

    public function setLanguage(SS_HTTPRequest $request) {

        $requestedLanguage = $request->param('ID');
        $languageCookie = new Cookie;
        $languageCookie->set('site_language', $requestedLanguage);

        return $this->owner->RedirectBack();

    }
}

And I'm calling in the extension with a YML config file:

Page_Controller:
  extensions:
    - Page_ControllerLanguageExtension

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • du1108 2015-07-15 11:03
    关注

    So if you need /setlanguage/<language> as the url you have to route the url /setlanguage/ to a seperate controller:

    class SetLanguageController extends Controller {
    
        public function index(SS_HTTPRequest $request) {
    
            $requestedLanguage = $request->param('Language'); //as defined in config below
            $languageCookie = new Cookie;
            $languageCookie->set('site_language', $requestedLanguage);
    
            return $this->RedirectBack();
    
        }
    }
    

    We don't need to define $allowed_actions in this case, cause action index is allowed by default.

    Now in your /mysite/_config/routes.yml you have to define the route to your controller:

    ---
    Name: mysite-routes
    After: framework/routes#coreroutes
    ---
    
    Director:
      rules:
        'setlanguage/$Language': 'SetLanguageController'
    

    See also: Docs for routing

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题