目前在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