1
votes

I am trying to extend a method of a class but having no luck. I am doing this as part of an upgrade from 3.7 to 4.5. In 3.7 it was working fine without using traits. Reading documentation, i used ‘Extensible’ trait. Now i am getting the following error. Checked all my method access levels. All are public but still gets this error

Fatal error : Access level to SilverStripe\Core\Extensible::defineMethods() must be public (as in class SilverStripe\View\ViewableData)

    class MyOpenController extends Controller {

    use Extensible;

     private static $allowed_actions = array(
        'login',
        'logout'
    );

    public function login($data = array()) {
      //code here
      $this->extend('customFunction');
      //more code      
    }
}

I have an Extension class that has definition for this method.

class MyOpenControllerExtension extends Extension
{

    public function customFunction() {
          //some code here
    }
}

cant seem to get why i am having this error. Can someone please help me figure out what is wrong here.

Thanks DR

1

1 Answers

0
votes

ViewableData in SilverStripe 4 already implements the Extensible, Injectable, and Configurable traits that make up the common foundation you are used to from SilverStripe 3. You only need to add the Extensible trait for custom classes - anything extending Controller or DataObject already has them.

The error you are getting is caused by applying this trait twice in your class's hierarchy. ViewableData applies it, and essentially redefines the defineMethods() method at a public level. When you apply the trait again further down the hierarchy, it tries to define the defineMethods() method again at protected visibility, which is what is causing your error.

Removing use Extensible; from your controller will fix your problem.

Also, ensure you have applied your extension to your controller in YAML config:

# File: app/_config/extensions.yml
MyOpenController:
  extensions:
    - MyOpenControllerExtension