New callback methods for components

Published on June 02, 2008 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 June 02, 2008 at 17:24

    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 June 02, 2008 at 17:25

    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 June 02, 2008 at 17:59

    Much needed functionalities for components. Thanks for sharing.

  • CakePHP Brasil » Novos métodos de callback para componentes June 02, 2008 at 18:23

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

  • cakebaker June 02, 2008 at 18:48

    @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 June 02, 2008 at 20:27

    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. June 03, 2008 at 03:00

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

  • Manny June 03, 2008 at 05:36

    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 June 03, 2008 at 16:14

    @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 June 04, 2008 at 13:48

    @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 June 04, 2008 at 16:40

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

    @nate: Good to hear that :)

  • brian June 05, 2008 at 20:04

    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 June 06, 2008 at 05:33

    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 June 07, 2008 at 16:08

    @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