اذهب إلى المحتوى

السؤال

نشر

في Laravel كل متحكم controller سيحتوي على ['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'));
    }

في ملف الواجهة blade، سيكون الكود كالتالي:

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

ما احتاجه هو طريقة لاستعمال  ['heading['panelText$ داخل المتحكم Controller، هل سأحتاج إنشاء مكتبة لفعل ذلك؟

 

Recommended Posts

  • 1
نشر (معدل)

يمكننا استعمال ControllerClass وتوريثه من قبل Controllers أخرى، بهذه الطريقة:

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

ليكون المتحكم Controller مكتوب على هذا الشكل:

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

يمكن استعمال طريقة ثانية تفي بنفس الغرض، وهي استعمال Helper Class:

<?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
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');
    }
}
'][] = $panelText;
        }

        return $this;
    }

    public function loadView($view){
public function pageName2(){  
        return $this->helper->header2()->loadView('mockup/pagename2');
    }

    public function customPage3(){
        //setting on the controller

        $panelTexts = [
            "Some Random line One fo
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');
    }
}
r page 3",
            "Some Random Line Two for page 3",
        ];

        return $this->helper->setPanelText($panelTexts)->loadView('mockup/pagename3');
    }
}
        return View($view)->withHeading($this->heading);
    }
}

ليكون المتحكم Controller:

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

        $panelTexts = [
            "Some Random line One for page 3",
            "Some Random Line Two for page 3",
        ];

        return $this->helper->setPanelText($panelTexts)->loadView('mockup/pagename3');
    }
}

 

تم التعديل في بواسطة E.Nourddine

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...