Cakephp:appController 中的变量数据在 Xcontroller 子级中不一致
我正在使用 Cakephp (2.X) 框架。
我创建了一个 Facebook 画布应用程序。我将 facebook 对象作为 appController 中的变量。我在构造函数中实例化它。在我的 usersController 中,我尝试使用这个对象。该对象就在那里,尽管 facebook 对象中的所有数据并非都是持久的。
例如,如果我在 appController 中 var dump facebook 对象,则所有属性都存在。当我将 facebook 对象转储到其他控制器中时,某些属性为 null/0/不存在。例如“user”、“signedRequest”和“accessToken”。
目前,我的文件如下所示:
appController.php
class AppController extends AppController {
public $facebook;
//other vars here
function __construct($request = null, $response = null) {
parent::__construct($request, $response);
App::import('Vendor','fb/src/facebook');
...
$this->facebook = new Facebook(//array with appId and Secret);
// doing stuff here with $this->facebook
}
//other methods
并且 facebook 对象在此类
UsersController.php
class UsersController extends Controller {
//some functions
function useParentsFBvar(){
//I have tried
$this->facebook;
//and I have tried
parent::facebook;
}
中的每个方法中都已完全定义。 上述两者都不是完全定义的 facebook 对象。特别是“user”、“signedRequest”和“accessToken”。
我什至尝试在 appController 中实现一个函数并从我的 UsersController 调用
function getFB() {
return $this->facebook;
}
但我遇到了同样的问题。
我在这里做错了什么明显的事情吗?
哦,这是我的第一个问题,我对 cakephp 和 PHP 很陌生,所以请对我宽容一点。
如果您需要任何其他信息,请告诉我。我搜索了类似的问题,但没有找到任何合适的...抱歉,如果这是重复的。
- - - - - - - - - - - - 编辑 - - - - - - - - - - - - - -
这就是我为了让它按我想要的方式工作而所做的。我所做的事情的原因有人可以确认这是否会破坏任何模式或者这是否可以接受 MVC
我所做的是将 appController 中的 facebook 对象转换为静态成员。然后,在检查它是否已经被新建之后,我在构造函数中对其进行新建。如果它已经是新的,我什么也不做。一旦我进行了此更改,它就会在 UsersController 中创建 facebook 对象。
< appController.php >
App::uses('Controller', 'Controller');
App::import('Vendor','fb/src/facebook');
class AppController extends Controller {
public static $facebook = null;
//other members, components and helpers
function __construct($request = null, $response = null){
parent::__construct($request,$response);
//load the configurations for app
if(!static::$facebook){
static::$facebook = new Facebook(
array('appId' => $this->app_id,
'secret' => $this->secret,
'cookie'=> true));
}
}
function beforeFilter() {
//get the user and do some things with static::facebook
}
然后在 UserController.php $this->facebook 中给了我一个有效的对象。一旦我开始工作,我认为将逻辑转移到用户模型中是有意义的,因此我创建了一个模型函数来返回使用该应用程序的 X 个朋友。
<---------------UsersController.php------------------>
class UsersController extends AppController {
public $name = 'Users';
public function friends($count = 5) {
$this->set('friends', $this->User->getFriendsUsingApp($this->facebook, $count));
}
}
在
<-------------------User.php--------------------------->
class User extends AppModel {
public function getFriendsUsingApp($facebook, $max = 5) {
$installedFriends = array();
try {
$installedFriends = $facebook->api('/me/friends?fields=installed');
} catch (FacebookApiException $e) {
$this->log($e->getMessage());
}
//search through the array of all returned friends and search for "ionstalled" field
$friends = array();
foreach ($installedFriends['data'] as $friend) {
if (array_key_exists('installed',$friend) && $friend['installed']) {
$friends[] = $friend;
}
}
return $friends;
}
}
This 中工作得很好,我觉得通过将更多的逻辑移入模型中(尽管不确定)来遵守 MVC,请告诉我我是否正确。
我认为问题在于,当 appController 首次构造时,它会创建一个新的 facebook 对象(包含所有有效数据 [accessToken 和 user])。然后,当我的 userController 实例被构造时,构造了一个新的 appController ,从而创建了另一个新的 facebook 对象。不知何故,当第二次调用 new Facebook(...) 时,它返回某种类型的仅部分有效的对象(appId 和 Secret 现在都是数组,并且没有 accessToken 或 User。所以我将 facebook 设为静态,现在全部控制器共享 facebook 对象的相同副本。
<-----facebook object dumped in AppController--------->
(
[appId:protected] => //appId//
[appSecret:protected] => //secret//
[user:protected] => //an actual userID//
[signedRequest:protected] =>
[state:protected] => //state//
[accessToken:protected] => //an actual accessToken//
[fileUploadSupport:protected] =>
)
<-----facebook object on dump in UsersController--------->
(
[appId:protected] => array(
[0] => //appId//
[1] => //appId//
)
[appSecret:protected] => array(
[0] => //secret//
[1] => //secret//
)
[user:protected] => array|array
[signedRequest:protected] =>
[state:protected] => //state//
[accessToken:protected] => array|array -//though sometimes just blank
[fileUploadSupport:protected] =>
)
希望这是有道理的。
I am using the Cakephp (2.X) framework.
I have created a facebook canvas app. I have my facebook object as a variable in appController. I instantiate it in the constructor. In my usersController I am trying to use this object. The object is there, though not all of the data within the facebook object is persistent.
For instance if I var dump facebook object in appController all of the properties are present. When I dump the facebook object in my other controller some of the properties are null/0/not present. For instance 'user', 'signedRequest' and 'accessToken'.
Currently this is what my files look like:
appController.php
class AppController extends AppController {
public $facebook;
//other vars here
function __construct($request = null, $response = null) {
parent::__construct($request, $response);
App::import('Vendor','fb/src/facebook');
...
$this->facebook = new Facebook(//array with appId and Secret);
// doing stuff here with $this->facebook
}
//other methods
And the facebook object is fully defined in every method in this class
UsersController.php
class UsersController extends Controller {
//some functions
function useParentsFBvar(){
//I have tried
$this->facebook;
//and I have tried
parent::facebook;
}
Both of the above are not fully defined facebook objects. Specifically 'user', 'signedRequest', and 'accessToken'.
I have even tried implementing a function in appController and calling from my UsersController
function getFB() {
return $this->facebook;
}
But I get the same issue.
Is there something obvious that I am doing wrong here?
Oh and this is my first question and I am very new to cakephp and PHP so please be easy on me.
If you need any other info please let me know. I searched for similar questions but did not find any suitable...sorry if this is a repeat.
------------------------EDIT--------------------------
This is what I've done to get it to work as I wanted. And the reasons I did things Can someone confirm whether this breaks any patterns or if this is acceptable MVC
What I did was convert facebook object in appController to a static member. I then new it in the constructor after checking to see if it has already been new'd. If it has already been new'd I do nothing. once I made this change it made the facebook object in UsersController.
< appController.php >
App::uses('Controller', 'Controller');
App::import('Vendor','fb/src/facebook');
class AppController extends Controller {
public static $facebook = null;
//other members, components and helpers
function __construct($request = null, $response = null){
parent::__construct($request,$response);
//load the configurations for app
if(!static::$facebook){
static::$facebook = new Facebook(
array('appId' => $this->app_id,
'secret' => $this->secret,
'cookie'=> true));
}
}
function beforeFilter() {
//get the user and do some things with static::facebook
}
Then in UserController.php $this->facebook gave me a valid object. Once I got that working I figured that it made sense to move the logic into the user model so I created a model function to return X number of friends using the app.
<---------------UsersController.php------------------>
class UsersController extends AppController {
public $name = 'Users';
public function friends($count = 5) {
$this->set('friends', $this->User->getFriendsUsingApp($this->facebook, $count));
}
}
and in
<-------------------User.php--------------------------->
class User extends AppModel {
public function getFriendsUsingApp($facebook, $max = 5) {
$installedFriends = array();
try {
$installedFriends = $facebook->api('/me/friends?fields=installed');
} catch (FacebookApiException $e) {
$this->log($e->getMessage());
}
//search through the array of all returned friends and search for "ionstalled" field
$friends = array();
foreach ($installedFriends['data'] as $friend) {
if (array_key_exists('installed',$friend) && $friend['installed']) {
$friends[] = $friend;
}
}
return $friends;
}
}
This has been working fine and I feel abides by MVC by moving some more of the logic into the model (though uncertain) please tell me if I am correct.
I think the problem was that when appController is first constructed it creates a new facebook object (with all valid data [accessToken and user]). Then when my userController instance was being constructed a new appController was constructed thus creating another new facebook object. Somehow when the second call to new Facebook(...) was called it was returning some type of only partially valid object (appId and secret were now each arrays, and there was no accessToken or User. So I made facebook static and now all controllers share the same copy of the facebook object.
<-----facebook object dumped in AppController--------->
(
[appId:protected] => //appId//
[appSecret:protected] => //secret//
[user:protected] => //an actual userID//
[signedRequest:protected] =>
[state:protected] => //state//
[accessToken:protected] => //an actual accessToken//
[fileUploadSupport:protected] =>
)
<-----facebook object on dump in UsersController--------->
(
[appId:protected] => array(
[0] => //appId//
[1] => //appId//
)
[appSecret:protected] => array(
[0] => //secret//
[1] => //secret//
)
[user:protected] => array|array
[signedRequest:protected] =>
[state:protected] => //state//
[accessToken:protected] => array|array -//though sometimes just blank
[fileUploadSupport:protected] =>
)
Hope this makes sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
beforeFilter 方法在任何操作之前被调用。
确保您的 facebook 类位于 /App/Vendor/facebook 文件夹中。
Facebook 实例现在应该可以通过任何控制器中的 $this->facebook 进行访问。
如果您在另一个控制器中使用 beforeFilter 方法,请确保调用
另外您的用户控制器应该如下所示:
祝您好运
Try this:
The beforeFilter method get's invoked before any action.
Make sure your facebook classes are in the /App/Vendor/facebook folder.
Facebook instance should now be accessable via $this->facebook in any controller.
If you use the beforeFilter method in another controller make sure to call
Also your user controller should look like this:
good luck