drd94483 2016-07-11 15:50
浏览 221
已采纳

为什么localhost:8000不起作用,而...:8000 / test在symfony上正常工作?

Im completely new to Symfony so please bear with me. Im trying to configure my controller to atleast get a working webpage. I created a new Symfony project and started my server. After writing some code into MyController

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;

 class MyController
{
 /**
 * @Route("/test")
 */
 public function numberAction()
 {
    $prankBro= "Its just a prank bro";

    return new Response($prankBro);
 }
}

After trying to access localhost:8000 I get a fatal 404 error: No route found for "GET /" Being a complete beginner I have no idea what this is. I believe it is some kind of response type, although I'm not sure. On the other hand if I use localhost:8000/test It seems to be working fine, I get my webpage with a message saying that it was just a prank bro :) Is this how it is supposed to be in symfony? It really messes up with my brain, so if it is fine, can someone explain me why plain localhost:8000 URL gives me 404 error? Thank you!

  • 写回答

1条回答 默认 最新

  • doupin5408 2016-07-11 15:54
    关注

    Take note of the comment above numberAction:

    /**
    * @Route("/test")
    */
    

    That implies to me that there is a route set up for localhost:8000/test which will execute the numberAction method.

    You need to create a method that will be your home page, and then create a route for / which will run your new method. Then when you visit localhost:8000, your new method will be executed.

    See the Symphony documentation for details on how to set up routes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?