在获取{{url}}/api/user/me
时出现此错误:
在呈现模板的过程中抛出了一个异常(“捆绑”>“应用”不存在或未启用。也许您忘记将其添加到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]
];
UserController
<?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/"
}
},
...
答案 0 :(得分:0)
带有{at的符号的@App/...
表示法仅用于解析包的位置。
您正在尝试解决项目资源文件的位置,这些文件不在捆绑包中。
在routes.yml
中尝试以下操作:
app_api:
resource: "../src/Controller/*"
type: annotation