久久久久久久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)
主站蜘蛛池模板: 精品国产黄a∨片高清在线 www.一级片 国产欧美日韩综合精品一区二区 | 亚洲欧美在线观看 | 99在线免费视频 | 99精品久久| 久久久久久久国产 | jdav视频在线观看免费 | 91视频大全 | 97色在线观看免费视频 | 中国毛片免费 | 日本成人综合 | 亚洲精品电影网在线观看 | 国产精品精品久久久久久 | 欧美xxxx日本 | 美女久久 | 中文字幕1区2区3区 日韩在线视频免费观看 | 欧美日韩电影一区 | 日韩精品一区二区三区视频播放 | 日韩综合在线播放 | 国产精品久久久久久 | 天堂网av在线 | 黄视频国产 | 国产偷自视频区视频 | 亚洲视频二区 | 久久久久1| 91精品在线播放 | 欧美日韩免费一区二区三区 | 精品久久久久久久久久 | 精品99久久 | 成人黄在线观看 | 中文字幕精品一区 | 久久久久久久久久久国产 | 免费污视频 | 91精品国产91久久久久久最新 | 精品www | 国产乱人伦精品一区二区 | 亚洲三区视频 | xx视频在线 | 国产小视频自拍 | 色www精品视频在线观看 | 欧美日韩一区二区在线 | www.久久久久久久久久久久 |