Save page output as HTML with CakePHP

I’m currently working on a project that requires storing dynamically generated HTML files on a CDN. Without getting too far off topic, I need to dynamically create an HTML page with database data and then store that HTML file on a CDN (Amazon S3) which cannot parse PHP. Even if it could parse PHP, these files could conceivably be hit very many times and this solution cannot be load dependent.

I was looking for a way to output a view, and save it as an HTML file which could then be uploaded via the S3 API. In the CakePHP API, we see the Controller render method returns the output of the View model’s render method which returns a string of the HTML output to be loaded in the browser. But we don’t want to call the Controller’s render method as that will send the user’s browser to that page; instead we want to use the View’s render method so we get the string value that can be saved into an HTML file.

Assume we have a publish controller method which is called when the user wants to publish the HTML file. We also have a private method to generate the HTML and save it as a file in the local filesystem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
class VideosController extends AppController {

    protected function publish( $id ){
        $html_file = $this->generate( (int)$id );
        //do whatever needs to be done with the HTML file
        //for example, this is where I upload the file to S3
    }

    private function generate( $id ){
        //set whatever data is required to build the page;
        //be sure this is done before instantiating the View class so all
        //set variables are passed when you pass this controller object in the constructor
        $video = $this->Video->read( null, $id );
        $this->set('video', $video);

        //instantiate a new View class from the controller
        $view = new View($this);

        //call the View object's render method which will return the HTML.
        //Note, I'm setting the layout to HTML and telling the view to render the html.ctp file in /app/views/videos/ directory
        $viewdata = $view->render(null,'html','html');

        //set the file name to save the View's output
        $path = WWW_ROOT . 'files/html/' . $id . '.html';
        $file = new File($path, true);

        //write the content to the file
        $file->write( $viewdata );

        //return the path
        return $path;

    }
}

Haven’t thoroughly tested this yet, so let me know if you run into any problems. But so far, seems to be working well. Note, this isn’t really production ready as the generate method assumes success.

3 Comments

  1. It would be helpful if you can add a “$this->autoRender = false;” to your controller action’s last line. Else you get missing view exception.

    • kettle says:

      For sure, but I generally call that method from a shell or another action. I think a better approach would be for me to make it a protected function to make that clear.

  2. [...] Cut from the North I was looking for a way to output a view, and save it as an HTML file which could then be uploaded [...]

Leave a Reply to Save page output as HTML with CakePHPБлог Demokrite | Блог Demokrite