douxueke5653 2013-02-24 04:37
浏览 22
已采纳

Laravel 3 - 404事件监听器未从控制器中拾取

I'm trying to use the default event listener in Laravel for errors (i.e. 404).

I have the following code:

Event::listen('404', function()
{
    return Controller::call('errors@404');
});

This works fine (it calls the error controller's 404 action) if the URL doesn't exist (i.e. doesn't hit a controller). But when the path hits a controller, then it seems to ignore that event listener.

Do I need to do something to tell it to use that listener?

  • 写回答

1条回答 默认 最新

  • dongxiandi8313 2013-02-24 12:54
    关注

    If you are using a catch-all route - like:

    Route::controller(Controller::detect())
    

    Then you are correct - the event does not seem to be captured by the Events class. It might be a bug - I'm not sure.

    However for 404 it is a really easy fix - just change your base_controller:

    public function __call($method, $parameters)
    {
        return Response::error('404');
    }
    

    to

    public function __call($method, $parameters)
    {
        Event::fire('404');
    }
    

    and it works as expected

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理