Tuesday, September 7, 2010

Action paramater binding in Yii 1.1.4

The new Yii 1.1.4 contains numerous improvements. One that I wasn't clear on from the changelog was  the line saying "automatic action parameter binding" for $_GET values.  This is a very subtle way of saying that they've made it much easier to handle some of the 'grunt work' for actions that work with multiple $_GET variables.


http://www.yiiframework.com/doc/guide/basics.controller#action-parameter-binding

Quite simply pass in the name of the $_GET variables to the controller action method as parameters, including any default value you would like applied if they did not specify a value. 

Easily looked over, but very nice addition to the framework!!

8 comments:

  1. Thank you. I did not even know about it :D

    ReplyDelete
  2. Anything that keeps me from having:

    if (isset($_GET['var']))
    {
    $var = $_GET['var'];
    }

    is a plus in my book.. :)

    ReplyDelete
  3. Awesome! But it's not possible to use this in external CAction classes, is it? There is just the run()…

    ReplyDelete
  4. That's a really interesting question and one I don't know the answer to. I'm definitely going to do some digging and find out ;)

    ReplyDelete
  5. No, it won't work unless you're extending CInlineAction.

    The parameter binding happens in the CInlineAction::run() method. (See: http://code.google.com/p/yii/source/browse/tags/1.1.6/framework/web/actions/CInlineAction.php)

    That said, you could duplicate the functionality within a CAction that you create by duplicating what's been done for the CInlineAction.

    ReplyDelete
  6. They've just added this ability in Yii 1.1.7 to have action parameter binding to the run method of a CAction!

    http://www.yiiframework.com/doc/guide/basics.controller#action-parameter-binding

    ReplyDelete