Cakephp

Cakephp 3 define variable in AppController, use in other controllers and views

Cakephp 3 define variable in AppController, use in other controllers and views


Cakephp 3 define variable in AppController, use in other controllers and views

In the appController in Cakephp 3 you can do the following:

        $site = $this->Sitesettings->get('1');
        
        $sitename = $site->site;
        $this->set('sitedefaultname', $sitedefaultname);
        
        $sitedescription = $site->description;
        $this->set('sitedefaultdescription', $sitedefaultdescription);
        
        $sitekeywords = $site->keywords;
        $this->set('sitedefaultkeywords', $sitedefaultkeywords);
        
        $sitedefaultcountryid = $site->country_id;
        $this->set('sitedefaultcountryid', $sitedefaultcountryid);
        
        $sitedefaultstateid = $site->state_id;
        $this->set('sitedefaultstateid', $sitedefaultstateid);

THEN

In the controller you want to use:

   public function index()
    {
        $sitedefaultcountryid = $this->viewVars['sitedefaultcountryid'];
        $states = $this->paginate($this->States, ['conditions' => ['States.country_id' => $sitedefaultcountryid" style="padding:10px;">);

        $this->set(compact('states'));
        $this->set('_serialize', ['states']);
    }

Also you can use it an a view by just using the variable like $sitedefaultcountryid;

Published: 14th December 2016 by

Adverts