【问题标题】:"Bundle "App" does not exist or it is not enabled“捆绑”应用程序“不存在或未启用
【发布时间】: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/"
        }
    },
...

Directory

【问题讨论】:

  • 你永远不会真正拥有一个名为 App.因此错误。拥有 AppBundle 曾经是标准的,但不再是标准了。只需注释掉资源并输入 routes.yml。除非你弄乱了其他配置文件,否则用户控制器和注释仍然应该被选中。

标签: php symfony symfony4 fosrestbundle


【解决方案1】:

带有“at”符号的@App/... 符号纯粹用于解析包的位置。

您正在尝试解析项目源文件的位置,这些文件不存在于包中。

routes.yml 中试试这个:

app_api:
  resource: "../src/Controller/*"
  type:     annotation

【讨论】:

    猜你喜欢
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多