In SilverStripe 4, how to extend an existing metho

2019-07-09 08:14发布

问题:

I want to override 'loginForm' method in 'LoginHandler' class. I am trying to use the code given below for that. But it is not working.

LoginHandlerExtension.php

    <?php

    use SilverStripe\Core\Extension;

    class LoginHandlerExtension extends Extension {

        public function loginForm() {
            return 'xxxxxx';
        }
    }

app.yml

SilverStripe\Security\MemberAuthenticator\LoginHandler:
  extensions:
    - LoginHandlerExtension

回答1:

What would you want to override on the loginForm method? If you want it to use a different Form class (e.g. MyMemberLoginForm), you can tell Injector to use your custom class in YML like this:

SilverStripe\Core\Injector\Injector:
  MemberLoginForm:
    class: My\Namespaced\LoginForm

as the LoginForm method does nothing else but return the form.