Manipulating Laravel html response just before sending it to browser

梦想的初衷 提交于 2020-01-02 06:51:14

问题


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

I have created facade

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);
    }
}

and in the controller action i use

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

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

most probably \Response related __toString method behaves strangely

any ideas? (thanks!)


回答1:


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


来源:https://stackoverflow.com/questions/31182355/manipulating-laravel-html-response-just-before-sending-it-to-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!