【问题标题】:Symfony type:annotation won't include subdirectoriesSymfony 类型:注解不会包含子目录
【发布时间】:2016-08-17 15:58:37
【问题描述】:

我正在尝试在“控制器”目录的不同子文件夹中创建具有少量控制器的项目。

我的 routing.yml 配置:

app:
resource: '@AppBundle/Controller/'
type:     annotation

官方 Symfony 最佳实践指南有信息,该配置将从存储在 src/AppBundle/Controller/ 目录中的任何控制器甚至从其子目录中加载注释。 http://symfony.com/doc/current/best_practices/controllers.html

但是当我尝试打开链接到子文件夹控制器的页面时,出现以下错误:

Class AppBundle\Controller\FirtreeShopMapController does not exist in 
/src/AppBundle/Controller/ (which is being imported from
 "/app/config/routing.yml").

我的子目录控制器:

<?php

namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class FirtreeShopMapController extends Controller
{

    /**
     * @Route("/firtreeShopMap", name="firtreeShopMap")
     */

    public function showFirtreeShopMapAction(Request $request)
    {
        $renderData = array();
        return $this->render('plugins/firtreeShopMap/index.html.twig', $renderData);
    }
}

我的Controller目录结构:

Controller/
     ├─ DefaultController.php
     ├─ FirtreeShopMap/
     │  ├─ FirtreeShopMapController.php

那么我做错了什么?

【问题讨论】:

  • 首先使用正确的命名空间,以便自动加载器可以找到您的类:命名空间 AppBundle\Controller\FirtreeShopMap;
  • 抱歉,现在有什么不对的地方?
  • 哦!!!我应该在 FirtreeShopMapController.php 中编辑命名空间。谢谢。

标签: symfony


【解决方案1】:

是的,@Cerad 是正确的。 像这样更改命名空间行:

<?php

namespace AppBundle\Controller\FirtreeShopMap;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class FirtreeShopMapController extends Controller
{

我认为这就是你需要改变的全部。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 2014-12-16
    相关资源
    最近更新 更多