简体   繁体   English

在鲁门放置业务逻辑?

[英]Where to put business logic in Lumen?

I am developing my first API with Lumen. 我正在用Lumen开发我的第一个API。 Normally I am using services for separating business logic or reused code from the controllers and share it with other controllers. 通常我使用服务来分离业务逻辑或从控制器重用代码并与其他控制器共享。

How to do this with lumen? 用流明怎么做? Where to put the services? 在哪里提供服务? I only see ServiceProviders to register these services but for me it's not clear where and how to define them. 我只看到ServiceProviders注册这些服务,但对我来说,目前尚不清楚在何处以及如何定义它们。

Lumen and its big brother Laravel come with a service container, which handles the dependencies injection. Lumen和它的大哥Laravel带有一个服务容器,它处理依赖注入。

To resolve things out of the container, you may either type-hint the dependency you need on a class that is already automatically resolved by the container, such as a route Closure, controller constructor, controller method, middleware, event listener, or queued job. 要解决容器外的问题,您可以在容器已自动解析的类上键入提示所需的依赖关系,例如路由Closure,控制器构造函数,控制器方法,中间件,事件侦听器或排队作业。 Or, you may use the app function from anywhere in your application: 或者,您可以在app任何位置使用app功能:

$instance = app(Something::class);

That's for "resolving things out". 这是为了“解决问题”。 Registering the "things" is what the service providers are for. 注册“事物”是服务提供商的用途。 A service provider is just a class that extends Illuminate\\Support\\ServiceProvider and binds interfaces or classes to concrete implementations. 服务提供者只是扩展Illuminate\\Support\\ServiceProvider并将接口或类绑定到具体实现的类。 (Read the docs for a detail on how to write your own.) (阅读文档以获取有关如何编写自己的详细信息。)


Example: Create some test route: 示例:创建一些测试路线:

$app->get('/test', 'TestController@test');

and create the controller method, type-hinting a parameter: 并创建控制器方法,键入提示参数:

public function test(DatabaseManager $dbm)
{
    dd($dbm);
}

You will see that the DatabaseManager interface is resolved to a concrete class, properly instantiated and configured with your DB config. 您将看到DatabaseManager接口已解析为具体类,已正确实例化并使用您的数据库配置进行配置。 That's because at some point the framework is calling a service provider which takes care of doing that. 那是因为在某些时候框架正在调用一个服务提供者来负责这样做。

Any custom providers you may want to include are set in /bootstrap/app.php like so: 您可能想要包含的任何自定义提供程序都设置在/bootstrap/app.php如下所示:

$app->register(App\\Providers\\AuthServiceProvider::class);

(Otherwise if you ask for a class that hasn't been bound by a provider, the framework just injects a new instance of that class.) (否则,如果您要求一个未被提供者绑定的类,则该框架只会注入该类的new实例。)


So, for this problem you probably want some repository class where you can encapsulate all database access. 因此,对于此问题,您可能需要一些存储库类,您可以在其中封装所有数据库访问。

Example: 例:

// app/Repositories/ProductRepository.php
private $db;

public function __construct(DatabaseManager $dbm)
{
    $this->db = $dbm->connection();
}

public function findById($id)
{
    return $this->db->table('products')->where('id', '=', $id)->get();
}

//routes.php
$app->get('products/{id}', 'ProductsController@show');

//ProductsController.php
public function show(ProductRepository $repo, $id)
{
    $product = $repo->findById($id);
    dd($product);
}

It's interesting in this example that you call for a ProductRepository injection, and, since it has a DatabaseManager dependency, the framework handles the instantiation of both. 在这个示例中,您需要一个ProductRepository注入,并且由于它具有DatabaseManager依赖性,因此该框架处理两者的实例化。


I hope this begins to answer your question about managing business logic in service providers. 我希望这开始回答你关于在服务提供商中管理业务逻辑的问题。 I guess another typical use case is authorization handling. 我想另一个典型的用例是授权处理。 You can follow the docs on this subject after this intro. 在此介绍之后,您可以按照此主题的文档进行操作

Services as a Service class? 服务即服务类? Service classes are not part of a framework, it's more like an application architecture problem you are trying to solve here. 服务类不是框架的一部分,它更像是您在此尝试解决的应用程序架构问题。

Depending on the project you are working on, either a Services folder in the app folder (if you go for the folders by type structure) or the feature folder it belongs to (if you go for app folders by feature style). 根据您正在处理的项目,应用程序文件夹中的“服务”文件夹(如果按类型结构查找文件夹)或其所属的功能文件夹(如果按功能样式选择应用程序文件夹)。 These are just 2 of the many possible ways for folder structures. 这些只是文件夹结构的许多可能方式中的两种。

It's different for every project so, it's up to you to decide where to put the service classes and how you are going to structure your app. 每个项目都有所不同,因此,您可以自行决定将服务类放在何处,以及如何构建应用程序。

Remember to stick to one convention throughout the project development cycle. 记住在整个项目开发周期中坚持一个约定。 If you can't think of it right now, structure your classes later in the refactoring sessions. 如果您现在无法想到它,请稍后在重构会话中构建您的类。 I usually get a lot more ideas when I am working on something else rather than at the start when I am thinking about it. 当我在研究别的东西时,我通常会得到更多的想法,而不是在我考虑它的时候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM