即使路由器已定义,Symfony路由,控制器方法Not Found 404

时间:2017-07-09 15:57:53

标签: php symfony routing

好的,我正在使用symfony 3.3,yml routing和Nelmio文档包。 我有一个Public Bundle,其中包含一个Controller / SearchController.php,里面有一个GET方法,该方法以某种方式找不到。

symfony debug:路由器将此方法视为:

  Name         Method  Scheme  Host  Path
  get_public   GET     ANY     ANY   /public/search/current.{_format} `

Nelmio也认识到了这一点。 但是,如果我要打电话,它会在Nelmio和Postman中给我404。 我正在使用fos rest,在app / config.config.yml中配置如下:

fos_rest:
    param_fetcher_listener: true
    routing_loader:
        default_format: json
    view:
        view_response_listener: true
        formats:
            json: true
            xml: false
            rss: false

我真的被困了,请帮忙,这是我的routing.yml:

app / config / routing.yml中的

 public:
     type: rest
     resource: "@PublicBundle/Resources/config/routing.yml"

在src / PublicBundle / Resources / config / routing.yml中:

search_controller:
    type: rest
    resource: "PublicBundle\\Controller\\SearchController"
    prefix: /public/search
    defaults: {_format: json}

和我的控制员:

    <?php

    namespace PublicBundle\Controller;

    use Symfony\Component\HttpFoundation\Request;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

    // all FOS stuff that made Nelmio work:
    use FOS\RestBundle\Request\ParamFetcher;
    use FOS\RestBundle\View\View;
    use FOS\RestBundle\Controller\FOSRestController;
    use FOS\RestBundle\Controller\Annotations;
    use FOS\RestBundle\Controller\Annotations\RouteResource;
    use FOS\RestBundle\Controller\Annotations\Get;

    use JMS\Serializer as Serializer;
    use Nelmio\ApiDocBundle\Annotation\ApiDoc;
    use Symfony\Component\HttpFoundation\Response;
    use Doctrine\Common\Util\Debug as Debug;
    /**
     * Class SearchController
     * @RouteResource("Public", pluralize=false)
     */

    class SearchController extends FOSRestController
    {

        /**
         *
         * @ApiDoc(
         *   resource = true,
         *    section = "Public Resource API",
         *    description = "nelmio doc test",
         *    statusCodes = {
         *     200 = "Returned when successful",
         *     400 = "Bad Request: returned when parameters are not of required type"
         *   }
         * )
         * @GET("/current")
         * @Route("/current")
         * @param $request
         * @return View
         */
        public function getAction(Request $request)
        {
            $view = View::create();
            return $view->setData(['test'=>'test'])->setStatusCode(Response::HTTP_OK);
        }
    }

但是当尝试这条路线时,它会从Nelmio(Chrome)和Postman投出404。

获取my-api.com/public/search/current.json

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>An Error Occurred: Not Found</title>
    </head>
    <body>
        <h1>Oops! An Error Occurred</h1>
        <h2>The server returned a "404 Not Found".</h2>

        <div>
            Something is broken. Please let us know what you were doing when this error occurred.
            We will fix it as soon as possible. Sorry for any inconvenience caused.
        </div>
    </body>
</html>
编辑:哦,只是添加,拖延apache错误我得到了这个,我觉得它没有用:

"GET /public/search/current?_format=json HTTP/1.1" 404 717 "http://api-netvlies.com/doc" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/58.0.3029.110 Chrome/58.0.3029.110 Safari/537.36"

0 个答案:

没有答案