2016-11-14 3 views
0

テンプレート(必要に応じてすべてのjs、css、imgなどのファイルを含むindex.htmlを持つzip)を出力できるシステムを作成する必要があります。オプションのセット(メニュー1を含める、小さなフッターなどを使用)。テンプレート用SymfonyのzipへのTWIG出力の送信

私はコードブロックを置くことができるので、実際にブラウザに必要な外観を出力するのは難しくありませんが、レンダリングされたHTMLを保存したい場合はどうしたらいいですか?これはファイルの場所を変更する必要があるjsなどのように)代わりにジップに?

アイデア?現時点では、PHPで処理する必要があると思っています(コントローラー)とTWIGはあまり役に立たないかもしれませんが、それはもっと複雑になります。

答えて

0

は、だから私はこの1つを働いた、ここで必要な小枝はzip形式を出力するコントローラは、次のとおりです。

<?php 

namespace AppBundle\Controller; 

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

class DefaultController extends Controller 
{ 
    /** 
    * @Route("/{variableName}", name="homepage"}) 
    */ 
    public function indexAction($variableName = "Default") 
    { 
     //do whatever you need to get the template correct 

     //send rendered TWIG to a variable 
     $renderedhtml = $this->render('default/index.html.twig', [ 
      'variableName' => $variableName 
     ])->getContent(); 

     $zipName = "template.zip"; 
     $main = $this->get('kernel')->getRootDir() . '/../web/workingdir/'; 
     $vername = time(); //we use the exact time to get a unique dir name 

     //remove any projects older than a few minutes (5 in this case) 
     $dirHandle = opendir($main); 
     while($item = readdir($dirHandle)){ 
      if(is_dir($main . $item) && $item != '.' && $item != '..') { 
       if ((($item + (60 * 5)) < $vername)) { 
        system('/bin/rm -rf ' . escapeshellarg($main . $item)); 
       } 
      } 
     } 

     //make the project dir and populate it with files (in this case just the template as index.html, you would want to copy all other files here though) 
     mkdir($main.$vername); 
     file_put_contents($main.$vername."/index.html", $renderedhtml); 

     //create the zip 
     $zip = new \ZipArchive(); 
     $zip->open($zipName, \ZipArchive::CREATE); 
     $zip = $this->createZip($zip, $main.$vername); 
     $zip->close(); 

     //get the zip ready to return 
     header('Content-Type', 'application/zip'); 
     header('Content-disposition: attachment; filename="' . $zipName . '"'); 
     header('Content-Length: ' . filesize($zipName)); 
     readfile($zipName); 
    } 

    // Create a zip with zip object and root dir params (find and include sub-dirs with content) 
    private function createZip($zip, $source) { 
     $source = str_replace('\\', '/', realpath($source)); 

     if (is_dir($source) === true) { 
      $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST); 

      foreach ($files as $file) { 
       $file = str_replace('\\', '/', $file); 

       if(in_array(substr($file, strrpos($file, '/')+1), array('.', '..'))) 
       continue; 

       $file = realpath($file); 

       if (is_dir($file) === true) 
       { 
        $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); 
       } 
       else if (is_file($file) === true) 
       { 
        $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); 
       } 
      } 
     } 
     return $zip; 
    } 
} 

あなたは、このステージにそれを得れば、あなただけの作業に、他の必要なファイルを追加する必要があります私はそれが別の問題の下で行うことができると思う、これは、Webサイトをzipのhtmlにすることに関するものです。

これは誰かを助けることを望みます。

関連する問題