Symfony 1.4 和全局变量
我有一个非常旧的 php 应用程序(1999 年),它在过去十年中一直在开发。此时,应用程序开始显示它的年龄,因此我正在迁移到“新”框架 symfony 1.4。但由于该应用程序非常大,我无法立即执行此操作。所以我打算将旧应用程序包装到新的 symfony 应用程序中,并按功能转换功能。
此转换的第一步是让旧应用程序出现在新的 symfony 应用程序中。因此,我创建了“前端”应用程序,添加了一个“遗留”模块,将其设为默认主页,并且我将我的所有内容都放在了我的index.php(所有页面都经过此index.php)中indexAction 的indexSuccess.php 文件。我已在“视图”中添加了代码,因为其中也有功能,并且更改该设置将花费我比在旧应用程序上花费的时间更多的时间。
不幸的是我现在遇到了全局变量的问题。让我给你举个例子(我从来没有像这样做过这个注册函数,但它确实如此,所以请看过去。
$session = new ps_session;
$demo = "this is a demo variable";
$session->register('demo');
在 ps_session 中我有这个方法
public function register($var) {
global $$var;
$_SESSION [$var] = $$var;
}
所以它应该将 $demo 的内容放在名为“demo”的会话变量中”。聪明吧:)无论如何,var_dumping 向我显示 $$var 为“null”,并且如果我在调用该函数之前和之后进行 var_dump,$demo 就会被填充。没有 symfony 的完全相同的代码,它返回正确的内容。
我缺少什么?全局调用分布在这个大型应用程序的所有区域,所以我真的不想切换到其他东西,所以我希望快速修复:)
也许相关,除了index.php内容之外的所有代码都是在 frontend/lib/legacy/ 文件夹中,索引位于 frontend/modules/legacy/ (如果存在一些范围问题,我丢失了)
I've got a very old php application (1999) that has been worked on during the last ten years. At this point the app starts to show it's age so i'm in te progress of migrating to a "new" framework, symfony 1.4. But since the app is very large, i cannot do this at once. So i'm planning to wrap the old app into the new symfony app, and convert functionality by functionality.
First step in this transition was making the old app appear in the new symfony app. So, i've created the "frontend" application, added a "legacy" module, made it the default homepage, and i've put everyhting i had in my index.php (all pages went through this index.php) in the indexSuccess.php file for the indexAction. I've added the code in the "view" because there are also functions in it and changing that setup would take me more time than i want to spend on the old app.
Unfortunately i've now got an issue with global variables. Let me give you an example (i would have never made this register function like this, but it is, so please look past that.
$session = new ps_session;
$demo = "this is a demo variable";
$session->register('demo');
In ps_session i have this method
public function register($var) {
global $var;
$_SESSION [$var] = $var;
}
So it should put the content of $demo in a session var named "demo". Clever right :) Anyway, var_dumping shows me the that $$var is "null" and $demo is filled if i var_dump before and after calling the function. Exact same code without symfony and it returns the correct content.
What am i missing? The global call is spread out in all area's of this massive app so i really don't want to switch to something else, so i'm hoping for a quick fix :)
Maybe relevant, the all code except the index.php content are in frontend/lib/legacy/ folder, the index is in frontend/modules/legacy/ (if there is some scope issue i'm missing)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为由于您的 indexSuccess.php 文件包含在函数内(更准确地说,这里: lib/vendor/symfony/lib/view/sfPHPView.class.php:185 ),所以这是行不通的,因为 $demo 不是在全球范围内的时间更长。我没有看到任何简单的解决方法......
我认为您应该在 /web 中创建一个旧文件夹,如果 url 对应于尚未迁移的内容,则使用路由重定向到该文件夹。
I think that since your indexSuccess.php file is included inside a function (more precisely, here : lib/vendor/symfony/lib/view/sfPHPView.class.php:185 ), this can't work, because $demo is no longer in the global scope. I don't see any easy workaround for this...
I think you should create a legacy folder in /web , and use routing to redirect to it if the url corresponds to something not migrated yet.
我将整个旧站点放在 web/legacy 下,并从默认索引操作重定向到旧文件夹。大多数 url 都是由 mod_rewrite 制作的,因此很容易修复。其他网址经过了一个函数,因此修复没问题,并且只有少数是硬编码的。为了使其完全透明,我只需要重做主页即可,因此我的网址中没有可见的 /legacy/ 。感谢您的帮助!
I went with putting the entire old site under web/legacy and redirecting from the default index action to the legacy folder. Most of the url's were made by mod_rewrite so easily fixed. The other url's went through a function so fixing was ok, and only a few were hardcoded. To make it totally transparant, i only need to redo the homepage to start from, so i don't have a visible /legacy/ in my url. Thanks for the help!
我同意 greg0ire 的观点,这是 sfPHPView 包含 indexSuccess 的方式的问题。
您可以在default/index操作中简单地
require
index.php吗?I agree with greg0ire that this is an issue with the way sfPHPView includes indexSuccess.
Could you simply
require
index.php in the default/index action?