douluan5444 2012-07-28 13:14
浏览 109
已采纳

为什么这个Symfony2路由不起作用?

I have a problem with the routing in Symfony2.

Actually I downloaded the latest release and run it on my server. The demo works fine.

Now I want to do the following: I want to create a TestController, this controller should have:

  • an index view
  • a view like hello world
  • a view where i can pass 2 parameters

So I started to create a new controller in the src\Acme\DemoBundle\Controller folder called TestController. Here is the code:

<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Acme\DemoBundle\Form\ContactType;

// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class TestController extends Controller
{
    public function indexAction()
    {
        return array();
    }

    public function hello2Action($name1, $name2)
    {
        return array();
    }

    public function helloAction()
    {
        return array();
    }
}

Then I created 3 views in a new folder src\Acme\DemoBundle\Resources\views\Test called hello.html.twig, index.html.twig and hello2.html.twig

Both of them have a content like this

{% extends "AcmeDemoBundle::layout.html.twig" %}

{% block title "Symfony - Demos" %}

{% block content_header '' %}

{% block content %}
    foo!!!
{% endblock %}

Finally I edited the routing.dev.yml and added somthing like this:

_name1:
    resource: "@AcmeDemoBundle/Controller/TestController.php"
    type:     annotation
    prefix:   /test

_name2:
    resource: "@AcmeDemoBundle/Controller/TestController.php"
    type:     annotation
    prefix:   /test/hello

_name3:
    resource: "@AcmeDemoBundle/Controller/TestController.php"
    type:     annotation
    prefix:   /test/hello2/{name1}&{name2}

When i want to run the test controller i get:

No route found for "GET /test/"

What is wrong? Is it possible to have one view for two controller functions? (like hello() and hello($foo))?

  • 写回答

3条回答 默认 最新

  • dsk710351 2012-07-28 14:01
    关注

    You can also do this:

    1. In routing_dev.yml, make sure you have this:

       _main:
           resource: routing.yml
      
    2. In routing.yml, add something like this:

         AcmeDemoBundle:
            resource: "@AcmeDemoBundle/Resources/config/routing.yml"
            prefix:   /test
      

      You can select the prefix you want in accessing that particular bundle.

    3. In Acme/DemoBundle/Resources/config/routing.yml you can now add your route patterns.

        name1:
            pattern: /
            defaults: { _controller: AcmeDemoBundle:Test:index }
      
        name2:
            pattern: /hello
            defaults: { _controller: AcmeDemoBundle:Test:hello }
      
        name3:
            pattern: /hello2/{name1}/{name2}
            defaults: { _controller: AcmeDemoBundle:Test:hello2 }
      

    And you can now access, route /test, /test/hello and /test/hello2/firstname/lastname'. This is just one way of managing routes in symfony 2. This might help: http://symfony.com/doc/current/book/routing.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器