【发布时间】:2019-07-08 15:22:27
【问题描述】:
我在 GETing{{url}}/api/user/me 时收到此错误:
在渲染模板期间抛出异常(“Bundle>”App 不存在或未启用。可能您忘记在 App\Kernel.php 的 >registerBundles() 方法中添加它文件?在@App/Controller/UserController(从“....\config/routes.yaml”导入)。确保“App/Controller/UserController”包已正确注册并>加载到应用程序中内核类。如果捆绑包已注册,请确保>确保捆绑包路径“@App/Controller/UserController”不为空。”)。
我正在使用 FOSRestBundle 和 FOSOauthBundle。
routes.yml
app_api:
resource: "@App/Controller/UserController"
type: annotation
bundles.php
<?php
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['dev' => true],
Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle::class => ['all' => true],
FOS\OAuthServerBundle\FOSOAuthServerBundle::class => ['all' => true],
FOS\RestBundle\FOSRestBundle::class => ['all' => true],
JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true],
Nelmio\ApiDocBundle\NelmioApiDocBundle::class => ['all' => true],
FOS\UserBundle\FOSUserBundle::class => ['all' => true]
];
用户控制器
<?php
namespace App\Controller;
use App\Entity\User;
use App\Service\UserService;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\View\View;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Controller\Annotations as Rest;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
/**
* @RouteResource("User")
*/
class UserController extends AbstractFOSRestController implements ClassResourceInterface
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
/**=
* @Route("/api/user/me")
* @Method("GET")
*
* @return User|string
*/
public function getMeAction()
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$loggedInUser = $this->tokenStorage->getToken()->getUser();
return new Response($loggedInUser);
}
composer.json
...
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
...
【问题讨论】:
-
你永远不会真正拥有一个名为 App.因此错误。拥有 AppBundle 曾经是标准的,但不再是标准了。只需注释掉资源并输入 routes.yml。除非你弄乱了其他配置文件,否则用户控制器和注释仍然应该被选中。
标签: php symfony symfony4 fosrestbundle