无法获取路由 Symfony

Cannot Get Route Symfony

如标题所言,我最近添加的一些特定路线没有得到。 我的 symfony 版本:2.8.18。 所以这是详细信息:

我尝试匹配的路线: - /platform/cat - /platform/cat/{id}

文件 - app/config/routing.yml :

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

文件 - GB\PlatformBundle\Resources\config\routing.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+

文件 - 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,
            ));
    }
}

在原点,捆绑包的 routing.yml 无效。修复后,我尝试获取两个命令:

php app/console debug:router

php app/console debug:router gb_platform_cat

php app/console debug:router gb_platform_add

每个命令都成功,我发现 /cat 和 /add 之间没有区别,一个匹配,另一个不匹配。 我也多次尝试使用 :

清除缓存

php app/console cache:clear --env prod

还是不行。我糊涂了 ... 错误是基本的:找不到“GET /platform/cat/7

的路由

您定义了 /cat/7 而不是 /platform/cat/7

无论如何在控制器而不是路由文件中使用注释更好:

/**
 * @Route("/platform/cat/{id}", name="gb_platform_cat_view")
 */
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,
        ));
}

根据 docs,您需要在模式周围加上引号以满足要求。

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

终于找到答案了

首先要确定 routing.yml 文件的有效性。 除了通常的 symfony 命令(它会提示你错误)之外,你还可以使用像这样的验证器:yamllint.com

就我而言,路由配置是正确的。我知道 symfony 可以通过 debug:router 命令找到它们并将它们与其他功能路由进行比较。

我注意到,即使更改可以匹配的路由控制器,系统也不会考虑更新。很明显,这是一个缓存问题。

一开始我使用命令来拒绝这个假设:

php app/console cache:clear --env prod

但是这个问题比生产环境更具全局性,所以我在没有指定环境的情况下解决了我的问题:

php app/console cache:clear

这种错误你只犯过一次,但不会犯两次,所以 .. ;)