Search code examples
cakephp-2.0attr-protected

Asset Helper and protected View _scripts in CakePHP 2.0


After upgrading to 2.0 many "hacks" like accessing protected attributes are not possible anymore.

For example (AssetHelper):

$scripts = $this->View->_scripts;
//pack and return combined scripts

The helper fatal errors, of course. Does anyone know how to access those attributes in 2.0 without creating too much overhead in the process? Are the AssetHelper and other such classes outdated if they try to access the View from inside a Helper?


Solution

  • The new way of accessing the View from a Helper in 2.0 is $this->_View which is protected. You can see it on the Helper API page.

    I looked at the AssetHelper on Github and it is outdated. It still retrieves the view from the ClassRegistry instead of the new method. It also accesses the __scripts attribute of the old 1.3 View class which wasn't actually "private". I think you're correct that the change to real visibility declarations has broken this use.

    Just brainstorming, but I wonder if you could make your own View class that has a getter for the _scripts attribute like $this->_View->getScripts(). I know that in 2.0 they added a slick ability to alias core classes; while I think this is restricted to helpers, components and behaviors it's something to think about.

    Hope that helps.