【问题标题】:Defining content outside the controller?在控制器之外定义内容?
【发布时间】:2016-04-13 05:39:21
【问题描述】:

在 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 内容列表,并且控制器将使用该库或者我该怎么做?

【问题讨论】:

    标签: php laravel laravel-4 laravel-blade


    【解决方案1】:

    这仅适用于控制器吗?如果是这样,我将创建一个 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');
        }
    }
    

    【讨论】:

    • 不错的主意。有没有办法没有 HeadController 并创建一个类助手,在控制器文件中我可以命名空间类似于 App\Helper;
    • 是的,你可以这样做,只要确保你调整了代码,这样你就不会调用 $this-> header2(),但可能会调用 $helper->header2() 或者如果你依赖注入它 $this->helper->header2()
    【解决方案2】:

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

    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

    【讨论】:

    • 还有其他选择吗?我个人不喜欢在view composer中定义
    猜你喜欢
    • 2012-11-02
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2011-05-15
    相关资源
    最近更新 更多