首页 文章

未找到FosBundle路线

提问于
浏览
1

目前正在关于Symfony 3.x的how to create Rest API教程 .

我的问题是使用此代码使用 getAction() 方法 .

namespace SwipeBundle\Controller\Backend\API;

use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\View;

class UserController extends FOSRestController
{
    /**
     * @Rest\Get("/api/user")
     */
    public function getAction() {
        $restresult = $this->getDoctrine()
            ->getRepository('SwipeBundle:User')
            ->findAll();

        if ($restresult) {
            return new View("there are no users exist", Response::HTTP_NOT_FOUND);
        }
        return $restresult;
    }
}

检查日志错误:

未捕获的PHP异常Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException:“在/home/swipecom/contactless/var/cache/prod/classes.php第4595行找不到”GET / api / user“的路由{”exception“ :“[object](Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException(code:0):在/ home / swipecom / contactless / var / cache / prod / classes中找不到\”GET / api / user \“的路由 . php:4595,Symfony \ Component \ Routing \ Exception \ ResourceNotFoundException(code:0):at /home/swipecom/contactless/var/cache/prod/appProdProjectContainerUrlMatcher.php:750)"} []

我检查并配置了 FOSRest configuration and Nelmio 但仍然无法正常工作 .

这里是:

# Nelmio CORS Configuration
nelmio_cors:
    defaults:
        allow_credentials: false
        allow_origin: ['*']
        allow_headers: ['*']
        allow_methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']
        max_age: 3600
        hosts: []
        origin_regex: false

# FOSRest Configuration
fos_rest:
    body_listener: true
    format_listener:
        rules:
            - { path: '^/', priorities: ['text/html'], fallback_format: html, prefer_extension: false }
            - { path: '^/api/', priorities: ['json'], fallback_format: json, prefer_extension: false }
    param_fetcher_listener: true
    view:
        view_response_listener: 'force'
        formats:
            json: true
            xml: true
            yml: true

1 回答

  • 0

    使用path属性作为示例:

    * @Rest\Get(path="/api/user")
    

    代替

    * @Rest\Get("/api/user")
    

    您可以使用console命令进行调试:

    To show all available route:

    控制台调试:路由器

    To see who match your route:

    console router:match / api / user

    注意:

    如果问题仍然存在,请检查命令

    console debug:router --show-controllers

    并寻找你的控制器,看看会发生什么 . 例如,如果未列出您的控制器,请按照文档here中的说明检查路由配置 .

    希望这有帮助

相关问题