如何在Kohana中包装PHP旧代码? [英] How can I wrap PHP legacy code in Kohana?

查看:58
本文介绍了如何在Kohana中包装PHP旧代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多用PHP编写的遗留代码,这些代码不是在任何特定框架上编写的,而是大部分是老式的风格(即内联)的PHP.但是,我的大多数新代码都是在Kohana 3.1.X框架上编写的.尽管Kohana确实允许遗留代码和Kohana文件在同一网站上共存,但我现在想将每个遗留代码文件包装为一个视图,并充分利用Kohana的MVC设计模式和URL重写.但是我的遗留代码无法访问从视图本身内部定义的全局变量(即遗留代码),也无法利用试图通过全局作用域语句查看那些全局变量的内联函数遇到问题.例如:

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()
}
...

由于我有很多遗留代码,因此将所有这些遗留代码文件重构为真实视图是不切实际的.我如何将这些文件视为视图,或者如何像对待视图一样访问它们,以便我可以从现在开始在控制器中编写逻辑,而不是内联(因此,遵循真正的MVC设计模式),然后绑定变量这些旧文件?

我确实查看了在PHP中,我如何将过程代码包装在一个类中?,但是由于我正在处理Kohana框架,因此这篇文章在这种情况下并没有真正的作用.

更新:

Kohana似乎正在使用输出缓冲区,这就是为什么它无法访问旧文件中的此类全局变量的原因.有没有人成功在Kohana 3.2中获得视图以访问全局变量?

解决方案

我从没有尝试过,但是您可以尝试将代码放到控制器中,而不是视图中.

假设您在此处为控制器使用Controller_Template

public function action_legacy() {
    $this->auto_render = FALSE;

    include('legacy file'); 
    // you could cut and paste the legacy code here, but it might get to messy
}

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?

解决方案

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

public function action_legacy() {
    $this->auto_render = FALSE;

    include('legacy file'); 
    // you could cut and paste the legacy code here, but it might get to messy
}

这篇关于如何在Kohana中包装PHP旧代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆