Created
September 17, 2014 05:53
-
-
Save hirayama-evolni/f2afc102afc57745ff7b to your computer and use it in GitHub Desktop.
Laravelで、cssとjsをbladeに通す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cssとjs | |
function tmptmp_proc_assets($path, $is_css) | |
{ | |
if ($is_css) { | |
// css | |
$content_type = 'text/css'; | |
} else { | |
// js | |
$content_type = 'text/javascript'; | |
} | |
$base = public_path(); | |
$path = $base . '/' . $path; | |
if (!file_exists($path)) { | |
return Response::make("not found", 404); | |
} | |
// ファイルの読み込み | |
$contents = File::get($path); | |
// Bladeのコンパイル | |
$compiled = Blade::compileString($contents); | |
$tmpfile = tempnam(sys_get_temp_dir(), "XXXXX"); | |
file_put_contents($tmpfile, $compiled); | |
// phpをコンパイル | |
ob_start(); | |
include $tmpfile; | |
$result = ob_get_clean(); | |
unlink($tmpfile); | |
$res = Response::make($result, 200); | |
$res->header('Content-Type', $content_type); | |
return $res; | |
} | |
Route::get('proc_js/{path}', function($path) { | |
$res = tmptmp_proc_assets($path, false); | |
return $res; | |
})->where('path', '.*'); | |
Route::get('proc_css/{path}', function($path) { | |
$res = tmptmp_proc_assets($path, true); | |
return $res; | |
})->where('path', '.*'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment