在 Kohana 3 中将属性从父控制器传递到子控制器

发布于 2024-10-18 21:08:35 字数 1513 浏览 8 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔书几行 2024-10-25 21:08:36

IMO,最好为 FB 类创建 Kohana 包装器,然后在控制器中使用它。像这样:

$this->facebook = Facebook::instance(); // wrapper will automatically load config with appId etc
$this->session = $this->facebook->get_session();

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:

$this->facebook = Facebook::instance(); // wrapper will automatically load config with appId etc
$this->session = $this->facebook->get_session();

PS. May be Kohana already has this implementation? Something like https://github.com/zombor/Kohana-Facebook.

七月上 2024-10-25 21:08:35

不要盲目地不要重载__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 the before() method. Be sure to call parent::before() after you are done.

And that global stuff is totally unnecessary. Just set $this->facebook and then access it later with $this->facebook.

信仰 2024-10-25 21:08:35

你把 __construct 拼写错了。

此外,如果您将其设置为类属性,则无需访问全局。

只需使用以下命令访问它

$this->facebook

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

$this->facebook
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文