سؤال

I'm trying to use phpBB3 (forum app) along with ZF2. For that, I have to include a file from the phpBB3. In theory this is as simple as:

include('/path/to/phpbb3/common.php');
$user->session_begin(); //$user is defined in common.php file

In common.php a lot of globals are defined, and after that are required some files which are using those globals. In ZF2 simply including the common.php would not work, because the scope of the globals will not span over the required files, so I tried a little trick:

//in Application/Forum/Service

public function callForumAPI(){
    $zf_dir = getcwd();
    chdir('/var/www/html/phpBB3');

    include('common.php');
    $user->session_begin();

    chdir($zf_dir);
}

Neither in this case the scope of the global variables didn't span over the required files, so all the globals where NULL in those files. How could I solve this issue?

هل كانت مفيدة؟

المحلول

I consider 2 main problems:

1. Loading resources

I dont know if you changed the code of phpBB3, since if you dont, your problem is other.

Phpbb3, as many systems, doesnt let you access directly to any file, you have to go through index.php. As you can see in common.php

if (!defined('IN_PHPBB'))

{
    exit;
}

IN_PHPBB is defined in index.php, so you can simply use

Also, common.php and other files, makes use of $phpbb_root_path, that is defined in index.php. So, at least, when you are going to include common.php you need

 $zf_dir = getcwd();
chdir('/var/www/html/phpBB3');

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';

include('common.php');
...

chdir($zf_dir);

probably there are some other things you have to take care about.

2. Variable scopes

Also, consider than in PHP, like in almost every language, a variable declared inside a function, is considered local, and will be undefined outside that function. So for sure, if you do that inside callForumAPI(), you wont have any variable outside, and moreover, depending on where you are doing that includes...it could be actually inside a function, no matter you can notice it or not, since ZF2 is a framenwork with a complex, non-obvius architecture.

So, what i recomend, as soon as you load the file, is to use the ZF2 service manager to store all the variables and object than you would use in your application. This is a good measure even if you didnt need it,since this way you can have everything integrated as much as possible, it is important to minimize and localize access to phpbb3, since it is not meant to be a library, maintenance could be tricky, so if everyhing is in the same file, and then you create your own internal api through the service manager, it will more encapsulated and nicer. I assume you already know how to do this, if you dont, just let me know.

try this, and tell me if its enough or we need more research

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top