doushenmao9036 2017-03-30 12:55
浏览 64
已采纳

无法获得Route Symfony

As the title say explecitly, I don't manage to get some specific route I recently added. My symfony version : 2.8.18. So here are the details :

Route I try to match : - /platform/cat - /platform/cat/{id}

File - app/config/routing.yml :

gb_platform:
resource: "@GBPlatformBundle/Resources/config/routing.yml"
prefix:   /platform

File - GB\PlatformBundle\Resources\configouting.yml :

    gb_platform_home_:
    path :   /
      defaults: { _controller: GBPlatformBundle:Advert:index }

gb_platform_home:
    path :   /{page}
    defaults: 
        _controller : GBPlatformBundle:Advert:index
        page: 1
    requirements:
        page: \d*

gb_platform_view:
    path :   /advert/{id}
    defaults: { _controller : GBPlatformBundle:Advert:view }
    requirements:
        id: \d+

gb_platform_add:
    path :   /add
    defaults: { _controller : GBPlatformBundle:Advert:add }

gb_platform_edit:
    path :   /edit/{id}
    defaults: { _controller : GBPlatformBundle:Advert:edit }
    requirements:
        id: \d+

gb_platform_delete:
    path :   /delete/{id}
    defaults: { _controller : GBPlatformBundle:Advert:delete }
    requirements:
        id: \d+

gb_platform_cat:
    path :   /cat
    defaults: { _controller : GBPlatformBundle:Category:index }

gb_platform_cat_view:
    path : /cat/{id}
    defaults: { _controller : GBPlatformBundle:Category:view }
    requirements:
        id: \d+

File - GB\PlatformBundle\Resources\controller\CategoryController.php :

    <?php

namespace GB\PlatformBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;

use GB\PlatformBundle\Entity\Advert;
use GB\PlatformBundle\Entity\Category;

class CategoryController extends Controller
{
    public function indexAction(Request $request)
    {

    }

    public function viewAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $catRepo = $em
            ->getRepository('GBPlatformBundle:Category');
        $cat = $catRepo
            ->find($id);

        if($cat === null)
        {
            throw new NotFoundHttpException("L'annonce d'id ".$id." n'existe pas.");
        }

        return $this->render('GBPlatformBundle:Category:view.html.twig', array(
            'category' => $cat,
            ));
    }
}

At the origin, the routing.yml of the bundle was invalid. Once it's fixed, I try to get the both command :

php app/console debug:router

php app/console debug:router gb_platform_cat

php app/console debug:router gb_platform_add

Debug Router

Each command were successfull and I saw no difference between /cat and /add while one match and the other don't. I tried also many times to clear cache with :

php app/console cache:clear --env prod

Still don't work. I'm confused ... The error is basic : No route found for "GET /platform/cat/7

  • 写回答

3条回答 默认 最新

  • doujiaochan7317 2017-03-30 22:14
    关注

    I finally get the solution.

    First of all be sure of the validity of the routing.yml files. Additionnaly to usual symfony command (which will advice you of a mistake), you can use a validator like : yamllint.com

    In my case, routes config was correct. And I knew symfony could find them thanks to debug:router command and comparing them with other functional routes.

    What I noticed, is that even changing the controller of routes that can matched, the update wasn't considered by the system. So obviously, it's a cache problem.

    At the really begining I used, to reject this hypothesis, the command :

    php app/console cache:clear --env prod

    But the problem was more global that the production environnement, so I solved my problem without specifying the env :

    php app/console cache:clear

    The kind of mistake you do once, but not twice so it's .. ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?