dongsi7067 2017-05-28 21:26
浏览 51
已采纳

Symfony 2 - 非捆绑库集成和位置

I am all about the best practice of symfony 2 and I would like to integrate a php library into the project. Library is a non bundle, a simple php class with some methods.

My question just follows the following, which DOES NOT have an accepted answer. Anyway from what I read here I decided to autoload the class, but have no idea where should I locate the php file.

Maybe src/MyBundle/DependencyInjection/? I really doubt it since library has no dependency of other services I have.

Should I create a directory like src/MyBundle/Services/ or src/MyBundle/Libraries/?

What is the best practice here?

  • 写回答

1条回答 默认 最新

  • dongpian6319 2017-05-29 06:28
    关注

    as mentioned by b.enoit.be create a service from the class.

    MyBundle/Service/MyServiceClass.php

    <?php
    
    namespace MyBundle\Service;
    
    class MyService
    {
    ...
    }
    

    app/config/services.yml

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

    use it e.g. in a controller

    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');
    
            ...
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵