扩展Laravel 5响应门面 [英] Extend Laravel 5 Response Facade

查看:89
本文介绍了扩展Laravel 5响应门面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Laravel 5中扩展Response门面时出现命名空间问题.我在app目录下创建了一个名为Extensions\Facades的新文件夹树.在此文件夹中,我有一个名为AjaxResponse.php的文件,该文件包含以下内容:

I am getting a namespacing issue when trying to extend the Response facade in Laravel 5. I have created a new folder tree under the app directory called Extensions\Facades. In this folder I have a file called AjaxResponse.php which has the following contents:

<?php namespace App\Extensions\Facades;

use Illuminate\Support\Facades\Response;

class AjaxResponse extends Response{

    public static function send($code,$body,$http_code=200){

        parent::json( array(
                'status'=>(string)$code,
                'body' =>$body
            ) )->setStatusCode($http_code)->send();
        exit();

    }
}

我正在config/app.php中将其注册为服务提供商,就像我了解的那样:

I am registering this as a service provider in config/app.php like I understand I am supposed to:

providers=[
            //..normal stuff
            'App\Extensions\Facades\AjaxResponse',
]

这会引发未找到的类的常规名称空间错误:

And this is throwing the normal namespace error of class not found:

FatalErrorException in ProviderRepository.php line 150: 
Class 'App\Extensions\Facades\AjaxResponse' not found

有人可以阐明为什么找不到该类吗?

Can anyone shed any light on why the class is not found?

推荐答案

转到项目根文件夹并在终端类型中

Go to project root folder and in the terminal type

composer dump-autoload

那么一切都很好.创建新文件夹时,composer并不知道它,因此即使它们是psr-4命名空间,它也无法从其中自动加载文件.

Everything should be fine then. When you create a new folder, composer does not know about it, so it can not autoload files from it, even if they are psr-4 namespaced.

编辑此外,您还需要在别名数组下的config/app.php中声明外观的别名,而不是提供者之一:

EDIT Also you need to declare alias for your facade in config/app.php under aliases array, not the providers one:

 'AjaxResponse'   => 'App\Extensions\Facades\AjaxResponse',

这篇关于扩展Laravel 5响应门面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆