I have a good amount of legacy code written in PHP, which was not written on any particular framework rather it is mostly old-school style (i.e. inline) PHP. However, most of my new code is written on the Kohana 3.1.X framework. Although Kohana does allow both legacy code and Kohana files to coexist on the same Web site, I would like to now wrap up each legacy code file as a view and take full advantage of Kohana's MVC design pattern and URL rewriting. Yet I am encountering problems with my legacy code not being able to access global variables defined from within the view itself (i.e. legacy code) and am not able to utilize inline functions that are trying to see those global variables via the global scope statement. For example:
application/views/legacy.php
$gvar = 5;
function getadminsettings(){
global $gvar;
echo $gvar;
}
application/classes/controller/myctrl.php
...
public function action_legacy() {
// call legacy.php as a view via View::factory()
}
...
Since I have so much legacy code, it is impractical to refactor all of these legacy code files to be true views. How might I treat these files as views or access them as if they were view-like so that I can from-now-on write my logic in the controller and not inline (thereby, following a true MVC design pattern) and then bind variables to these legacy files?
I did look at In PHP, how can I wrap procedural code in a class? but this post doesn't really work in this case because I am dealing with the Kohana framework.
Update:
Kohana appears to be using an output buffer and that is why it is unable to access such global variables in the legacy files. Has anyone been successful in getting a view in Kohana 3.2 to access global variables?
Moving a legacy application to an MVC-style framework does not lend itself to a cut-and-paste approach. In any but the simplest applications, a great deal of time and effort will be required to refactor the code to make it MVC-compatible.
If your legacy application works and is not under regular development, you will gain little by switching to MVC. The MVC pattern often introduces overhead, and is primarily a benefit to developers rather than end users.
However, if your application requires regular maintenance, you will have to expend the required effort to refactor the legacy code to fit the new method.
I have never tried this, but you could try putting the code into the controller, rather then the view.
Assuming here you are using a Controller_Template for your controller