在将Laravel HTML响应发送到浏览器之前对其进行处理 [英] Manipulating Laravel html response just before sending it to browser

查看:224
本文介绍了在将Laravel HTML响应发送到浏览器之前对其进行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将最终输出发送到浏览器之前,对其进行适当处理的正确方法是什么? (laravel 5.*)

What is the proper way to manipulate final output before sending it to browser? (laravel 5.*)

我创建了门面

namespace App\Facades;

use Illuminate\Support\Facades\Response as ResponseFacade;
use Illuminate\Http\Response as ResponseHttp;

class Response extends ResponseFacade
{

    public static function viewMod($view, $data = [], $status = 200, array $headers = [])
    {   
        $output = \Response::view($view, $data, $status, $headers);

        return some_manipulating_function($output);
    }
}

在控制器操作中我使用

return viewMod("my_view_file", array $view_data);

但是我收到损坏的输出(http响应标头添加到html或在html之前)

but i receive corrupted output (http response headers are added to/ prepended to html)

\ Response相关的__toString方法的行为很奇怪

most probably \Response related __toString method behaves strangely

有什么想法吗? (谢谢!)

any ideas? (thanks!)

推荐答案

您可以使用 AfterMiddleware 像下面的文档示例一样,

You can use AfterMiddleware like below example from docs,

<?php

namespace App\Http\Middleware;

use Closure;

class AfterMiddleware
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        // Perform action

        return $response;
    }
}

这篇关于在将Laravel HTML响应发送到浏览器之前对其进行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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