久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

覆蓋 Laravel 5 輔助函數

Overwrite laravel 5 helper function(覆蓋 Laravel 5 輔助函數)
本文介紹了覆蓋 Laravel 5 輔助函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我經常使用 response() 助手,我只是將數據和一條消息返回給用戶.現在我還必須包含 http 狀態代碼,但我不想更改每個響應(無論如何這可能很糟糕).

I'm using the response() helper very often and I just return the data with a message to the user. Now I have to include the http status code as well, but I don't want to change every response (which is likely bad anyway).

所以我試圖通過在 app/Http/helpers.php 中創建我自己的 helpers.php 來覆蓋 response() 輔助函數代碼>.

So I'm trying to overwrite the response() helper function by creating my own helpers.php within app/Http/helpers.php.

當我將它添加到我的 Composer 文件時,它會首先從框架自動加載當前的 helpers.php,當我在 autload 包含在 bootstrap/global.php 之前添加它時,我將無法使用 app() 和其他 Laravel 函數.

When I add it to my composer files, it does autoload the current helpers.php from the framework first and when I add it before the autload include in bootstrap/global.php I wont be able to use the app() and other Laravel functions.

我該如何解決這個問題?我只想在響應數組中包含狀態代碼.

How would I be able to solve this issue? I just want to include the status code as well in the response array.

推薦答案

所有 Laravel 輔助函數都用這個邏輯編寫

All Laravel helper functions written with this logic

if ( ! function_exists('response'))
{
    function response($content = '', $status = 200, array $headers = array())
    {
         // function body
    }
}

首先 Laravel 檢查這個函數是否存在,如果存在,Laravel 不會再次定義這個函數(否則會拋出致命錯誤).因此,如果您將在自動加載器包含 vendor/laravel/framework/src/Illuminate/Foundation/helpers.php 文件之前定義您的函數,您可以定義自定義響應函數.

for first Laravel check is this function exists, if it exists, Laravel will not define this function again(otherwise it will throw fatal error). So if you will define your function before autoloader include vendor/laravel/framework/src/Illuminate/Foundation/helpers.php file, you can define your custom response function.

不幸的是,沒有辦法說 composer 首先加載你的 autoload.files 部分,然后是 laravel autoload.files.但是你可以做一些小黑客......

Unfortunately there is no way to say composer load first your autoload.files section, then laravel autoload.files. But you can do small hack ...

打開 bootstrap/autoload.php 文件并在自動加載器之前包含您的文件

open bootstrap/autoload.php file and include your file before autoloader

// file with your custom helper functions
require __DIR__.'/../app/app/Http/helpers.php'; 
require __DIR__.'/../vendor/autoload.php';

這篇關于覆蓋 Laravel 5 輔助函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Laravel Eloquent Union query(Laravel Eloquent Union 查詢)
laravel querybuilder how to use like in wherein function(laravel querybuilder 如何在 where 函數中使用 like)
The Response content must be a string or object implementing __toString(), quot;booleanquot; given after move to psql(響應內容必須是實現 __toString()、“boolean和“boolean的字符串或對象.移動到 psql 后給出) - IT屋-程
Roles with laravel 5, how to allow only admin access to some root(Laravel 5 的角色,如何只允許管理員訪問某些根)
Laravel Auth - use md5 instead of the integrated Hash::make()(Laravel Auth - 使用 md5 而不是集成的 Hash::make())
How to set and get Cookie in laravel(如何在 Laravel 中設置和獲取 Cookie)
主站蜘蛛池模板: 性网站免费| 日韩欧美在线观看视频网站 | 一级黄色av电影 | 人人九九精 | 欧美 日韩 中文 | 91九色porny首页最多播放 | 成人精品毛片 | 国产精品www | 欧美国产精品久久久 | 亚洲一区二区在线免费观看 | 欧日韩在线 | 一区二区国产精品 | 日韩91 | 中文字幕久久久 | 夜夜干夜夜操 | 中文字幕高清免费日韩视频在线 | 国产精品www | 毛片在线看片 | 国产成人网 | 国产美女精品视频免费观看 | 龙珠z在线观看 | 久久国产综合 | 欧美日韩国产一区 | 亚洲在线日韩 | 国产精品久久久久久久久婷婷 | 成人久久网 | 一区二区三区av | 国产亚洲精品综合一区 | 在线观看亚洲专区 | 亚洲精品久久久久久久久久吃药 | 一区二区三区在线观看视频 | 亚洲视频免费播放 | 久在线| 男女羞羞视频在线观看 | 国产精品美女久久久久久久网站 | www.五月天婷婷.com | 第一av| 久久国产精品视频 | 一区二区av | 欧美精品一二三区 | 免费激情av|