目前在Symfony 3.x上的how to create Rest API教程之后。未找到FosBundle路由
我的问题是我坚持使用此代码的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 \分量\ HttpKernel \异常\ NotFoundHttpException:在 GET/API /用户 “ ”“ 中找不到 路线” /home/swipecom/contactless/var/cache/prod/classes.php行4595 {“exception”:“[object] (Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException(code: 0):找不到路由\“GET/api/user \”在 /home/swipecom/contactles s/var/cache/prod/classes.php:4595, Symfony \ Component \ Routing \ Exception \ ResourceNotFoundException(代码: 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
控制器的路径?谢谢 – phpmeter
我刚刚为我的路线添加了一个路径属性。路径=“/ api /用户”仍然不工作,我检查路由器也使用路由器:匹配和错误说,没有路线匹配的路径 – phpmeter
@phpmeter看到我的更新:尝试与'console debug:router- show-controllers'并搜索你的控制器 – Matteo