Определение содержимого вне контроллера?

В Laravel каждый метод в контроллере будет содержать $heading['panelText']

Например:

  public function pageName1() { $heading['panelText'][] = "Content Here one"; $heading['panelText'][] = "Content Here Two"; $heading['panelText'][] = "Content Here Three"; return View('mockup/pagename1', compact('heading')); } public function pageName2() { $heading['panelText'][] = "Some Random one"; $heading['panelText'][] = "Some Random Line Two"; return View('mockup/pagename2', compact('heading')); } 

В файле клинка это выглядело бы примерно так

  @foreach($heading['panelText'] as $content) <p>{{$content}}</p> @endforeach 

Как вы можете видеть, методы контроллера могут немного запутаться. Я ищу более чистое решение. Мне не нужно определять значение для $heading['panelText'] в методах контроллера? Может быть, создать библиотеку, чтобы иметь список содержимого $heading и контроллер будет использовать эту библиотеку или как мне это сделать?

Это относится только к контроллерам? Если это так, я бы создал ControllerClass и расширил все мои контроллеры от этого. Что-то вроде этого:

 class HeadController extends Controller { protected $heading = []; public function header1(){ $this->heading['panelText'][] = "Content Here one"; $this->heading['panelText'][] = "Content Here Two"; $this->heading['panelText'][] = "Content Here Three"; return $this; } public function header2(){ $this->heading['panelText'][] = "Some Random one"; $this->heading['panelText'][] = "Some Random Line Two"; return $this; } public function setPanelText(array $panelTexts){ foreach($panelTexts as $panelText){ $this->heading['panelText'][] = $panelText; } return $this; } public function loadView($view){ return View($view)->withHeading($this->heading); } } 

Затем на вашем контроллере вы можете сделать что-то вроде этого:

 class YourController extends HeadController{ public function pageName1(){ return $this->header1()->loadView('mockup/pagename1'); } public function pageName2(){ return $this->header2()->loadView('mockup/pagename2'); } public function customPage3(){ //setting on the controller $panelTexts = [ "Some Random line One for page 3", "Some Random Line Two for page 3", ]; return $this->setPanelText($panelTexts)->loadView('mockup/pagename3'); } } 

Альтернатива со вспомогательным классом:

 <?php namespace Your\Namespace; use View; class Helper { protected $heading = []; public function header1(){ $this->heading['panelText'][] = "Content Here one"; $this->heading['panelText'][] = "Content Here Two"; $this->heading['panelText'][] = "Content Here Three"; return $this; } public function header2(){ $this->heading['panelText'][] = "Some Random one"; $this->heading['panelText'][] = "Some Random Line Two"; return $this; } public function setPanelText(array $panelTexts){ foreach($panelTexts as $panelText){ $this->heading['panelText'][] = $panelText; } return $this; } public function loadView($view){ return View($view)->withHeading($this->heading); } } 

Затем на вашем контроллере вы можете сделать что-то вроде этого:

 <?php namespace Your\Controllers\Namespace; use Your\Namespace\Helper; class YourController extends Controller{ protected $helper; public function __construct(Helper $helper){ $this->helper = $helper; } public function pageName1(){ return $this->helper->header1()->loadView('mockup/pagename1'); } public function pageName2(){ return $this->helper->header2()->loadView('mockup/pagename2'); } public function customPage3(){ //setting on the controller $panelTexts = [ "Some Random line One for page 3", "Some Random Line Two for page 3", ]; return $this->helper->setPanelText($panelTexts)->loadView('mockup/pagename3'); } } 

Вы можете создать композитор, который автоматически привяжет его ко всем вашим представлениям.

 view()->composer('*', function($view) { $panelText = [ 'Content Here one', 'Content Here Two', 'Content Here Three' ]; return $view->with('panelText', $panelText); }); 

Вы можете больше узнать о композиторах представления здесь: https://laravel.com/docs/5.2/views#view-composers