Symfony2 服务是否始终需要在捆绑包中声明?

发布于 2025-01-03 19:45:53 字数 1246 浏览 3 评论 0原文

在 Symfony2 中,服务被定义为[强调我的]:

服务是任何执行某项操作的 PHP 对象的通用术语 具体任务。服务通常“全局”使用,例如 数据库连接对象或传递电子邮件消息的对象。 在 Symfony2 中,服务通常是从 服务容器。具有许多解耦服务的应用程序是 据说遵循面向服务的架构。

以“全局”为关键词,我看到的所有有关如何定义服务的示例都在现有捆绑包中声明了该服务吗?这是 MartinSikora.com 的示例,

<?php
    // Bundle/HelloBundle/Services/MyService.php
    namespace Bundle\HelloBundle\Services;

    class MyService {

        public function sum($n1, $n2) {
            return $n1 + $n2;
        }

    }
?>

然后他使用从 Hello 控制器中进行:

<?php
// Bundle/HelloBundle/Controller/HelloController.php
namespace Bundle\HelloBundle\Controller;

class HelloController extends Controller {

    public function indexAction() {
        $number = $this->get('my_service')->sum(12, 37);
        // this returns 49

        /*
        ...
        */
    }
}
?>

注意他的示例服务是如何在标有“Services”的文件夹中的 HelloBundle 包中声明的。如果“Services”文件夹存储在任何特定捆绑包之外的一个或多个更高级别,不是更好吗?因为服务是要在整个应用程序中使用的?

  • 这里的最佳实践是什么?
  • 该服务是否仅适用于 HelloBundle Bundle 内的控制器?
  • 为什么通常这样做?

In Symfony2, a service is defined as [emphasis mine]:

A Service is a generic term for any PHP object that performs a
specific task. A service is usually used "globally", such as a
database connection object or an object that delivers email messages.
In Symfony2, services are often configured and retrieved from the
service container. An application that has many decoupled services is
said to follow a service-oriented architecture.

With "globally" being the key word, all the examples I see of how to define a service have the service declared within an existing bundle? Here's an example from MartinSikora.com

<?php
    // Bundle/HelloBundle/Services/MyService.php
    namespace Bundle\HelloBundle\Services;

    class MyService {

        public function sum($n1, $n2) {
            return $n1 + $n2;
        }

    }
?>

He then uses it from within the Hello Controller:

<?php
// Bundle/HelloBundle/Controller/HelloController.php
namespace Bundle\HelloBundle\Controller;

class HelloController extends Controller {

    public function indexAction() {
        $number = $this->get('my_service')->sum(12, 37);
        // this returns 49

        /*
        ...
        */
    }
}
?>

Notice how his example service is declared inside of the HelloBundle bundle in a folder labeled "Services". Wouldn't it be better if the "Services" folder were stored one or more levels higher outside of any specific bundle since services are meant to be used across the entire application?

  • What's the best practice here?
  • Is the service only available to controllers within the HelloBundle Bundle?
  • Why is it usually done this way?

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

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

发布评论

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

评论(1

满意归宿 2025-01-10 19:45:53

简短回答
不,服务不需要在捆绑包中定义。实际的服务类可能存在于捆绑包中,也可能不存在于捆绑包中 - 它们可能存在于供应商库文件夹中(或与此相关的其他任何地方)。服务定义虽然通常在捆绑包的 Resources\config\services.yml 中定义,但也可以在 services 下的 app/config.yml 中定义代码>键。

服务只是一个已在 Symfony 的依赖注入容器中注册的类。类本身可以存在于任何地方。

长答案
了解 Symfony2 中捆绑包的用途非常重要。您的项目的大部分(主要例外是第 3 方库)都是由捆绑包组成的。有些人喜欢将所有内容放在一个巨大的捆绑包中,但我更喜欢将它们用作特定功能的容器(即:用户管理、博客文章管理、资产管理)。

因为捆绑包应该代表一项功能,所以这是有意义的定义捆绑包内的一些服务。例如,在 BlogPostBundle 中定义 BlogPostEntityService 类是很自然的。现在,仅仅因为服务包含在捆绑包中,并不会降低其全局性。如果我将我的服务注册为 blog_bundle.blog_post_entity_service,我仍然可以从任何其他捆绑包访问它。

在 Martin 的示例中,MyService 是一个模糊的示例 - 它不像 BlogPostEntityService 那样具体。但是,您可能会创建实用程序服务(即ArrayUtilService),在这种情况下,您可能希望创建UtilBundle 并将服务存储在其中。

这里的最佳实践是什么?

没有明确的答案 - 由您决定在哪里存储服务类。问问自己该服务是否直接处理您的捆绑包所代表的功能 - 如果是这样,它可能属于该捆绑包。如果您计划共享或重复使用您的捆绑包,这也很有意义。

该服务是否仅适用于 HelloBundle 内的控制器
捆绑?

不可以。您可以从任何控制器(或任何容器感知类)中调用该服务。您还可以将 MyService 注入任何其他服务。

Short answer
No, services do not need to be defined within a bundle. The actual service class may or may not live within a bundle - they may live in a vendor library folder (or anywhere else for that matter). And service definitions, while commonly defined within a bundle's Resources\config\services.yml, can also be defined in app/config.yml under a services key.

A service is simply a class that has been registered with Symfony's dependency injection container. The class itself can live anywhere.

Long answer
It's important to understand the purpose of bundles in Symfony2. The majority of your project (with the main exception being 3rd party libraries) is comprised of bundles. Some people like to put everything in one giant bundle, but I prefer to use them as containers for specific pieces of functionality (ie: User management, Blog Post management, Asset management.)

Because bundles should represent a piece of functionality, it makes sense to define some services within bundles. For example, it's natural to define a BlogPostEntityService class in a BlogPostBundle. Now, just because the service is contained within a bundle, doesn't make it any less global. If I register my service as blog_bundle.blog_post_entity_service, I can still access it from any other bundle.

In Martin's example, MyService is a vague example - it isn't specific like BlogPostEntityService. However, it is likely that you'll create utility services (ie ArrayUtilService), in which case you might want to create a UtilBundle and store the service there.

What's the best practice here?

There's no definitive answer - it's up to you to decide where to store service classes. Ask yourself if the service deals directly with the piece of functionality your bundle represents - if so, it likely belongs in the bundle. This also makes sense if you plan on sharing or reusing your bundle.

Is the service only available to controllers within the HelloBundle
Bundle?

No. You can call the service from within any controller (or any container aware class). You can also inject MyService into any other service.

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