簡體   English   中英

使用 Symfony3 路由不起作用

[英]Routing with Symfony3 is not working

我把我能在這里找到的關於 Symfony 3 路由問題的所有主題都寫成了紅色,但它們對我沒有幫助。 所以我的問題是我的控制器沒有被訪問。 這是我的代碼:

路由.yaml:

app:
    path: /test
    defaults: {_controller: AppBundle:Lucky:number}

工作區/測試/src/AppBundle/Controller/LuckyController.php:

<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class LuckyController extends Controller
{
    public function numberAction()
    {
        $number = rand(0, 1000);
        return new Response(
            '<html><body>Lucky number: '.$number.'</body></html>'
        );
    }
}

調用時: http://test.local/test我得到一個 404 返回,並使用bin/console cache:clear 清除緩存沒有幫助。

如果您對 YML 不太滿意,為什么不選擇 Annotation? 這可能是您的控制器類使用注釋的樣子:

    <?php
        namespace AppBundle\Controller;
        use Symfony\Bundle\FrameworkBundle\Controller\Controller;
        use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; //<== BE SURE YOU IMPORTED THE "ROUTE" CLASS
        use Symfony\Component\HttpFoundation\Response;


        /**
         * Lucky controller.
         *
         * @Route("/")
         */
        class LuckyController extends Controller {


            /**
             *
             * @Route("/test", name="lucky_test")
             */
            public function numberAction() {
                $number = rand(0, 1000);
                return new Response(
                    '<html><body>Lucky number: '.$number.'</body></html>'
                );
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM