- 1. 概要
- 2. 法則をもって変換
- 3. blade ファイル名を取得
1. 概要
通常、「PHP」で、自身のファイル名を知るためには、「__FILE__」を使用するのですが、「Laravel」ではそれが通用しません。
「Lalavel blade __FILE__」で検索するとすぐに見つかります。
「Laravel」のドキュメントに
Note: Bladeビューの中では__DIR__や__FILE__を使わないでください。キャッシュされたコンパイル済みのビューのパスが返されるからです。
と書かれておりますでな。
「__FILE__」で取得すると「generated::boJJf5MbNTiZXNHP」てな、わけのわからんことになります。
本ページは、下記のサイトを参考にさせていただきました。
「caching — Laravelビューキャッシュを無効にするにはどうすればよいですか?」← リンク切れになってしまいました
2. 法則をもって変換
わたしは
app/Providers/RouteServiceProvider.php
および、その派生ファイルの中で
Route::any('URI的なもの', function (){ return view('パス/BLADE名');});
を書く際に、ある法則をもって、決めることにして、なんとかしのいできました。
しかし、コンテンツが増えていくと、例外が発生したりするんだなぁ・・・。
困ったもんだ。
3. blade ファイル名を取得
同じことを考える人がいるんですな。
やはり需要があるのでしょう。
参考サイトでやっているようです。
ただし、わたしのような未熟者には、ちょっと難しそうです。できるかしら?
と「ビューキャッシュを無効にする」って方向で調べていたのですが、そのうちに、わかっちゃいました。
「blade」ファイル名は
vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php
の
/**
* Get the evaluated contents of the view.
*
* @param string $path
* @param array $data
* @return string
*/
public function get($path, array $data = [])
{
$this->lastCompiled[] = $path;
// If this given view has expired, which means it has simply been edited since
// it was last compiled, we will re-compile the views so we can evaluate a
// fresh copy of the view. We'll pass the compiler the path of the view.
if ($this->compiler->isExpired($path)) {
$this->compiler->compile($path);
}
// Once we have the path to the compiled file, we will evaluate the paths with
// typical PHP just like any other templates. We also keep a stack of views
// which have been rendered for right exception messages to be generated.
$results = $this->evaluatePath($this->compiler->getCompiledPath($path), $data);
array_pop($this->lastCompiled);
return $results;
}
ここの、47行目、引数でわたってくる「$path」変数に、ファイル名がフルパスではいっています。
細かいことは省略しますが、これで、この変数を他へ使いまわせば、くっきり、ファイル名がわかることになります。
|