在 Laravel 中检查 blade.php

Check for blade.php in Laravel

你好不累

有没有办法检查 Laravel 中的 blade.php 文件?

因为我想在寻址前检查文件是否存在

你可以试试这样的

if(view()->exists($view)){
    return view($view)->render();
}
return "404 page not found";

在这里你可以知道文件是否存在于 Laravel

 if (\View::exists('some.view')) { 

 }

来自此 link 的引用 https://webdevetc.com/programming-tricks/laravel/laravel-blade/how-to-check-if-a-blade-view-file-exists/

实际上,您不需要此检查,因为它不是动态元素。但是如果你有理由需要这样做,你可以简单地使用 php 自己的功能 file_exists("bla.blade.php");。它会 return 真或假。

要完成@kamran-khalid 的回答,您还可以仅在视图存在时在blade 中包含一个部分

{{-- include `view.name` only if it exists --}}
@includeIf('view.name', ['status' => 'complete'])
{{-- include the first partial that exists --}}
@includeFirst(['custom.admin', 'admin'], ['status' => 'complete'])

文档中的更多信息:https://laravel.com/docs/8.x/blade#including-subviews