我有/app/resources/views/category/index.blade.php
在index.blade.php
我想知道它位于“ category ”文件夹中。是否有laravel函数将返回当前视图的路径?
类似的东西:
$folder = view_path(); // "/category"
答案 0 :(得分:0)
在班级顶部添加名称空间:use Illuminate\Support\Facades\File;
运行:
$files = File::allFiles(resource_path('views'));
foreach ($files as $file) {
echo (string) $file, "\n";
}
或您的情况:
$files = File::files(resource_path('views/category'));
foreach ($files as $path => $value) {
echo $value->getPath() . "\n";
}
答案 1 :(得分:0)
您可以使用Laravel View Composers
视图作曲家是a时调用的回调或类方法 视图呈现。如果您有要绑定到视图的数据 每次渲染视图时,视图编辑器都可以帮助您进行组织 把这个逻辑放到一个地方。
在文件app/Providers/AppServiceProvider.php
中,修改public function boot()
方法
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(Application $app)
{
view()->composer('*', function($view) {
// get view paths from config - this is absolute path
$configPaths = config('view.paths');
// get view's full path
$absoluteViewPath = $view->getPath();
foreach($configPaths as $configPath) {
// see if current view's path matches path from config
// both are absolute paths
if ( strpos($absoluteViewPath, $configPath) !== false ) {
$path = str_replace($configPath, '', $absoluteViewPath);
// remove view name - from last position of `/`
$dir = substr($path, 0, strrpos( $path, '/'));
// attach to views
view()->share('view_folder', $dir);
// view matched, stop looking
break;
}
}
// if nothing was found
view()->share('view_folder', '');
});
}
在此之后,在任何刀片模板中,您现在都可以访问变量
{{ $view_folder }}