Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do multiple @Before methods work in Controller inheritance?

I'm trying something similar to this, and only one of the @Before methods gets called:

public abstract class ControllerBase extends Controller {
  @Before
  static void foo() {
    // this actually gets called
  }
}

public class ConcreteController extends ControllerBase {
  @Before
  static void bar() {
    // This DOES NOT get called
  }

  public static void index() {
    render();
  }
}

Is this a bug, feature, or something I'm doing wrong?

like image 419
ripper234 Avatar asked Dec 03 '25 07:12

ripper234


1 Answers

You're trying to do something weird. And I think your example doesn't match your question. Did you mean to implement ConcreteController on ControllerBase? Rather than both of them extending on Controller?

The @before tag is a concrete class tag. Only the one in the concrete class will get executed.

You can @override the original function, but I don't think that's what you were looking for.

The best way to get what you want is to remove @before from the abstract and from the concrete function call the implemented function you want to run.

public abstract class ControllerBase extends Controller {
  static void foo() {
    // this actually gets called
  }
}

public static class ConcreteController extends Controller {
  @Before
  static void bar() {
    foo();
    // This DOES NOT get called
  }

  public static void index() {
    render();
  }
}
like image 151
awm Avatar answered Dec 05 '25 20:12

awm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!