获取laravel视图的文件夹名称 [英] Get folder name of laravel view
问题描述
我有/app/resources/views/category/index.blade.php
在index.blade.php
中,我想知道它位于文件夹"类别"中.是否有一个laravel函数可以返回当前视图的路径?
And in index.blade.php
I want to know that it was in the folder "category". Is there a laravel function that will return the path of the current view?
类似的东西:
$folder = view_path(); // "/category"
推荐答案
您可以使用Laravel 查看作曲家
You can use Laravel View Composers
视图编写器是在以下情况下调用的回调或类方法: 视图已呈现.如果您有想要绑定到视图的数据 每次渲染视图时,视图编辑器都可以帮助您组织 将该逻辑放在一个位置.
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.
在文件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', '');
});
}
此后,您现在可以在任何刀片服务器模板中访问变量
After this, in any blade template you will now have access to a variable
{{ $view_folder }}
这篇关于获取laravel视图的文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!