2016-11-12 116 views
1

目前我正在为Laravel定制CMS,我需要获取某个刀片视图的所有可用部分。这些部分将以用户填充内容的形式呈现。Laravel 5.3刀片 - 获取可用部分

可以说我有这样的观点:

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

@endsection 

@section ('right_column') 

@endsection 

我会是很好,如果我可以检索某种阵列的这些部分,并将其呈现形式。任何人都知道现有方法可以实现这一点我在Laravel文件中找不到任何有用的方法。

如果不是,我会编写一个自定义方法(或某种)来检索这些部分并将其作为答案发布。

+0

好吧我找到了生成视图的ViewFactory,并且有一个名为'getSections'的方法。但是,当视图生成时,它会像段数组一样刷新一些变量。 –

回答

0

一个解决办法是这样的:

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]; 

} 

这将像返回:

[ 
    'left_column', 
    'right_column' 
] 

编辑

更改:preg_matchpreg_match_all

改变:return $matchesreturn $matches[0]