こんなこと誰かがやってそうなもんですけど、全然記事が見つからないので
自分で実装することにしました。今更こんな枯れたフレームワーク使ってるとこないか
もっと良い方法あったら教えてちょ
# ファイル出力テスト用のディレクトリを作成して書き込み権限を付与
mkdir /var/www/html/test/
sudo chmod -R 777 /var/www/html/test/
# ファイル出力テスト用のControllerを作成
touch /var/www/html/admin/application/controllers/tools/View.php
# ファイル出力テスト用のViewを作成(レンダリングにはTwigライブラリが必要です)
touch /var/www/html/admin/application/views/user/test/hello.twig.html
/var/www/html/admin/application/controllers/tools/View.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class View extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper("file");
}
public function test_output()
{
$data = "Hello World";
// FCPATH は "/var/www/html/admin/public" を返す( "/var/www/html/admin/public/index.php" で定義)
if (write_file(FCPATH . '../../test/hello.html', $data) == FALSE){
echo 'Unable to write the file';
}
else {
echo 'File written!';
}
}
public function test_output2()
{
$this->load->library('twig');
$filePath = "user/test/hello";
$data = array(
"test" => "Hello World2"
);
$html = $this->twig->render($filePath, $data);
if (write_file(FCPATH . '../../test/hello2.html', $html) == FALSE){
echo 'Unable to write the file';
} else {
echo 'File written!';
}
}
}
/var/www/html/admin/application/views/user/test/hello.twig.html
<!DOCTYPE html>
<html>
<head></head>
<body>
<p>{{ test }}</p>
</body>
</html>
http://www.example.com/admin/tools/view/test_output
にアクセスすれば、以下の場所にHTMLファイルが出力されます
cat /var/www/html/test/hello.html
Hello World
http://www.example.com/admin/tools/view/test_output2
にアクセスすれば、以下の場所にHTMLファイルが出力されます
cat /var/www/html/test/hello2.html
<!DOCTYPE html>
<html>
<head></head>
<body>
<p>Hello World2</p>
</body>
</html>