【问题标题】:Symfony2: creation of an event listener: syntax error, unexpected '-', expecting '('Symfony2:创建事件监听器:语法错误,意外'-',期待'('
【发布时间】:2012-08-31 12:04:25
【问题描述】:

我已经实现了"How to create an Event Listener" 来捕获和处理我的应用程序抛出的所有异常。

但是,当我转到主页时,我收到了这个错误(来自 apache 日志):

在生产模式下(app.php):

语法错误,意外的 '-',期待 '(' appProdProjectContainer.php 第 383 行

在开发中。模式(app_dev.php):

语法错误,意外的“-”,在第 1131 行的 appDevDebugProjectContainer.php 中预期为“(”

我已经用谷歌搜索了,但我没有找到可以解决我的问题的东西......

所以,这里是我的 services.yml 由“DependencyInjection/lhnbackendExtension”类加载:

services:
    kernel.listener.lhn-exception:
        class: lhn\backendBundle\Listener\LhnExceptionListener
        tags:
            - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

--> 我曾尝试在 XML 文件中配置服务,但出现同样的错误...

以及基本的监听器实现:

namespace lhn\backendBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;

class LhnExceptionListener
{
    public function onKernelException(GetResponseForExceptionEvent $event){
        // We get the exception object from the received event
        $exception = $event->getException();
        $message = 'My Error says: ' . $exception->getMessage();

        // Customize our response object to display our exception details
        $response = new Response();
        $response->setContent($message);
        $response->setStatusCode($exception->getStatusCode());

        // Send our modified response object to the event
        $event->setResponse($response);
    }
}

【问题讨论】:

  • 尝试用lhn_exception替换lhn-exception

标签: php symfony


【解决方案1】:

尽量不要在服务名称中使用连字符。

services:
    kernel.listener.lhn-exception:    
    ...

变成:

services:
    kernel.listener.lhn_exception:    
    ...

【讨论】:

    猜你喜欢
    • 2017-09-12
    • 2019-12-06
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 2013-11-11
    • 1970-01-01
    相关资源
    最近更新 更多