Assuming you use Laravel 5.2 you can use the method render
, then in Controller would look something like, this is an example just to show how to use it, I switched the $data content for a new output with the 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 match the other code /a/102364/3635 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)
can call directly:
return view('welcome')->render("\\App\\Http\\Controllers\\reduzirHtml");
Or you can even move the function reduzirHtml
for 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");
I would mark this as the correct one because it is smarter, I mean it detects if the output is really html and on top of that is organized in a separate file and at the same time is automated.
– Guilherme Nascimento
Your answer is good yes. I only answered with alternative of a "global treatment"
– Wallace Maxters
I know :) ... I just meant that your being smarter is the best alternative, but it doesn’t mean mine is bad. The problem with my answer is who uses it, the thing is "loose", a user with less experience would probably end up disorganizing things or even replicating several function, this for same inexperience ;)
– Guilherme Nascimento