创建一个动作 - Symfony2

时间:2012-11-21 23:33:37

标签: symfony

我创建了一个bundle backend / main /:

我的app / routing.yml

backendmain:
    resource: "@backendmainBundle/Resources/config/routing.yml"
    prefix:   /main

我的后端/ Bundle / mainBundle / config / routing.yml:

backendmain_homepage:
    pattern:  /hello/{name}
    defaults: { _controller: backendmainBundle:Default:index }

我的后端/ Bundle / mainBundle / DefaultController.php:

namespace backend\Bundle\mainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

    class DefaultController extends Controller
    {
        public function indexAction($name)
        {
            return $this->render('backendmainBundle:Default:index.html.twig', array('name' => $name));
        }

            public function testAction($name)
        {
            return $this->render('backendmainBundle:Default:test.html.twig', array());
        }
    }

如何在浏览器中调用动作测试?

很抱歉,当他创建Symfony2时,Fabien是否吸毒? Symfony 1.4非常简单!

1 个答案:

答案 0 :(得分:2)

测试操作的路径定义中没有任何匹配项。您添加的唯一路由是索引操作。所以你需要添加类似的东西,

backendmain_test:
    pattern:  /test
    defaults: { _controller: backendmainBundle:Default:test }

backend/Bundle/mainBundle/config/routing.yml文件中,以便您可以调用测试操作。

<强>更新 使用注释

应用/配置/ routing.yml中

backendmain:
    resource: "@backendmainBundle/Controller/DefaultController.php"
    prefix:   /main
    type:     annotation

<强> DefaultController.php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
// ...

class DefaultController extends Controller
{
    /**
     * @Route("/test", name="backendmain_test")
     */
    public function testAction()
    {
        // ...
    }

    // ...