Best Practises: Bug Fixing without Core Hacking
Posted on 1/2/06 by Felix Geisendörfer
The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.
Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.
Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).
Update: I fixed a minor bug in the code. I don't think it changes anything but I've forgot to pass some variable as a reference which it should be passed as. I also corrected the code snippet below.
One thing I've been confronted with since the first time I've used CakePHP is that there where bugs that I would need to fix in order to make my apps work. Now I could have just gone into the file and change the stuff around, but the problem was that all these fixes would be lost with the next update, no matter if the problem still existed or not.
So what I found was that most of the Problems can be traced back to Model, Controller, View (and Helpers). Here is where Cake's very good architecture comes in handy, since you can just use classes that extend your "Fix-Classes". But how does one exactly do that? Since I think it's something a lot of people will come across I've created a little Zip package called FlexiFix which contains a vendor folder with a fix_controller, fix_model and fix_view and ... (this was the most difficult thing), a folder called "helper" where you can put in files that will overwrite existing helpers like Html, but extend the original ones.
So if you want to try out my little package the download it here: (flexifix 0.2.zip) [3,57 kbyte].
If you want to do it yourself but are interested in the way I overwrite exisiting helper classes have a look at this snippet out of fix_view.php:
$loadedHelpers = &parent::_loadHelpers($loaded, $helpers);
$fixHelperFolder = &new Folder(VENDORS.DS.'flexifix'.DS.'helpers');
$fixHelperFiles = $fixHelperFolder->find('^.*\.php$');
$emptyLoaded = array();
foreach($fixHelperFiles as $num => $file);
list($helperName, $ext) = explode('.', $file);
$helperName = Inflector::camelize($helperName);
$fixHelperName = 'Fix'.$helperName;
$fixHelper = &parent::_loadHelpers($emptyLoaded, array($fixHelperName));
$loadedHelpers[$helperName] = $fixHelper[$fixHelperName];
I hope this helps people to seperate their own bug fixes from the applications they write and therefore making updating an easier part of life.
You can skip to the end and add a comment.
Hey! this is good. I'm gonna try it. But please report all the bugs you found
I think it is a good solution when you want to distribute your application like rdBloggery and you don't want to force the people to patch their cake installation.
Personally, I fix bugs directly in the core as updating and merging is almost always very easy thanks to tools like Subclipse.
Nate: Well this doesn't mean that you don't submit tickets. It just means that you can go from one stable releases to the next without having to merge the trunk every night. This way you only fix the stuff you really need to have working right now without beeing afraid of breaking your app by changing function definitions. Personally I think life can be much easier if you run with the same code base for a while (~2 weeks) and don't always move the ground that you are trying to build on ^^.
RosSoft: I will, np.
Daniel Hofstetter: Well, if you are confortable with SVN and merge/diff/patch tools then that is propably just as good as what I am doing. But for everybody like me who doesn't have enough experience with SVN to reall rely on it this and likes the idea of using stable branches, this should be as fast and easy as what you are doing.
Another thing I like about extending things instead of hacking them is that you can add functionality to helpers that will maybe never become part of the core, but really are a part to ajax, js, html, etc. in your mind.
PS: I'm interested in feedback of people who tried it out.
Definitively it is useful. I have a lot of troubles with IIS and fix_controller.php is the best place to hack the core.
RosSoft: I'm glad it helps you getting cake to run for you properly and you can avoid the use of core hacks. (PhpNut_ banns people who hack the core ^^).
I just updated flexifix to 0.2 which is just a very minor bug fix. I've forgot an &-sign somewhere. Oh well, not it is in ; ).
OK after updating to RC4, finally I have removed the hacks of the core and moved to flexifix the rest of hacks that remains
RosSoft: If you find yourself in the situation of needing to "hack" something that can't be done with flexifix let me know about it and I'll see what I can do. Otherwise have fun baking ; ).
Your solution reminded me to my problem that is: I have some code from other programmer that I want to use in my app, and it uses AppController to make some system-wide verifications.
The problem is that I don't want to merge his Appcontroler inside mine because both code is still under development, and it should get a lot of conflicts to me and he.
I thought in some kind of "abstraction layer", as you are used to think of, of his application, that can communicate with mine, without to make merges, but it can visible to all my application, and eventually I can make some customizations.
Do you have any idea?
Kiyoshi: You should really come in IRC and hang out in #cakephp and/or #cakemmm since that's the best way for solving problems like this. My nickname there is the_undefined and if you type this name in I get an sound alert so you'll grab my attention : ).
One idea before: If function-names & parameters are still changing you could make a function-router that will solve problems that are results of changing function names. Depending on how OOP the code is you could either overload() the classes or create a function that you use like com('function', $param1, $param2, etc.); and wenn development bekcomes more stable you do a regex_replace on those calls in your php-documents and replace them with normal function calls. Uhm but that's just a thought, no idea how practically I'd be. I' would need to know more about the two projects to come up with something more concrete, so come on irc! : P
Flexifix is great, but now I need to override the file tags.ini.php, but I want to override only a few tags (I don't want to copy all the file)
&_loadHelpers(&$loaded, $helpers) is bugged.
Only first fixhelper is loaded because :
9. foreach($fixHelperFiles as $num => $file);
My comment was strip, so :
9. foreach($fixHelperFiles as $num => $file); // here ";"
If removed, it cause infinite loops because already loaded helpers aren't pass to parent method.
Sry, I can't follow your logic, could you explain this using a couple more words? I This class has worked good for me so far, even so I seldom used it. What exact problem did you have when using it? What errors?
Ros, I just can't get the helper functionality to work on php5 and the most recent stable distro of cake.
fix_view.php is being included by the vendor code, but the function function &_loadHelpers( &$loaded, $helpers ) is never being executed. This makes sense to me.
How is this function supposed to be called?
sorry, scratch that last comment.
why deprecate this? I think it's great. Or is there a better way to extend the helper without core hacking ?
I'm actually wondering how to override the FormHelper::file() and the only way I've found so far is copying the entire cake/libs/view/helpers/form.php to app/view/helpers/form.php and make the changed needed here.
I would love a better solution for overriding core helpers, and as you're marking this post as deprecated I wonder if you've come with a better solution ?
P.S : In fact, I don't really need to override this method, I can find a workaround by playing around with the before/between/after options but I would appreciate an other way if you know one.
Oh, and btw, the Flexifix link is broken, I can't try your way
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.
If you have a patch for Cake, why not just submit it with a ticket and help everyone else out, too?