Symfony 2 - 非bundle库的集成和位置

3
我非常关注Symfony 2的最佳实践,并希望将一个PHP库集成到项目中。该库是一个非bundle,是一个简单的PHP类,其中包含一些方法。
我的问题遵循以下内容,但其没有被接受的答案。从这里阅读的内容来看,我决定自动加载该类,但不知道应该将PHP文件放在哪里。
也许是src/MyBundle/DependencyInjection/?我真的很怀疑,因为该库没有其他服务的依赖。
我应该创建像src/MyBundle/Services/src/MyBundle/Libraries/这样的目录吗?
在这里,最佳实践是什么?

即使其他问题没有被接受的答案,得票最高的答案是正确的,你应该将其封装成一个服务。因为该库基本上为您的应用程序提供了特定的服务来实现某些功能。 - β.εηοιτ.βε
那么我可以将它定位在DependencyInjection目录内吗? - user3076049
不是,在一个服务中:http://symfony.com/doc/current/service_container.html - β.εηοιτ.βε
1个回答

0

正如b.enoit.be所提到的,从该类创建一个服务。

MyBundle/Service/MyServiceClass.php

<?php

namespace MyBundle\Service;

class MyService
{
...
}

app/config/services.yml

services:
    app.my_service:
        class: MyBundle\Service\MyService

例如在控制器中使用它

MyBundle/Controller/DefaultController.php

<?php

namespace MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    public function indexAction(Request $request)
    {

        ...

        // get the service from the container
        $yourService = $this->get('app.my_service');

        ...
    }
}
?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接