简体   繁体   中英

Routing with Symfony3 is not working

I have red all the topics I could find here regarding routing problems with Symfony 3 but they didn't help me. So my problem is that my controller is not getting accessed. Here is my code:

routing.yaml:

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

workspace/test/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>'
        );
    }
}

When calling: http://test.local/test I get a 404 returned, and clearing the cache with bin/console cache:clear doesn't help.

If you are not so much comfortable with YML why not go for Annotation? Here could be what your Controller Class could look like using 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>'
                );
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM