Symfony 4条路线

时间:2018-12-23 12:05:54

标签: routes annotations symfony4

在Symfony 4上启动项目。但是我遇到了问题。当我使用新路线时,出现“找不到路线”。但是类似的路线是正确的。 命令debug:显示旧路线的路由器,清除现金对我不起作用,我使用了

1)php bin/console cache:clear --env=dev
2)php bin/console cache:clear --env=prod
3)php bin/console cache:clear --env=prod --no-debug

debug:router result 甚至路由名称也不正确。

routes.yaml为空(使用注释)。 我的控制器:

namespace App\Controller;
use App\Entity\Category;
use App\Entity\Product;
use App\Entity\Subcategory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Psr\Log\LoggerInterface;

class DefaultController extends AbstractController
{
    /**
     * Matches / exactly
     *
     * @Route("/", name="index")
     */
    public function index()
    {
        $products = $this->getDoctrine()
            ->getRepository(Product::class)
            ->findDiscounts();
        $categories = $this->getDoctrine()
            ->getRepository(Category::class)
            ->findAll();
        return $this->render('default/index.html.twig', compact('products', 'categories'));
    }

    /**
     * Matches /shop/*
     *
     * @Route("/shop/{categoryAlias}", name="shop_show")
     * @param string $categoryAlias
     * @return Response
     */
    public function shop($categoryAlias = 'null')
    {
        if ($categoryAlias != 'null') :
            $response = $this->getDoctrine()
                ->getRepository(Category::class)
                ->findbyAlias($categoryAlias);
            $categories = array(); //Костыль
            $categories[] = $response; //Костыль
            $subcategories = $categories[0]->getSubcategories();
        else :
            $categories = $this->getDoctrine()
                ->getRepository(Category::class)
                ->findAll();
            $subcategories = null;
        endif;
        $allCategories = $this->getDoctrine()
            ->getRepository(Category::class)
            ->findAll();
        return $this->render('default/shop.html.twig', compact('allCategories','subcategories','categories'));
    }

    /**
     * Matches /product/*
     *
     * @Route("/product/{productAlias}", name="product_show")
     * @param string $productAlias
     * @return Response
     */
    public function product($productAlias)
    {
        $product = $this->getDoctrine()
            ->getRepository(Product::class)
            ->findByAlias($productAlias);
        return $this->render('default/product.html.twig', compact('product'));
    }
}

路线/ shop正在运行。路线/产品不是。 我只是从Symfony开始,并停留在这里。 谢谢。

1 个答案:

答案 0 :(得分:0)

手动删除文件夹“缓存”,一切正常