更新时间:2023-11-26 10:23:52
您可以使用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 }}