定义控制器外的内容?

时间:2016-01-08 13:54:45

标签: php laravel laravel-4 laravel-blade

在Laravel中,控制器中的每个方法都包含 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的值?也许创建一个库以获得$this->generateUrl($route);内容列表,控制器将使用该库或我该如何处理?

2 个答案:

答案 0 :(得分:1)

这仅适用于控制器吗?如果是这样,我将创建一个ControllerClass并从中扩展我的所有控制器。像这样:

//controller for all partials
app.controller('mainController', function($scope,$location) {
    $scope.go = function (path) { 
        $location.path();
    };
});

然后你的控制器可以做这样的事情:

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);
    }
}

答案 1 :(得分:0)

您可以创建一个将自动将其附加到所有视图的作曲家。

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