在 Kohana 3 中将属性从父控制器传递到子控制器
我正在使用 Kohana 3 并使用一个名为controller_Facebook 的中间控制器,该控制器从 Controller_Template 扩展,然后从 Controller_Facebook 扩展 Controller_Home。我在 Controller_Facebook 中设置两个属性,并尝试在 Controller_Home 中使用它们,但在那里不可用。它给出空值。我的代码类似于以下内容:
class Controller_Facebook extends Controller_Template{
public $template='template';
public $facebook;
public $session;
public function __contstruct(){
include_once(dirname(__FILE__)."/facebook_class.php");
global $facebook;
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
$this->facebook=$facebook;
$this->session = $facebook->getSession();
}
}
然后在 Home_Controller 中:
class Controller_Home extends Controller_Facebook{
public function __contstruct() {
parent::__contstruct();
}
public function action_index()
{
global $facebook;
$this->template->content=new View('home');
$this->template->selected='home'; var_dump($this->facebook);
$this->template->app_id='123';
var_dump($facebook);
$this->template->session=$this->session;
}
}
此外,如果我回显父类中的某些内容,那么它不会输出该内容。它在 facebook 中也工作,这是否意味着我的父构造函数正在工作?我认为它正在发挥作用。如果有人认为我做错了什么,请告诉我。
I am using Kohana 3 and using a middle controller named controller_Facebook that is extended from Controller_Template and then extending Controller_Home from Controller_Facebook. I am setting two properties in Controller_Facebook and trying to use them in Controller_Home but it not available there. It gives null value. My code is similar to following:
class Controller_Facebook extends Controller_Template{
public $template='template';
public $facebook;
public $session;
public function __contstruct(){
include_once(dirname(__FILE__)."/facebook_class.php");
global $facebook;
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
$this->facebook=$facebook;
$this->session = $facebook->getSession();
}
}
Then in Home_Controller :
class Controller_Home extends Controller_Facebook{
public function __contstruct() {
parent::__contstruct();
}
public function action_index()
{
global $facebook;
$this->template->content=new View('home');
$this->template->selected='home'; var_dump($this->facebook);
$this->template->app_id='123';
var_dump($facebook);
$this->template->session=$this->session;
}
}
Also if I echo some thing in parent class then it doesnot output that. It working in facebook also so does that mean that my parent constructor is working? I think it is working. Please tell me if some one think that I am doing some thing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IMO,最好为 FB 类创建 Kohana 包装器,然后在控制器中使用它。像这样:
PS。也许 Kohana 已经有了这个实现?类似于 https://github.com/zombor/Kohana-Facebook。
IMO, it will be better to create Kohana wrapper for FB class, and then use it in your controllers. Like this:
PS. May be Kohana already has this implementation? Something like https://github.com/zombor/Kohana-Facebook.
不要盲目地不要重载
__construct
,它会破坏你的控制器。您应该在before()
方法中执行此操作。完成后请务必调用parent::before()
。而那些
global
的东西是完全没有必要的。只需设置$this->facebook
,然后稍后使用$this->facebook
访问它。Do not overload
__construct
blindly, it will break your controllers. You should be doing this in thebefore()
method. Be sure to callparent::before()
after you are done.And that
global
stuff is totally unnecessary. Just set$this->facebook
and then access it later with$this->facebook
.你把 __construct 拼写错了。
此外,如果您将其设置为类属性,则无需访问全局。
只需使用以下命令访问它
You've spelt __construct wrong.
Also you don't need to access the global if you've set it as a class property.
Just access it with