Php Symfony通过自定义包路由仅在控制台模式下有效

Php Symfony通过自定义包路由仅在控制台模式下有效,php,routing,symfony,Php,Routing,Symfony,我不熟悉Symfony,但不熟悉PHP。我只是不明白为什么我不能从自定义包中获得主页路径,但它似乎是从控制台输出中工作的。当我在web浏览器中执行路由时,它将始终从AppBundle(即默认的Symfony欢迎页面)重新调用/路由,即使AppBundle已从AppKernal.php中的registerBundles()函数中删除。我可以确认这一点,因为如果从/src中删除AppBundle目录,我将得到以下错误日志: [2016-06-22 19:07:57] request.INFO: Ma

我不熟悉Symfony,但不熟悉PHP。我只是不明白为什么我不能从自定义包中获得主页路径,但它似乎是从控制台输出中工作的。当我在web浏览器中执行路由时,它将始终从AppBundle(即默认的Symfony欢迎页面)重新调用
/
路由,即使AppBundle已从AppKernal.php中的
registerBundles()
函数中删除。我可以确认这一点,因为如果从/src中删除AppBundle目录,我将得到以下错误日志:

[2016-06-22 19:07:57] request.INFO: Matched route "homepage". {"route_parameters":{"_controller":"AppBundle\\Controller\\DefaultController::indexAction","_route":"homepage"},"request_uri":"http://bvd-v3.dev/"} []
[2016-06-22 19:07:57] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2016-06-22 19:07:57] request.CRITICAL: Uncaught PHP Exception InvalidArgumentException: "Class "AppBundle\Controller\DefaultController" does not exist." at /Users/apple/projects/mentel/bvd-v3/var/cache/prod/classes.php line 2498 {"exception":"[object] (InvalidArgumentException(code: 0): Class \"AppBundle\\Controller\\DefaultController\" does not exist. at /Users/apple/projects/mentel/bvd-v3/var/cache/prod/classes.php:2498)"} []
然而,如果我调用
php-bin/console-router:match/

输出为:

+--------------+---------------------------------------------------------+
| Property     | Value                                                   |
+--------------+---------------------------------------------------------+
| Route Name   | homepage                                                |
| Path         | /                                                       |
| Path Regex   | #^/$#s                                                  |
| Host         | ANY                                                     |
| Host Regex   |                                                         |
| Scheme       | ANY                                                     | 
| Method       | ANY                                                     |
| Requirements | NO CUSTOM                                               |
| Class        | Symfony\Component\Routing\Route                         |
| Defaults     | _controller: MentelBundle:Index:index                   |
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+
 -------------------------- -------- -------- ------ ----------------------------------- 
  Name                       Method   Scheme   Host   Path                               
 -------------------------- -------- -------- ------ ----------------------------------- 
  _wdt                       ANY      ANY      ANY    /_wdt/{token}                      
  _profiler_home             ANY      ANY      ANY    /_profiler/                        
  _profiler_search           ANY      ANY      ANY    /_profiler/search                  
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar              
  _profiler_info             ANY      ANY      ANY    /_profiler/info/{about}            
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo                 
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results  
  _profiler                  ANY      ANY      ANY    /_profiler/{token}                 
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router          
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception       
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css   
  _twig_error_test           ANY      ANY      ANY    /_error/{code}.{_format}           
  homepage                   ANY      ANY      ANY    /                                  
如果我调用
php bin/console debug:router

输出为:

+--------------+---------------------------------------------------------+
| Property     | Value                                                   |
+--------------+---------------------------------------------------------+
| Route Name   | homepage                                                |
| Path         | /                                                       |
| Path Regex   | #^/$#s                                                  |
| Host         | ANY                                                     |
| Host Regex   |                                                         |
| Scheme       | ANY                                                     | 
| Method       | ANY                                                     |
| Requirements | NO CUSTOM                                               |
| Class        | Symfony\Component\Routing\Route                         |
| Defaults     | _controller: MentelBundle:Index:index                   |
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+
 -------------------------- -------- -------- ------ ----------------------------------- 
  Name                       Method   Scheme   Host   Path                               
 -------------------------- -------- -------- ------ ----------------------------------- 
  _wdt                       ANY      ANY      ANY    /_wdt/{token}                      
  _profiler_home             ANY      ANY      ANY    /_profiler/                        
  _profiler_search           ANY      ANY      ANY    /_profiler/search                  
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar              
  _profiler_info             ANY      ANY      ANY    /_profiler/info/{about}            
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo                 
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results  
  _profiler                  ANY      ANY      ANY    /_profiler/{token}                 
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router          
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception       
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css   
  _twig_error_test           ANY      ANY      ANY    /_error/{code}.{_format}           
  homepage                   ANY      ANY      ANY    /                                  

routing.yml的内容:

mentel:
    resource: "@MentelBundle/Controller/"
    type:     annotation
路由_dev.yml的内容:

_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt

_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler

_errors:
    resource: "@TwigBundle/Resources/config/routing/errors.xml"
    prefix:   /_error

_main:
    resource: routing.yml
/app/AppKernal.php中的
registerBundles()
的内容:

public function registerBundles()
{
    $bundles = [
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new BVD\MentelBundle\MentelBundle(),
    ];

    if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
        $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
        $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
        $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
        $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
    }

    return $bundles;
}
控制器的内容:

<?php

namespace BVD\MentelBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class IndexController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction()
    {
        die('?');
        return $this->render('@mentel/pages/home.html.twig', array(
            // ...
        ));
     }

 }
运行:


因此,我认为存在误解和误解@Route(“/”,name=“homepage”)是指hostname.de/如果您想要hostname.de/主页@Route(“/homepage”,name=“homepage”)您应该使用的路由,请列出该路由。我不想要
/homepage
我想要从域提供的路由,即
http://example.com
这将是一个
/
,但你说什么并不重要,因为将路由更改为
/something\u other
只会得到一个Symfony 404。因为没有AppBundle路由。您是否尝试过清除缓存(
bin/console cache:clear
)?@Oldskool-是的,我也尝试过。输出似乎显示它已清除,没有任何问题:
[OK]已成功清除“dev”环境(debug=true)的缓存。
@Oldskool-Aha您基本上完全正确。我正在运行
bin/console cache:clear
,但它只在清除dev缓存,并且应用程序正在prod模式下运行。。。我没有意识到。运行php-bin/console-cache:clear--env=prod
为我解决了这个问题。谢谢你的意见。