gpt4 book ai didi

php - 找不到命名空间类? symfony2

转载 作者:可可西里 更新时间:2023-11-01 00:42:19 24 4
gpt4 key购买 nike

这里是新手问题,我创建了一个名为 MyServices 的 Bundle 和子文件夹 MyBundle,

我创建了一个名为 myAWServ 的服务,但是当我调用 $this->get('myAWS')->methodCall() 时,我收到以下错误:

CRITICAL - Fatal Error: Class 'MyServices\MyBundle\myAWS\myAWS' not found CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\ClassNotFoundException: "Attempted to load class "myAWS" from namespace "MyServices\MyBundle\myAWS". Did you forget a "use" statement for another namespace?" at /home/sergio/Desktop/hello_symfony_heroku/app/cache/dev/appDevDebugProjectContainer.php line 1755

我已经检查了所有文件一百次,但找不到文件的问题,如下所示:

<?php
//File Location : MyServices/MyBundle/Controller/myAWS.php
namespace MyServices\MyBundle\myAWS;

class myAWS
{
public function __construct($greeting)
{
$this->greeting = $greeting;
}

public function greet($name)
{
return $this->greeting . ' ' . $name;
}
}

使用包创建的根文件(php app/console generate:bundle)

//Filesource : MyServices/MyBundle/MyServicesMyBundle.php
<?php

namespace MyServices\MyBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class MyServicesMyBundle extends Bundle
{
}

和 services.yml

parameters:
myAWS.class: MyServices\MyBundle\myAWS\myAWS

services:
myAWSer:
class: "%myAWS.class%"
arguments: [teste]

我已经在 AppKernel 中加载它了 新的 MyServices\MyBundle\MyServicesMyBundle(),

目前我正在打一个简单的电话

<?php

namespace MyServices\MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
public function indexAction($name)
{
die($this->get('myAWSer')->greet($name));

}
}

我也试过清除缓存。抱歉发了这么长的帖子,提前谢谢你。

最佳答案

问题出在这里:

// File Location : MyServices/MyBundle/Controller/myAWS.php
namespace MyServices\MyBundle\myAWS;

你的文件路径是 MyServices/MyBundle/Controller/myAWS.php 但你必须遵循你的命名空间,所以正确的路径应该是 MyServices/MyBundle/myAWS/myAWS.php。您还应该查看 psr-4 specifications for autoloading .

关于php - 找不到命名空间类? symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31847595/

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