如何转换现有的PHP库文件以便在CakePHP框架中使用?
我有一个PHP非蛋糕格式的库,这是一种常用的PHP脚本,目前非常有效。我需要在蛋糕框架中使用它。库文件如下所示:(示例已提取)如何转换现有的PHP库文件以便在CakePHP框架中使用?,php,rest,cakephp,datasource,cakephp-1.3,Php,Rest,Cakephp,Datasource,Cakephp 1.3,我有一个PHP非蛋糕格式的库,这是一种常用的PHP脚本,目前非常有效。我需要在蛋糕框架中使用它。库文件如下所示:(示例已提取) view操作触发send_操作功能(每次用户单击foods controller上的view页面时)。因此,每次用户单击view action,his(动态变量):userid,sessionid,该页面的itemid,url,itemdescription;(时间范围值是一个静态字符串值“ALL”),如果有(等),到目前为止,只有这些值可用:将用作发送操作函数中的“
- view操作触发send_操作功能(每次用户单击foods controller上的view页面时)。因此,每次用户单击view action,his(动态变量):userid,sessionid,该页面的itemid,url,itemdescription;(时间范围值是一个静态字符串值“ALL”),如果有(等),到目前为止,只有这些值可用:将用作发送操作函数中的“参数”。您编写的代码与代码的功能非常接近。你说得对。除了我们应该在控制器的view()中包含Send Action函数之外
- 如果我们看一下动态填充上面提到的变量,您是否可以修改您的第二个代码(例如,来自产品控制器的代码),以便它也可以动态接收变量?(正如您在上次更新中所问:如何获取参数…)
- 我只是想说清楚
- 用户可以查看页面。发送操作收集数据并发送到API。(正如我们通过调用库中的函数(ACME.php)所做的那样)。*如果可能,请等待您的更新,谢谢
- 在foods控制器的函数view()中:还有一个附加调用。第二个调用是:
$recommendResponse=GetRecommends(“OtherUsersView”,$itemId,$userId);
第二个调用调用ACME.php库文件,其中包含检索数据的(2)个函数,如下所示:(它处于工作状态,但只需要像(1)个函数一样更改为公共静态函数。您能帮助修改此代码吗?:
function getRecommendations($recommendationType, $itemId, $userId){
// sample code similar to the first one.
}
仅此而已。它在普通PHP格式中似乎非常简单,而且工作起来也很容易,但在MVC框架中使用它对一些人来说有点挑战,对我来说很多。感谢您的帮助,Lionel.:-)
p.S.嗨,莱昂内尔,我注意到图书馆在更改后丢失了一些东西?看,原来我们有:
$somewebsiteAPI=$someapiwebsiteURL.$action.“?apikey=“.$apikey.
看,$SomeWebsiteAPI
和$SomeApiWebsiteURL
的变量是不同的。是我遗漏了什么吗?还是你修改了一些,所以效率更高?我看到名为$SomeWebsiteAPI
的变量被修改为名为$link
的变量,而变量$SomeApiWebsiteURL
是已设置为命名变量,$url
,对吗?。谢谢
谢谢,向你问好,约翰·马克西姆
对我来说,如果我有这段代码,我会首先将其包装到一个静态(或普通)类中,并将其命名为ACME,然后将ACME.php移动到
/apps/libs/ACME.php
。然后在控制器中,我将使用App::import('Lib',ACME'))
。此操作只需要文件,所以您可以通过调用ACME::sendAction(…)
立即使用它
关于global
这件事,您可能只需要声明一个静态(或普通)类,然后将共享变量定义为类属性的一部分,这样您就可以在类中的所有函数之间共享它们
例如,这是/app/libs/acme.php
class ACME {
private static $someapiwebsiteURL = "http://thewebsite/api/1.0/";
private static $apiKey = "0010KIUMLA0PLQA665JJ";
private static $tenantId = "THE_TENANT_NAME";
/**
* Simple builder to build links from array of $params
*
* @param string $url The api url
* @param array $params The given parameters
* @return string built url
*/
private static function BuildLink($url="", $params=array()) {
$link = $url;
foreach($params as $k=>$v) {
$link .= "&$k=$v";
}
//Replace the first & to ?
$link = preg_replace("/&/", "?", $link, 1);
//Not sure if we need URL encode here, please uncomment this
//if the API could not work.
//$link = urlencode($link);
return $link;
}
public static function SendAction($action, $itemId, $itemdescription, $itemurl, $itemimageurl, $sessionid, $userid, $rating="") {
$somewebsiteAPI = self::BuildLink(self::$someapiwebsiteURL.$action, array(
"apikey"=>self::$apiKey,
"sessionid"=>$sessionid,
"userid"=>$userid,
"tenantid"=>self::$tenantId,
"itemid"=>$itemId,
"itemdescription"=>$itemdescription,
"itemurl"=>$itemurl,
"itemimageurl"=>$itemimageurl,
/**
* Assuming your API smart enough to only use this value when
* the action is "rate"
*/
"ratingvalue"=>$rating
));
$xml = simplexml_load_file($somewebsiteAPI);
return $xml;
}
public static function GetRecommendations($recommendationType, $itemId, $userId) {
$somewebsiteAPI = self::BuildLink(self::$someapiwebsiteURL.$recommendationType, array(
'apikey'=>self::$apiKey,
'tenantid'=>self::$tenantId,
'itemid'=>$itemId,
'userid'=>$userId
));
$xml = simplexml_load_file($somewebsiteAPI);
return $xml;
}
}
在你的控制器里
App::import('Lib', 'acme');
class FoodController extends AppController {
//Food is plural already I assume? You can just use
//food, should be ok I think, else it will be weird
//to use /foods/view/?
var $name = "Food";
var $uses = array("Item", "Food");
function view($id="") {
//We accepts only valid $id and $id > 0.
//Take notes that this $id will be a string, not int.
if (ctype_digit($id) && $id > 0) {
//I don't know how you would gather the information, but I assume you
//have a database with the information ready.
//I assumed you have an `items` table
$item = $this->Item->findById($id);
$sessionid = "00988PPLO899223NHQQFA069F5434DB7EC2E34"; //$this->Session->...?
$timeRange = "ALL";
$userid = "24EH1725550099LLAOP3"; //$this->Auth->user('id')?
if (!empty($item)) {
$desc = $item['Item']['description'];
$url = "/foods/view/".$id;
$img = $item['Item']['img'];
$viewResponse = ACME::SendAction("view", $id, $desc ,$url, $img, $sessionid, $userid);
$this->set('food', $this->Food->read(null, $id));
}else{
$this->Session->setFlash(__('Invalid food', true));
$this->redirect(array('action' => 'index'));
}
}else{
$this->Session->setFlash(__('Invalid food', true));
$this->redirect(array('action' => 'index'));
}
}
}
编辑
代码已经填写完毕,当然,没有任何担保:)。我个人并不喜欢函数中有长参数(如SendAction
,error prune),而是使用较短的参数,如ACME::BuildLink
中的$params
。但是为了尊重您的代码,我没有对SendAction
方法做太多修改
然后,我不太确定您将如何使用此代码,因此我假设您有一个ProductsController
,用户会以某种方式触发url,如/products/send\u action/
。如果你能提供更多的信息,那么我们将能够提供帮助
再次编辑
我修改了ACME类以及控制器。是的,我确实遗漏了一些变量,但我已经将它们添加回更新的代码中
不太确定它是否会工作(可能是打字错误),如果代码不适合您,您可以修改代码
对于个人约定,我通常将静态方法大写,如ACME:GetRecommendations
或ACME::SendAction
哦,是的,我最好还是回到你使用的变量上来。很抱歉修改它们,只是我不喜欢长名称:)
顺便说一句,路行者的ACME公司?哈哈
干杯
Lionel对我来说,如果我有这段代码,我会首先将其包装到一个静态(或普通)类中,并将其命名为ACME,然后将ACME.php移动到
/apps/libs/ACME.php
。然后在控制器中,我将使用App::import('Lib','acme')
。此操作只需要文件,所以您可以通过调用ACME::sendAction(…)
立即使用它
关于global
这件事,您可能只需要声明一个静态(或普通)类,然后将共享变量定义为类属性的一部分,这样您就可以在类中的所有函数之间共享它们
例如,这是/app/libs/acme.php
class ACME {
private static $someapiwebsiteURL = "http://thewebsite/api/1.0/";
private static $apiKey = "0010KIUMLA0PLQA665JJ";
private static $tenantId = "THE_TENANT_NAME";
/**
* Simple builder to build links from array of $params
*
* @param string $url The api url
* @param array $params The given parameters
* @return string built url
*/
private static function BuildLink($url="", $params=array()) {
$link = $url;
foreach($params as $k=>$v) {
$link .= "&$k=$v";
}
//Replace the first & to ?
$link = preg_replace("/&/", "?", $link, 1);
//Not sure if we need URL encode here, please uncomment this
//if the API could not work.
//$link = urlencode($link);
return $link;
}
public static function SendAction($action, $itemId, $itemdescription, $itemurl, $itemimageurl, $sessionid, $userid, $rating="") {
$somewebsiteAPI = self::BuildLink(self::$someapiwebsiteURL.$action, array(
"apikey"=>self::$apiKey,
"sessionid"=>$sessionid,
"userid"=>$userid,
"tenantid"=>self::$tenantId,
"itemid"=>$itemId,
"itemdescription"=>$itemdescription,
"itemurl"=>$itemurl,
"itemimageurl"=>$itemimageurl,
/**
* Assuming your API smart enough to only use this value when
* the action is "rate"
*/
"ratingvalue"=>$rating
));
$xml = simplexml_load_file($somewebsiteAPI);
return $xml;
}
public static function GetRecommendations($recommendationType, $itemId, $userId) {
$somewebsiteAPI = self::BuildLink(self::$someapiwebsiteURL.$recommendationType, array(
'apikey'=>self::$apiKey,
'tenantid'=>self::$tenantId,
'itemid'=>$itemId,
'userid'=>$userId
));
$xml = simplexml_load_file($somewebsiteAPI);
return $xml;
}
}
在你的控制器里
App::import('Lib', 'acme');
class FoodController extends AppController {
//Food is plural already I assume? You can just use
//food, should be ok I think, else it will be weird
//to use /foods/view/?
var $name = "Food";
var $uses = array("Item", "Food");
function view($id="") {
//We accepts only valid $id and $id > 0.
//Take notes that this $id will be a string, not int.
if (ctype_digit($id) && $id > 0) {
//I don't know how you would gather the information, but I assume you
//have a database with the information ready.
//I assumed you have an `items` table
$item = $this->Item->findById($id);
$sessionid = "00988PPLO899223NHQQFA069F5434DB7EC2E34"; //$this->Session->...?
$timeRange = "ALL";
$userid = "24EH1725550099LLAOP3"; //$this->Auth->user('id')?
if (!empty($item)) {
$desc = $item['Item']['description'];
$url = "/foods/view/".$id;
$img = $item['Item']['img'];
$viewResponse = ACME::SendAction("view", $id, $desc ,$url, $img, $sessionid, $userid);
$this->set('food', $this->Food->read(null, $id));
}else{
$this->Session->setFlash(__('Invalid food', true));
$this->redirect(array('action' => 'index'));
}
}else{
$this->Session->setFlash(__('Invalid food', true));
$this->redirect(array('action' => 'index'));
}
}
}
编辑
代码已经填写完毕,当然,没有任何担保:)。我个人并不喜欢函数中有长参数(如SendAction
,error prune),而是使用较短的参数,如ACME::BuildLink
中的$params
。但是为了尊重您的代码,我没有对SendAction
方法做太多修改
App::import('Lib', 'acme');
class FoodController extends AppController {
//Food is plural already I assume? You can just use
//food, should be ok I think, else it will be weird
//to use /foods/view/?
var $name = "Food";
var $uses = array("Item", "Food");
function view($id="") {
//We accepts only valid $id and $id > 0.
//Take notes that this $id will be a string, not int.
if (ctype_digit($id) && $id > 0) {
//I don't know how you would gather the information, but I assume you
//have a database with the information ready.
//I assumed you have an `items` table
$item = $this->Item->findById($id);
$sessionid = "00988PPLO899223NHQQFA069F5434DB7EC2E34"; //$this->Session->...?
$timeRange = "ALL";
$userid = "24EH1725550099LLAOP3"; //$this->Auth->user('id')?
if (!empty($item)) {
$desc = $item['Item']['description'];
$url = "/foods/view/".$id;
$img = $item['Item']['img'];
$viewResponse = ACME::SendAction("view", $id, $desc ,$url, $img, $sessionid, $userid);
$this->set('food', $this->Food->read(null, $id));
}else{
$this->Session->setFlash(__('Invalid food', true));
$this->redirect(array('action' => 'index'));
}
}else{
$this->Session->setFlash(__('Invalid food', true));
$this->redirect(array('action' => 'index'));
}
}
}