【问题标题】:My symfony2 controller is not redirecting我的 symfony2 控制器没有重定向
【发布时间】:2013-02-16 05:34:11
【问题描述】:

我创建了一个默认控制器,如下所示

namespace Mytest\VameBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{
public function indexAction()
{
    return $this->redirect($this->generateUrl('home'));
}

public function myactionAction($name)
{
    return new Response('<html><body>'.$name.'</body></html>');
}

}

路由文件如下所示。

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;


$collection = new RouteCollection();

$collection->add('name', new Route('/vame',array(
    '_controller' => 'MytestVameBundle:Default:index',
    )));

$collection->add('home', new Route('/vame/{name}',array(
    '_controller' => 'MytestVameBundle:Default:myaction',
    'name' => 'hhhhhhhhhh',

)));

return $collection;

显示如下错误

页面重定向不正确

我想要做的是,一旦使用索引操作调用默认控制器,它将重定向到带有一个参数的 myaction 操作。

所以请大家帮忙...

【问题讨论】:

    标签: php symfony symfony-2.1


    【解决方案1】:

    您的应用以永远不会完成的方式重定向请求。所以你有某种重定向循环。

    看来你的代码没问题。但是,您不应该在路由文件中硬编码参数,而是这样

    $this->redirect($this->generateUrl('home', array('name' => 'test')));
    

    你有更多的路由定义吗?也许他们互相干扰。可以发一下你的日志吗?

    【讨论】:

    • 谢谢我的回答。我没有将参数传递给 url。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多