gpt4 book ai didi

php - Symfony 2 - 非捆绑库集成和定位

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:33 24 4
gpt4 key购买 nike

我对 symfony 2 的最佳实践很感兴趣,我想将一个 php 库集成到项目中。库是一个非包,一个简单的 php 类,带有一些方法。

我的问题紧接着 following ,它没有公认的答案。无论如何,根据我在这里阅读的内容,我决定自动加载该类,但不知道我应该在哪里找到 php 文件。

也许src/MyBundle/DependencyInjection/?我真的很怀疑,因为图书馆不依赖于我拥有的其他服务。

我应该创建一个像 src/MyBundle/Services/ 还是 src/MyBundle/Libraries/ 这样的目录?

此处的最佳做法是什么?

最佳答案

如 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

使用它,例如在 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');

...
}
}
?>

关于php - Symfony 2 - 非捆绑库集成和定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44232186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com