我不明白为什么我无法创建自定义服务。我遇到了这两种技术的错误。我在文档中找不到任何关于此的信息。添加自定义服务 - Symfony; Sylius
# app/config/services.yml
services:
jdf.utils.phphelper:
class: JDF\Utils\PhpHelper
// src/JDF/Utils/PhpHelper.php
namespace JDF\Utils;
class PhpHelper
{
/**
* [pdebug description]
* @param string $var The string to beautiful show
* @param string $msg Description of the $var
* @param integer $displayNone
* @return echo pre print_r $var string
*/
public function pdebug ($var, $msg = '', $displayNone = 0) {
}
}
情况1:(通行证PhpHelper在__construct函数)
// src/JDF/CsvTreatmentBundle\Controller/ImportController
namespace JDF\CsvTreatmentBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use JDF\Utils\PhpHelper;
use Psr\Log\LoggerInterface;
/**
*
*/
class ImportController extends Controller {
function __construct(
PhpHelper $PhpHelper
) {
}
public function indexAction() {
//$test = $this->container->get('jdf.utils.phphelper');
return new Response('<hr>');
}
} /*End of class*/
错误1: 捕致命错误:传递给JDF参数1 \ CsvTreatmentBundle \ Controller \ ImportController :: __ construct()必须是JDF \ Utils \ PhpHelper的一个实例,没有给出,在C:\ kitutilitaire \ vendor中调用\ symfony的\ symfony的\ SRC \的Symfony \元器件\ HttpKernel \上线202控制器\ ControllerResolver.php和定义 500内部服务器错误 - ContextErrorException
案例2(只是使用get()控制器的方法):
// src/JDF/CsvTreatmentBundle\Controller/ImportController
namespace JDF\CsvTreatmentBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use JDF\Utils\PhpHelper;
use Psr\Log\LoggerInterface;
/**
*
*/
class ImportController extends Controller {
function __construct(
//PhpHelper $PhpHelper
// LoggerInterface $logger
) {
}
public function indexAction() {
$test = $this->container->get('jdf.utils.phphelper');
// $logger = $this->container->get('logger');
return new Response('<hr>');
}
} /*End of class*/
错误2: 已尝试从名称空间 “JDF \ utils的” 加载类 “PhpHelper”。 你忘记了另一个命名空间的“使用”语句吗?
堆栈跟踪
in var\cache\dev\appDevDebugProjectContainer.php at line 3555 -
*/
protected function getJdf_Utils_PhphelperService()
{
return $this->services['jdf.utils.phphelper'] = new \JDF\Utils\PhpHelper();
}
/**
编辑:composer.json自动加载
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle/",
"JDF\\CsvTreatmentBundle\\": "src/JDF/CsvTreatmentBundle/",
"JDF\\Utils\\": "src/JDF/Utils/PhpHelper"
},
"classmap": ["app/AppKernel.php", "app/AppCache.php"]
},
感谢前请先对您有所帮助。
如何在services.yml中定义服务?我写了我的services.yml(第一个代码展示) – Xenofexs
@Xenofexs:你说得对,我的不好。我已经更新了答案。 – colburton
感谢您的更新,但这是我的情况2(已编辑,我已删除非浮动行) 使用此代码,我得到了error2(尝试从命名空间“JDF \ Utils”加载类“PhpHelper” ) 和行错误:'return $ this-> services ['jdf.utils.phphelper'] = new \ JDF \ Utils \ PhpHelper();' – Xenofexs