如何将我的控制器逻辑的一部分放在我的控制器之外? (一项 "big" 服务或多项服务)

How to put part of my Controller logic outside my Controller? (one "big" service or multiple services)

也许这是一个微不足道的问题,但我是 Silex 的初学者,我不知道该怎么做...所以这是我的情况:

我有一个 Silex 应用程序,一切正常,但我不是我的控制器的忠实粉丝,因为有很多代码,我重复这段代码两次:

第一个控制器:

$app->match('/', function (Request $request) use ($app) {
    // 1/ I get data from a form
    // 2/ I check if User is logged
    // 2.1 / If not logged -> I created new User (Entity User) + send email
    // 3/ I create a new Event (Entity Event) + put some file on AWS s3
    // 4/ I create a new Product (Entity Product)
    // 5/ I activate some add-on to my Event (request to an other BDD)

    return $app['twig']->render('home.html.twig');

})->bind('home');

第二个控制器

$app->match('/manage-my-product', function (Request $request) use ($app) {
    if ($app['security.authorization_checker']->isGranted('IS_AUTHENTICATED_FULLY')) {

    // 1/ I get data from a form
    // 2/ If user is here he must be logged so no need to check one more time
    // 3/ I create a new Event (Entity Event) + put some file on AWS s3
    // 4/ I create a new Product (Entity Product)
    // 5/ I activate some add-on to my Event (request to an other BDD)

    } else {
        return $app->redirect($app["url_generator"]->generate("login"));
    }
})->bind('product');

如您所见,我做的几乎相同,几乎没有区别,所以我的问题是:是否可以将该逻辑放在控制器外部并使用它两次? 我应该如何将逻辑放在我的控制器之外?

我真的不知道该怎么做,因为我里面有很多不同的东西:

编辑:

根据我得到的答案,我认为我的问题对我的真正问题不是很清楚...我的直觉是我需要使用服务,但我很难构建它,所以:

正如我之前所说,我已经有服务 $app['user.dao']$app['event.dao'],例如我用来创建新用户或新事件,所以我应该这样做:

$app->match('/', function (Request $request) use ($app) {
    // 1/ Service 1 to create new user
    // 2/ Service 2 to create new event
    // 3/ Service 3 to create new add-on
    // and so on...

    return $app['twig']->render('home.html.twig');

})->bind('home');

$app->match('/', function (Request $request) use ($app) {
    // Use ONE big service that use all the services I need

    return $app['twig']->render('home.html.twig');

})->bind('home');

编辑结束

正如我所说,它现在可以工作,但我想知道是否可以将所有这些混合在一个我可以多次使用的方法/功能/服务中,这对我未来的应用程序可能会很好!

谢谢

您可以使用添加到服务容器的服务,如此处所述:

https://symfony.com/doc/current/service_container.html#creating-configuring-services-in-the-container

(检查您使用的 Symfony 版本)