Login Register


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


In the controller you want to use:

   public function index()
        $sitedefaultcountryid = $this->viewVars['sitedefaultcountryid'];
        $states = $this->paginate($this->States, ['conditions' => ['States.country_id' => $sitedefaultcountryid]]);

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

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

Posted on: 2016-12-14 Please login/register to leave comments