在控制器之外定义内容? [英] Defining content outside the controller?

查看:61
本文介绍了在控制器之外定义内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel中,控制器中的每个方法都将包含$heading['panelText']

In Laravel, every method in the controller will contain $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'));
    }

在刀片文件中,看起来像这样

In the blade file, it would look something like that

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

如您所见,控制器方法可能会有些混乱.我是否正在寻找一种更清洁的解决方案,而不必在控制器方法中为$heading['panelText']定义值?也许创建一个具有$heading内容列表的库,控制器将使用该库,或者我该如何处理?

As you can see the controller methods can get a bit messy. I am looking for a cleaner solution I don't have to define the value for $heading['panelText'] in controller methods? Maybe create a library to have a list of $heading content and the controller will use that library or how do I go about this?

推荐答案

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

Does this only applies on controllers? If so I would create a ControllerClass and extend all of my controllers from that. Something like this:

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

然后在您的控制器上,您可以执行以下操作:

Then yon your controller you could do something like this:

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

然后在您的控制器上,您可以执行以下操作:

Then on your controller you could do something like this:

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

这篇关于在控制器之外定义内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆