Laravel 5.3 Blade - 获取可用部分

Laravel 5.3 Blade - Get available sections

目前我正在为 Laravel 开发自定义 CMS,我需要在其中获取某个 blade 视图的所有可用部分。这些部分将以用户可以填充内容的形式呈现。

假设我有这样的看法:

// view.blade.php
@section ('left_column')

@endsection

@section ('right_column')

@endsection

如果我能在某种数组中检索这些部分并在表单中呈现它们,我会很高兴。任何人都知道现有方法可以做到这一点吗?我在 Laravel 文件中找不到任何有用的方法。

如果没有,我将编写一个自定义方法(或某种方法)来检索这些部分并post它作为答案。

解决方案可能是这样的:

public function get_view_sections ($view) // $view should be like page.index
{

    # Create file name and path
    #
    $file_name = preg_replace('/\./', '/', $view).'.blade.php';

    $path = resource_path('views/'.$file_name);


    # Get file content
    #
    $file = File::get($path);


    # Get sections
    #
    $matches = [];

    preg_match_all('/(?<=\@section\(\').*?(?=\'\))/', $file, $matches);


    return $matches[0];

}

这将 return 类似于:

[
    'left_column',
    'right_column'
]

编辑

更改:preg_matchpreg_match_all

更改:return $matchesreturn $matches[0]