【问题标题】:Symfony routing confusionSymfony 路由混乱
【发布时间】:2014-07-18 21:16:47
【问题描述】:

大家好,我是 Symfony 菜鸟,所以请多多包涵

我有一个像这样的简单控制器

<?php

namespace Sites\AllBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

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


class SitesController extends Controller
{
    /**
     * @Route("/", name="_all")
     * @Template()
     */
    public function indexAction()
    {       
        return array('name' => 'sssssss');
    }

    /**
     * @Route("/wow",)
     * @Template()
     */
    public function wowAction()
    {       
        return array('wow' => 'sssssss');
    }

}

我的这个 bundle 的 routing.yml 是这样的

_all:
    resource: "@SitesAllBundle/Controller/SitesController.php"
    type:     annotation
    prefix:   /sites

而app/config/routing.yml中的路由是这样的

sites_all:
    resource: "@SitesAllBundle/Resources/config/routing.yml"

足够简单,它工作正常......但我不想添加这个

@Route("/")

在每个功能的评论中,如果我删除它,它就不起作用了。

有没有办法让控制器/函数直接指向那个函数?

谢谢大家

【问题讨论】:

    标签: php symfony routing


    【解决方案1】:

    如果我理解正确,您要求的是一种由控制器和操作名称定义路由的方法,而不必为每个操作指定路由,对吗?

    Symfony2 不提供开箱即用的功能,但可以通过第三方捆绑包LswDefaultRoutingBundle 实现。

    安装并启用捆绑包后,您可以像这样设置您的 routing.yml:

    _all:
        resource: "@SitesAllBundle"
        type:     default
        prefix:   /
    

    然后,如果您使用

    调试您的路线
    app/console router:debug
    

    你应该有类似的东西:

    [router] Current routes
    Name                                                   Method Pattern
    sites_allbundle.sites.index                            ANY    /sites/
    sites_allbundle.sites.wow                              ANY    /sites/wow
    

    匹配 /{prefix}/{controller}/{action}

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 2014-05-16
      • 2016-09-08
      • 1970-01-01
      相关资源
      最近更新 更多