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

覆蓋 Laravel 5 輔助函數(shù)

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

問題描述

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

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 中創(chuàng)建我自己的 helpers.php 來覆蓋 response() 輔助函數(shù)代碼>.

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

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

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.

我該如何解決這個問題?我只想在響應(yīng)數(shù)組中包含狀態(tài)代碼.

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

推薦答案

所有 Laravel 輔助函數(shù)都用這個邏輯編寫

All Laravel helper functions written with this logic

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

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

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';

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

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

相關(guān)文檔推薦

Laravel Eloquent Union query(Laravel Eloquent Union 查詢)
laravel querybuilder how to use like in wherein function(laravel querybuilder 如何在 where 函數(shù)中使用 like)
The Response content must be a string or object implementing __toString(), quot;booleanquot; given after move to psql(響應(yīng)內(nèi)容必須是實現(xiàn) __toString()、“boolean和“boolean的字符串或?qū)ο?移動到 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 中設(shè)置和獲取 Cookie)
主站蜘蛛池模板: 久久精品视频免费看 | 91在线看视频| 国产精品美女一区二区三区 | 久久一视频 | 国产精品免费福利 | 5060网一级毛片| 欧美美女一区二区 | 欧美亚洲国产日韩 | 国产人免费人成免费视频 | 免费黄色录像视频 | 天天草夜夜骑 | 免费一级毛片 | 欧美一卡二卡在线观看 | 好姑娘高清在线观看电影 | 亚洲精品99| 91精品国产91久久久久久密臀 | 午夜网站视频 | 在线第一页| 日韩一区二区三区在线观看 | 久久精片| 四虎永久免费地址 | 国产精品美女久久久av超清 | 国产精品福利网站 | 啪啪网页| 怡红院成人在线视频 | 天天夜天天操 | 国产在线精品一区 | 激情福利视频 | 日日人人 | 黑人巨大精品欧美一区二区免费 | 日韩久久久久久 | 中文字幕一区二区三区不卡 | 九九久久久久久 | 国产女人第一次做爰毛片 | 精品综合久久久 | 日本理论片好看理论片 | 亚洲国产一区视频 | 欧美一区二区三区四区五区无卡码 | 国产精品久久久久久238 | 神马久久春色视频 | 国产一级片 |