Assuming you use Laravel 5.2 you can use the render
method, then in Controller it would look something like, this is an example just to show how to use it, I changed the contents of $ data by a new output with return
:
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function home()
{
$v = view('welcome')->render(function ($obj, $data) {
return 'Hello world!';
});
return $v;
}
}
So just merge to the other link code getting something like:
function reduzirHtml($data)
{
$search = array(
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s' // shorten multiple whitespace sequences
);
$replace = array(
'>',
'<',
'\1'
);
return preg_replace($search, $replace, $data);
}
class UserController extends Controller
{
public function home()
{
return view('welcome')->render(function ($obj, $data) {
return reduzirHtml($data);
});
}
public function about()
{
return view('welcome')->render(function ($obj, $data) {
return reduzirHtml($data);
});
}
}
If you change the function to function reduzirHtml($obj, $data)
you can call directly:
return view('welcome')->render("\App\Http\Controllers\reduzirHtml");
Or you can even move the reduzirHtml
function to a file or method in a class and call it as in the example:
//Função
return view('welcome')->render("\reduzirHtml");
//Classe
return view('welcome')->render("\Namespace\Classe\reduzirHtml");