I need your help.

I've just found out that the class \Illuminate\View\Environment has a protected array $sections and a method to get it getSections() but it returns an empty array.

Here's how I tried to get it:

$view = View::make('pages');
$env = $view->getEnvironment();
$env->make('pages');

print($env->getSections());

And the result is Array( )

Where did I go wrong? Any suggestion will be appreciated.

有帮助吗?

解决方案

That's because when you call $env->make('pages'), the view isn't immediately processed. It's only processed when the render method on the $view object is called, which is done automatically when you return it as a response, via the __toString() method. The problem, though is that just after the page is rendered, still inside the render method, the $sections variable is cleared, using the flushSections method. So you actually never have access to it.

You could try to fool it if you call the incrementRender method before making the view, then make and render the view, get the sections and then finally decrementRender() and flushSections(), but this could bring unexpected results. Something like this:

// Fool it
$env->incrementRender();
$env->make('pages')->render();

$sections = $env->getSections();

// Clear it
$env->decrementRender();
$env->flushSections();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top