使用 Kohana/MVC 框架访问模型内​​的控制器方法

发布于 2024-09-05 00:42:50 字数 371 浏览 3 评论 0原文

需要能够使用 Kohana V2.3 框架从模型访问控制器方法。目前,我正在将控制器对象(通过参考)传递给创建时的模型,该模型工作得很好,但我不禁认为有一种更“干净”的方法 - 有人有任何建议吗? Kohana V3 会通过其 HMVC 模式解决这个问题吗?

这可能会有所帮助: http://www .ifc0nfig.com/accessing-the-calling-controller-in-a-model-within-kohana/

I need to able to access controller methods from a model using the Kohana V2.3 framework. At the moment I'm passing the controller object (by ref.) to the model on creation which works perfectly fine but I can't help think there is a more "cleaner" way - does anybody have any suggestions? Would Kohana V3 resolve this with its HMVC pattern?

This may help: http://www.ifc0nfig.com/accessing-the-calling-controller-in-a-model-within-kohana/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

这个俗人 2024-09-12 00:42:50

为什么模型中需要控制器?这违反了 MVC 的概念,因为现在模型(数据层)依赖于控制器层。

您的模型在控制器中所需的功能应卸载到通用库中。然后可以从控制器和模型进行访问。

Why do you need the controller inside the model? This violates the concept of MVC because now the model (data layer) is dependent on the Controller layer.

The functions your model needs in your controller should be offloaded to a generic library. Then accessible from both the controller and model.

剪不断理还乱 2024-09-12 00:42:50

您正在控制器中实例化 Facebook 连接器,并且您不想执行两次,因此您希望模型能够访问它是有道理的。到目前为止,一切都很好。有两种方法可以做到这一点。

1) 创建一个库,其中包含包装 Facebook 实例的单例实例。看起来像这样:

// libraries/FacebookApi.php
class FacebookApi {
  // this stores the singleton FacebookApi
  protected static $instance = null;

  // this is the singleton's Facebook instance
  protected $facebook = null;

  // Return the single FacebookApi wrapper
  public static function instance() {
    if (!isset(FacebookApi::$instance)) {
      self::$instance = new FacebookApi();
    }
    return $instance;
  }

  // Construct a single FacebookApi containing a single Facebook
  protected function __construct() {
    $this->facebook = new Facebook(  
      Kohana::config('mysubs.facebook_apikey'),  
      Kohana::config('mysubs.facebook_secret')  
    );  
  }
}

您的控制器和模型都将像这样访问它:

$facebook = FacebookApi::instance()->facebook;
$facebook->require_login(); // etc

Kohana 2.x 的示例代码:
http://dev.kohanaframework.org/ items/kohana2/repository/entry/trunk/system/libraries/Database.php

2) 由于除了访问 Facebook 类之外,您可能不会对实际库执行任何操作,因此只需创建一个简单的帮助程序包装一个单例:

// helpers/facebook_api.php
class facebook_api {
  static $facebook = null;

  static function instance() {
    if (!self::$facebook) {
      self::$facebook = new Facebook(  
        Kohana::config('mysubs.facebook_apikey'),  
        Kohana::config('mysubs.facebook_secret')  
      );
    }
    return self::$facebook;
  }
}

您的控制器和模型都将像这样访问它:

$facebook = facebook_api::instance();
$facebook->require_login(); // etc

You're instantiating the Facebook connector in the controller, and you don't want to do that twice so it makes sense that you'd want the model to have access to it. So far so good. There are two ways to do this.

1) Create a library which has a singleton instance that wraps the Facebook instance. That would look like this:

// libraries/FacebookApi.php
class FacebookApi {
  // this stores the singleton FacebookApi
  protected static $instance = null;

  // this is the singleton's Facebook instance
  protected $facebook = null;

  // Return the single FacebookApi wrapper
  public static function instance() {
    if (!isset(FacebookApi::$instance)) {
      self::$instance = new FacebookApi();
    }
    return $instance;
  }

  // Construct a single FacebookApi containing a single Facebook
  protected function __construct() {
    $this->facebook = new Facebook(  
      Kohana::config('mysubs.facebook_apikey'),  
      Kohana::config('mysubs.facebook_secret')  
    );  
  }
}

both your controller and your model would access it like this:

$facebook = FacebookApi::instance()->facebook;
$facebook->require_login(); // etc

Example code from Kohana 2.x:
http://dev.kohanaframework.org/projects/kohana2/repository/entry/trunk/system/libraries/Database.php

2) Since you're probably not going to do anything with an actual library other than just access the Facebook class, just create a simple helper that wraps a singleton:

// helpers/facebook_api.php
class facebook_api {
  static $facebook = null;

  static function instance() {
    if (!self::$facebook) {
      self::$facebook = new Facebook(  
        Kohana::config('mysubs.facebook_apikey'),  
        Kohana::config('mysubs.facebook_secret')  
      );
    }
    return self::$facebook;
  }
}

both your controller and your model would access it like this:

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