Symfony 3路由不工作YML

时间:2016-07-03 00:07:45

标签: php symfony

这是app / routing.yml

index:
path:     /
defaults: { _controller: AppBundle:Main:index }

path:     /new
defaults: { _controller: AppBundle:Main:create }
methods:  [POST]

这是我的Bundle Routes

class MainController extends Controller
{
    public function indexAction()
    {
    return $this->render('AppBundle:Main:index.html.twig', array(
        // ...
    ));
}

public function createAction ()
{
    $post = new Post();
    $post->setTitle('Example')
        ->setBody('THIS IS THE BODY OF THE POST');
    $em = $this->getDoctrine()->getManager();
    $em->persist($post);
    $em->flush();

    return new Response('POST '.$post->getTitle().' ADDED');
}

这是我的控制器

7.04 * numberOfTransactions

索引操作正常,但createAction没有。我只想将$ post对象持久化到数据库中。这应该工作正常,因为我遵循文档,它只是找不到路线。我检查确保使用./bin/console debug:router /

2 个答案:

答案 0 :(得分:0)

我认为yml应该是

index:
    path:     /
    defaults: { _controller: AppBundle:Main:index }

create:
    path:     /new
    defaults: { _controller: AppBundle:Main:create }

答案 1 :(得分:0)

您的MainController文件看起来不正确。应该是这样的:

class MainController extends Controller
{
    public function indexAction()
    {
        return $this->render('AppBundle:Main:index.html.twig', array(
          // ...
        ));
    }

    public function createAction ()
    {
         $post = new Post();
         $post->setTitle('Example')
              ->setBody('THIS IS THE BODY OF THE POST');
         $em = $this->getDoctrine()->getManager();
         $em->persist($post);
         $em->flush();

         return new Response('POST '.$post->getTitle().' ADDED');
    }
}