dou9022 2013-11-08 16:24
浏览 17
已采纳

如何将SilverStripe中的安全性/登录URL别名更改为用户/登录

I am working on a new website being built in SilverStripe. Currently I am having a ton of trouble trying to get the system to let me change the URL alias (or create a second one) for the Security controller's login (and eventually logout) function.

I have tried playing around with the routes.yml file and I tried creating the paths in my own UserController and loading directly from the Security controller with "return Security::login()". But that gives me errors about the use of the static functions.

Unfortunately I don't come from a ton of object oriented experience and this is the first CMS I have used that actually uses a bunch of true object orientation. The current version of SilverStripe we are using is 3.0 (but we will be upgrading to 3.1.1 in a few days).

Does anyone know much about the routing in SilverStripe?

  • 写回答

2条回答 默认 最新

  • dongxiezhuo8852 2013-11-09 03:43
    关注

    as you stated correctly, SilverStripe has routes, and they can be defined in a yaml config file.

    if you take a look at the existing routes.yml in the framework you can see how the default security route is defined: https://github.com/silverstripe/silverstripe-framework/blob/fd6a1619cb7696d0f7e3ab344bc5ac7d9f6cfe77/_config/routes.yml#L17

    if you just want to replace the Secuirty in Secuirty/login, its as easy as just creating your own routes.ymlin mysite/_config/ with the following content:

    ---
    Name: myroutesorsomeotherrandomname
    Before: '*'
    After:
      - '#rootroutes'
      - '#coreroutes'
      - '#modelascontrollerroutes'
      - '#adminroutes'
    ---
    Director:
      rules:
        'foobar//$Action/$ID/$OtherID': 'Security'
    

    NOTE: make sure you ran a ?flush=1 to ensure the yml file is loaded (they get cached)
    also make sure you use spaces in the yml file, if you use tabs you are going to have a bad time


    if you however wish to also replace /login and /logout this is no longer a thing for routing.
    login and logout are actions (php functions that are listed in Security::$allowed_actions and thus available as URL) on the on Security.
    but its still rather easy, just subclass Security and create the actions you want:

    <?php
    
    class MySuperSecurity extends Security {
        private static $allowed_actions = array(
            'showMeTheLoginForm',
            'alternative_login_action_with_dashes',
            'doTheLogout',
        );
    
        function showMeTheLoginForm() {
            // can be visited via http://website.com/foobar/showMeTheLoginForm
            return $this->login();
        }
    
        function alternative_login_action_with_dashes() {
            // can be visited via http://website.com/foobar/alternative-login-action-with-dashes
            return $this->login();
        }
    
        function doTheLogout($redirect = true) {
            // can be visited via http://website.com/foobar/doTheLogout
            return $this->logout($redirect);
        }
    }
    

    and make the route point to your new class instead of Security inside the routes.yml:

        'foobar//$Action/$ID/$OtherID': 'MySuperSecurity'
    

    NOTE: again, make sure you did a ?flush=1, both the private static $allowed_actions as well as the yml config file are cached by silverstripe.
    NOTE: both solutions suggested in this post will create an additional route to login and does not replace the existing one, so the old Security/login will still be available

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行