New callback methods for components

Published on and tagged with cakephp  component  feature

Recently, support for three new component callback methods has been added: beforeRender, beforeRedirect, and shutdown. (Correction: those callbacks have been around for quite a while, see Nate’s comment, but I noticed them only recently)

As its name implies, the beforeRender() callback method is called before the view is rendered, but after the beforeRender() callback method of the controller:

// in your component
public function beforeRender($controller) {
    // do something
}

The beforeRedirect() callback is also quite obvious, it gets called before a redirect is performed:

// in your component
public function beforeRedirect($controller, $url, $status = null, $exit = true) {
    // do something
}

By providing a return value, you can override the values with which the redirect method was originally called. For example:

public function beforeRedirect($controller, $url, $status = null, $exit = true) {
    // return '/redirect/target';  // a single value is treated as an URL
    return array('url' => '/redirect/target', 'status' => 307);
}

The last callback method, shutdown(), is called after Controller::render() and before Controller::afterFilter():

// in your component
public function shutdown($controller) {
    // do something
}

Those callback methods are probably a “nice to have” feature, at least I didn’t miss them up to now.

Anyway, happy baking!

14 comments baked

  • ryan

    this is awesome; i’ve actually wondered for a while why components didn’t have these, especially since behaviors have “beforeSave” and “afterSave”, etc; IMO it makes components that much more useful and flexible

  • nate

    You didn’t miss them up to now? The beforeRedirect callback was added quite a while ago, and the other two have been there for ages. Where have you been?? :-P

  • Ahsan

    Much needed functionalities for components. Thanks for sharing.

  • CakePHP Brasil » Novos métodos de callback para componentes

    […] Texto traduzido de New callback methods for components […]

  • cakebaker

    @all: Thanks for your comments!

    @nate: Yes, you are right, they have been there for quite a while, though this functionality was well hidden. Maybe it is time to add a base class for components?

  • nate

    Well-hidden? :-P If you say so. But yes, Component should have been a base class. It will be in the next version of Cake.

  • CakePHP1.2 で追加されるコンポーネントの3つのコールバック関数 | Sun Limited Mt.

    […] New callback methods for components – cakebaker によると次にリリースされる CakePHP1.2 ではコンポーネントに3つのコールバック関数が追加されるようです。 […]

  • Manny

    You know what I find ironic. There’s all of this new features and there’s no mention of them anywhere except on your blog. It would be nice if there was a change log.

    No flaming please – this is a legit comment ;-)

  • nate

    @Manny: Oh… you mean like this?

    https://trac.cakephp.org/wiki/changelog/1.2.x.x

    That’s only been there for a couple years now, and if you look, you’ll see that each one of the component callbacks mentioned here are listed specifically.

  • GreyCells

    @Manny: For an even greater view of the development flow:

    https://trac.cakephp.org/timeline

    gives you access to all the commits and bug reports too (the essential gotchas if you’re developing against the 1.2 branch)…

  • cakebaker

    @nate, Manny, GreyCells: Thanks for your comments!

    @nate: Good to hear that :)

  • brian

    while it’s great that you can find these in the changelogs, how does it go for years without being added to documentation intended for public consumption?

    And who cares if cakebaker didn’t notice, why is everyone always so defensive?

  • Daniel

    I tend to agree with Brian. After all the shenanigans that have happened between DH and the CakePHP core team, the core team members seem to be very defensive, and a little arrogant when responding to DH’s posts.

    That said, the core team are doing an awesome job with CakePHP. It’s an excellent framework, and even after much trawling around the web I can’t find anything that equals it or even comes close.

  • cakebaker

    @brian, Daniel: Thanks for your comments!

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License