在 Zend Framework 中启动对象?

发布于 2024-12-26 11:38:21 字数 197 浏览 1 评论 0原文

如何消除在每个控制器中写入 $object = new Application_Model_Database()

例如,对于文章控制器,我必须为每个控制器输入 $articles = new Application_Model_Articles() 。我应该将它放在查看器控制器、动作助手或任何其他方式下吗?

How can I eliminate to write $object = new Application_Model_Database() in every controller?

For example for an article controller, I have to type $articles = new Application_Model_Articles() for every controller. Should I put it under viewer controller, action helpers, or any other way?

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

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

发布评论

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

评论(5

拥抱我好吗 2025-01-02 11:38:21

您的问题几乎听起来像是 OOP 最佳实践问题,而不是 Zend 框架特定问题。无论我是否使用框架,也无论我选择什么框架,我都会根据可测试性创建新对象的时间和地点,以及我必须编写多少次 $object = new My_Random_Object();< /代码>。

特别针对 Zend Framework:我将在任何地方或几乎任何地方使用的对象是在 Bootstrap.php。这些对象通常包括数据库适配器、记录器、视图对象以及我可能使用的任何插件。为了跨应用程序访问这些属性,我将在适当的控制器中创建私有属性,并在控制器的 init() 方法中将对象分配给这些属性。

class ExampleController extends Zend_Controller_Action
{

    public function init()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        $this->_db = $bootstrap->getResource('db');
        $this->_log = $bootstrap->getResource('log');
        // and so on, and so forth
    }

}

理想情况下,模型、服务、DAO 等都将按控制器和操作相对紧密地分组。根据我的经验,一般来说,如果我的应用程序中的所有控制器都显示相同的模型或服务类,那么我就会遇到组织问题。话虽这么说,任何仅出现在一个操作中的模型都会在该操作中创建。如果它跨控制器中的操作,则会在 init() 方法中创建它并分配给属性。如果它出现在多个控制器中,它将在我的 Bootstrap.php 中创建。

(理想情况下,所有内容都在 Bootstrap.php 中创建,因此您可以出于测试目的替换该引导程序。遗憾的是,我并不总是这样做,而且我最常使用上面概述的原则。)

Your question almost sounds like an OOP best practices question as opposed to a Zend Framework specific question. Regardless of whether or not I'm using a framework, and regardless of what framework I choose, I base when and where I create new objects on testability how many times I have to write $object = new My_Random_Object();.

Speaking specifically to the Zend Framework: Objects I'm going to use everywhere, or almost everywhere, get created in Bootstrap.php. These objects generally include a database adapter, logger, view object, and any plugins I might use. To access these across the application, I'll create private properties in the appropriate controllers and assign the objects to those properties in the controller's init() method.

class ExampleController extends Zend_Controller_Action
{

    public function init()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        $this->_db = $bootstrap->getResource('db');
        $this->_log = $bootstrap->getResource('log');
        // and so on, and so forth
    }

}

Ideally, models, services, daos, etc, will all be relatively tightly grouped by controller and by action. In my experience, and this is speaking generally, if I have the same model or service class showing up across all of the controllers in my application, I have an organization problem. That being said, any model that shows up in only one action gets created in that action. If it's across actions in a controller, it gets created in the init() method and assigned to a property. If it shows up across multiple controllers, it gets created in my Bootstrap.php.

(Ideally, everything gets created in the Bootstrap.php, so you can swap out that bootstrap for testing purposes. Sadly, I don't always do that, and I most often use the principles I outlined above.)

昇り龍 2025-01-02 11:38:21

那么你真的在每个控制器中都需要它吗?因为这几乎是设计使然。您可以在需要时实施模型。其实代码并不多。

现在,如果要在控制器的多个操作中使用它,您始终可以:

class MyController extends Zend_Controllers{
     $protected $_articleModel;

...

并在构造函数或 __init() 函数中初始化它,以便您可以通过 $this->_articleModel 在每个操作中使用它

如果您确实希望它在应用程序中的任何地方都可以使用,只需在引导程序中初始化它并将其存储在注册表中即可。

public function __initModels(){
  $articles = new Application_Model_Articles() 
  Zend_Registry::set('articles', $articles );
}

然后在你的控制器中访问它,如下所示:

Zend_Registry::get('articles')->fetchAll();

但是你仍然写了几个字符。

希望这有帮助!

Well do you really need it in every controllers? Because that's pretty much by design. You implement models when you need them. Its not that much code really.

Now if its to be used across actions from a controller you could always:

class MyController extends Zend_Controllers{
     $protected $_articleModel;

...

and in your constructor or __init() function initialize it so you can use it in every action thru $this->_articleModel

If you REALLY want it everywhere in your application just initialize it in your bootstrap and store it in the registry.

public function __initModels(){
  $articles = new Application_Model_Articles() 
  Zend_Registry::set('articles', $articles );
}

And access it in your controllers like so:

Zend_Registry::get('articles')->fetchAll();

But then your still writing a couple of characters.

Hope this help!

安稳善良 2025-01-02 11:38:21

如果你想在控制器中使用模型,你必须调用它..无论如何,这里有一些快捷方式

1.你可以在控制器的 init 部分初始化它,例如

public function init(){
   $this->object = new Application_Model_Database();
}

这样 this->object 是在该特定控制器的所有操作中可用

2.按照上述答案中的建议使用 Zend_registry

IF you want to use models in the controllers you must call it..anyway some shortcuts are here

1.You can initialize it in the init section of your controller like

public function init(){
   $this->object = new Application_Model_Database();
}

So that the this->object is available in all the actions of that particular controller

2.Use Zend_registry as suggested in the above answer

污味仙女 2025-01-02 11:38:21

另一种可能性是使用依赖注入容器,例如 Symfony DI 组件。它负责实例化您的对象,并且您可以获得一些额外的好处:

  • 关注点分离。您有一个专门用于创建对象树的组件。
  • 对象的可测试性更容易。
  • 最后但并非最不重要的一点是,延迟实例化带来的性能优势(仅当您请求时才创建对象)。因此,如果某个对象未被服务于您的请求的特定控制器使用,则它不会被实例化)。

它比上述解决方案要费力一些,但如果您将来需要维护和扩展您的应用程序,则要灵活得多。

希望有帮助,

Another possibility is to use a Dependency Injection container, such as the Symfony DI component. It takes care of instantiating your objects, and you get some additional benefits:

  • Separation of concerns. You have a component devoted to create your object tree.
  • Easier testability of the objects.
  • Last, but not least, the performance benefits given by lazy instantiation (objects are created only when you ask for them). Thus, if some object is not used by the particular controller serving your request, it's not instantiated).

It's a bit more laborious than the above solutions, but much more flexible if you need to maintain and extend your application in the future.

Hope that helps,

把人绕傻吧 2025-01-02 11:38:21

如果您使用此对象仅在视图中显示数据,并使用控制器来获取数据并将其分配给您的视图,如下所示:

//someControllerAction
$object = new Application_Model_Articles();
$object->fetchAll();
//assign to view
$this->view->articles = $object;

您可能最好制作一个类似于以下内容的视图助手:

//Articles.php put in /application/views/helpers  
class Zend_View_Helper_Articles extends Zend_View_Helper_Abstract {

public function Articles() {

$articles = new Application_Model_Articles();
$articles->fetchAll();
//return rowset object
return $articles;

然后在您的视图中(phtml )你可以这样做:

//someView.phmtl
<?php $articles = $this->Articles(); ?>
<h1><?php echo $this->escape($articles->title); ?></h1>
<p><?php echo $this->escape($articles->body); ?></p>

如果你只需要显示模型中的数据,构建一个视图助手可以让你完全绕过控制器。这是一个非常简单的示例,可以与partials和partialLoops一起使用。
REF:ZF 参考自定义视图助手
ZF 部分视图助手参考

If you are using this object to just display data in your view and are using your controller to grab the data and assign it to your view, like so:

//someControllerAction
$object = new Application_Model_Articles();
$object->fetchAll();
//assign to view
$this->view->articles = $object;

You might be better off making a view helper similar to:

//Articles.php put in /application/views/helpers  
class Zend_View_Helper_Articles extends Zend_View_Helper_Abstract {

public function Articles() {

$articles = new Application_Model_Articles();
$articles->fetchAll();
//return rowset object
return $articles;

Then in your view (phtml) you could do something like:

//someView.phmtl
<?php $articles = $this->Articles(); ?>
<h1><?php echo $this->escape($articles->title); ?></h1>
<p><?php echo $this->escape($articles->body); ?></p>

building a view helper allows you to bypass the controller completely if you just need to display data from the model. This is a very simple example and can be used with partials and partialLoops.
REF:ZF reference Custom View Helper
ZF partial view helper reference

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