Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

deadlockgB's avatar

Saving HTML contents of view to disk

Hi folks!

I tried to dump my routes for static page generation like this:

Route::get('/', function()
{
    File::put(public_path() . '/static/index.html', View::make('index'));
});

The file is created but empty. How can I dump the generated HTML to the file. When just returning the view (either via view('...'); or View::make('...');) my page looks good.

0 likes
12 replies
RobinMalfait's avatar

You can do it using

$html = View::make('index')->render();
// Or
$html = view('index')->render();

That will return the html itself

2 likes
deadlockgB's avatar

I tried both but the file is still empty. It's not a restriction problem since the file can be created + I am on XAMPP on Windows.

No clue where this comes from.

bashy's avatar

How about testing with file_put_contents()?

deadlockgB's avatar

file_put_contents() also produces an empty file.

Edit: If I use hardcoded text it works. But the rendered HTML is not getting stored (file_put_contents() and File::put())

pmall's avatar

What is the content of your view ?

bashy's avatar

Have you tested the ->render() returns a string?

deadlockgB's avatar

@pmall My view is simply a blade file which contains 2 sections and extends to the master blade template. So kinda like this:

<!--master.blade.php-->
<DOCTYPE html>
<html>
    <head>
        ....
        <title>@yield('title')</title>
        ...
    </head>
    <body>
        <div>
            ...
            @yield('content')
        </div>
    </body>
</html>

And the extending blade:

<!--index.blade.php-->
@extends('master')
@section('title', 'Inndex')

@section('content')
<div>
    ...
</div>
@stop 

@bashy : From the docs (http://laravel.com/api/5.1/Illuminate/View/View.html#method_render ) it should but testing like this:

    $html = view('index')->render();
    $res = $html === '' ? 'true' : 'false';
    return $res;

returns the page in the web browser but not true or false

pmall's avatar

So render seems like to be echoing the string instead of returning it.

What if you do this ?

ob_start();
view('index')->render();
$content = ob_get_contents();
ob_end_clean();

File::put(public_path() . '/static/index.html', $content);
deadlockgB's avatar

It sadly creates an empty index.html. Man I think I will just save the f*cking sites via Firebug.

bashy's avatar

Seems there another issue with your code somewhere or you haven't tested the saving of the file. Permissions?

This works

$view = view('view.name');

$contents = (string) $view;
// or
$contents = $view->render();

file_put_contents('filename.html', $contents);
1 like
deadlockgB's avatar

@bashy: I don't think it is an issue with persmission since something like

file_put_contents(public_path() . '/static/index.html', "lsdflaflöksd");

works.

I think I will try to get a fresh install of laravel. Something is fishy here.

bashy's avatar

Yeah sounds like a good idea, very strange!

Please or to participate in this conversation.