简体   繁体   English

有没有一种方法可以使用PHP数据服务从Flex 4应用程序访问Joomla 1.5用户变量(例如用户ID)?

[英]Is there a way to access Joomla 1.5 user variables (like user id) from a Flex 4 application using a PHP Data Service?

I have written a script (in two files) that correctly displays a Joomla user id, like this: 我编写了一个脚本(在两个文件中),可以正确显示Joomla用户ID,如下所示:

//this is testy.php  
define( '_JEXEC', 1 );  
define('JPATH_BASE', dirname(__FILE__));  
define( 'DS', DIRECTORY_SEPARATOR );  
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );  
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );  
$mainframe =& JFactory::getApplication('site');  
$id = JFactory::getUser()->id;

The above file is located in the Joomla root folder. 上面的文件位于Joomla根文件夹中。 The other file is in a different directory and is as follows: 另一个文件位于另一个目录中,如下所示:

//this is testid.php  
include '../../joomla/testy.php';  
echo $id;

However, and here is the rub, when I change the "echo" to a "return" and put the second code snippet inside my Flex 4 Data Service script file, like this... 但是,碰巧的是,当我将“ echo”更改为“ return”并将第二个代码片段放入我的Flex 4 Data Service脚本文件中时,就像这样……

function getUserId() {  
  include '../../joomla/testy.php';  
  return $id;  
}

...I get a Flex error that says this: ...我收到一个Flex错误,内容如下:

Fatal error: Class 'JRequest' not found in /var/www/html/joomla/libraries/joomla    /import.php on line 33

I am extremely confused by this error and would appreciate any suggestions that the stackoverflow community may have. 我对这个错误感到非常困惑,并且非常感谢stackoverflow社区可能提出的任何建议。

Thanks so much! 非常感谢!

Zach 扎克

Greetings Rawan, 问候拉旺,

I did find a solution, but it's a bit hackish so I don't know if you'd want to use it. 我确实找到了一个解决方案,但是它有点破旧,所以我不知道您是否要使用它。

In your Flex application, add two things. 在您的Flex应用程序中,添加两件事。 First a method, that looks like this: 首先是一个方法,看起来像这样:

private function initVars():void {
   userid = FlexGlobals.topLevelApplication.parameters.id;
}

Then, add this line to the application header: 然后,将此行添加到应用程序标头中:

creationComplete="initVars()"

As in the original question, include the "testy.php" file in the Joomla directory. 与原始问题一样,在Joomla目录中包含“ testy.php”文件。 Upon creating a release build of the Flex app, open the wrapping html file for the app (located in the release build directory). 创建Flex应用程序的发行版后,请打开该应用程序的包装html文件(位于发行版目录中)。 Change this file's extension to ".php". 将此文件的扩展名更改为“ .php”。 Then in this newly created PHP file, add these lines in (NOTE: you may have to change your directory movement to fit your directory scheme): 然后在这个新创建的PHP文件中,添加以下行(注意:您可能必须更改目录移动以适合目录方案):

<?
include '../../joomla/testy.php';
idVar $id;
?>

In the release build file you will find this line: 在发行版本文件中,您将找到以下行:

var flashvars = {};

Flex allows us to include external variables in our app. Flex允许我们在应用程序中包含外部变量。 So, directly below this line add a new line, thusly: 因此,直接在此行下面添加新行,从而:

flashvars.id = "<?php echo $idVar; ?>";

We have now told our Flex app to include a flashvar with the name "id," and it's equal to the user ID we pulled from Joomla. 现在,我们告诉Flex应用程序包含一个名为“ id”的flashvar,它等于我们从Joomla中提取的用户ID。

In conclusion, you can now use the Flex variable "userid" (defined in the Flex method we wrote above) to do things like grab user data from your database. 总之,您现在可以使用Flex变量“ userid”(在我们上面编写的Flex方法中定义)来执行诸如从数据库中获取用户数据之类的操作。 I call this solution hackish because every time you create a new release build you must change the html wrapper to php and alter it like we did above. 我称此解决方案为骇人听闻的,因为每次创建新版本时,都必须像上面那样将html包装器更改为php并对其进行更改。

Hope this helps. 希望这可以帮助。

Cheers, 干杯,

Zach 扎克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM