如何在cakephp中创建一个函数来调用多个ctp文件

How to create one function to call multiple ctp file in cakephp

我创建了 12 个不同的函数和 12 个不同的 ctp 文件,用于将数据库中的值显示到我的网页中,使用 12 个月的 12 个选项卡,例如 1 月 ... 12 月。我想显示每个月的三个最高值。根据 cakephp,我们为不同的 CTP 文件创建了不同的函数。就像一月份一样,我创建了一个函数名称一月并创建了一个 CTP 文件名称 january.ctp,就像在剩下的 11 个月中,我使用月份名称创建了不同的函数,而 CTP files.Just 月份的值($month) 在每个函数中都不同,有没有办法编写一个函数并将变量发送到 12 个 ctp 文件中。我怎样才能让我的代码更有效率,我在下面添加了我的代码

<?php
namespace App\Controller;
use Cake\Core\Configure;
use App\Controller\AppController;
use Cake\ORM\TableRegistry; 

public function january()
{
      $userDetails=Configure::read('userdetails');
      $profile=Configure::read('profile');
      $this->loadModel('Users');
      $usersID = $this->Auth->user('id');
      $profileImageShow=$this->Users->getUploadPath($usersID);
      $categoryTable=TableRegistry::getTableLocator()->get('categories');
      $results=$categoryTable->find("all")->toList();
      $votes;
      $month  = 01;
      $year=date("Y");
      $categoriesArray;

      foreach($results as $result)
      {        
            $votesCount=$this->Sjvotes->find("all",[
                'conditions'=>['month'=> $month,'year'=>$year,'category_id'=>$result->id]])->select(['count' => $this->Sjvotes->find()->func()->count('*')])->group(['user_id','category_id'])->order(['count'=>'desc'])->limit(2)->toList();

            $categoriesArray[]=$result->id;

            $temp=0;
            foreach($votesCount as $key => $count)
            {
               if($temp!=$count->count)
               {
               $userVotes=$this->Sjvotes->find("all", ['conditions'=>['year'=>$year,'category_id'=>$result->id]])->
                select(['count' => $this->Sjvotes->find()->func()->count('*'), 'category_id','user_id','fname'=>'Users.firstname','lname'=>'Users.lastname', 'profile_image'=>'Users.profile_image'])
                ->contain(['Users'])
                ->group(['user_id','category_id'])
                ->having(['count'=>$count->count])
                ->order(['count'=>'desc'])
                ->toList();
                $temp=$count->count;

                if($key==0)
                {
                   $class="gold";
                }else 
                    if($key==1)
                    {
                        $class="silver";
                    }else 
                        {
                            $class="bronz";
                        }

                $votes[$result->id][$class]=$userVotes;
               } 
            }
      }     

        $voteToppers=$this->Sjvotes->find("all", ['conditions'=>['year'=>$year,'category_id in ' =>$categoriesArray]])->
            select(['count' => $this->Sjvotes->find()->func()->count('*'), 'category_id','user_id','fname'=>'Users.firstname','lname'=>'Users.lastname', 'profile_image'=>'Users.profile_image'])
            ->contain(['Users'])
            ->group(['user_id'])
            ->order(['count'=>'desc'])
            ->toList();
        $temp=0;
        $changes=0;
        foreach($voteToppers as $key => $topper)
        {
              if($temp!=$topper->count)
              {
                if($changes==0)
                {
                   $class="gold";
                }else 
                    if($changes==1)
                    {
                        $class="silver";
                    }else 
                        {
                            $class="bronz";
                        }

                    $temp=$topper->count;
                    $changes++;
              }
              if($changes>3)
              {
                break;
              }

              $topUsers[$class][]=$topper;
        }
       $this->set(compact("results"));
       $this->set(compact("votes"));
       $this->set(compact(["topUsers", "userDetails", "profile"]));

}

//For February 

public function february()
{
      $userDetails=Configure::read('userdetails');
      $profile=Configure::read('profile');
      $this->loadModel('Users');
      $usersID = $this->Auth->user('id');
      $profileImageShow=$this->Users->getUploadPath($usersID);
      $categoryTable=TableRegistry::getTableLocator()->get('categories');
      $results=$categoryTable->find("all")->toList();
      $votes;
      $month  = 02;
      $year=date("Y");
      $categoriesArray;

      foreach($results as $result)
      {        
            $votesCount=$this->Sjvotes->find("all",[
                'conditions'=>['month'=> $month,'year'=>$year,'category_id'=>$result->id]])->select(['count' => $this->Sjvotes->find()->func()->count('*')])->group(['user_id','category_id'])->order(['count'=>'desc'])->limit(2)->toList();

            $categoriesArray[]=$result->id;

            $temp=0;
            foreach($votesCount as $key => $count)
            {
               if($temp!=$count->count)
               {
               $userVotes=$this->Sjvotes->find("all", ['conditions'=>['month'=>$month,'year'=>$year,'category_id'=>$result->id]])->
                select(['count' => $this->Sjvotes->find()->func()->count('*'), 'category_id','user_id','fname'=>'Users.firstname','lname'=>'Users.lastname', 'profile_image'=>'Users.profile_image'])
                ->contain(['Users'])
                ->group(['user_id','category_id'])
                ->having(['count'=>$count->count])
                ->order(['count'=>'desc'])
                ->toList();
                $temp=$count->count;

                if($key==0)
                {
                   $class="gold";
                }else 
                    if($key==1)
                    {
                        $class="silver";
                    }else 
                        {
                            $class="bronz";
                        }

                $votes[$result->id][$class]=$userVotes;
               } 
            }
      }     

        $voteToppers=$this->Sjvotes->find("all", ['conditions'=>['month'=>$month,'year'=>$year,'category_id in ' =>$categoriesArray]])->
            select(['count' => $this->Sjvotes->find()->func()->count('*'), 'category_id','user_id','fname'=>'Users.firstname','lname'=>'Users.lastname', 'profile_image'=>'Users.profile_image'])
            ->contain(['Users'])
            ->group(['user_id'])
            ->order(['count'=>'desc'])
            ->toList();
        $temp=0;
        $changes=0;
        foreach($voteToppers as $key => $topper)
        {
              if($temp!=$topper->count)
              {
                if($changes==0)
                {
                   $class="gold";
                }else 
                    if($changes==1)
                    {
                        $class="silver";
                    }else 
                        {
                            $class="bronz";
                        }

                    $temp=$topper->count;
                    $changes++;
              }
              if($changes>3)
              {
                break;
              }

              $topUsers[$class][]=$topper;
        }
       $this->set(compact("results"));
       $this->set(compact("votes"));
       $this->set(compact(["topUsers", "userDetails", "profile"]));
}

//March 

public function march()
{
      $userDetails=Configure::read('userdetails');
      $profile=Configure::read('profile');
      $this->loadModel('Users');
      $usersID = $this->Auth->user('id');
      $profileImageShow=$this->Users->getUploadPath($usersID);
      $categoryTable=TableRegistry::getTableLocator()->get('categories');
      $results=$categoryTable->find("all")->toList();
      $votes;
      $month  = 03;
      $year=date("Y");
      $categoriesArray;

      foreach($results as $result)
      {        
            $votesCount=$this->Sjvotes->find("all",[
                'conditions'=>['month'=> $month,'year'=>$year,'category_id'=>$result->id]])->select(['count' => $this->Sjvotes->find()->func()->count('*')])->group(['user_id','category_id'])->order(['count'=>'desc'])->limit(2)->toList();

            $categoriesArray[]=$result->id;

            $temp=0;
            foreach($votesCount as $key => $count)
            {
               if($temp!=$count->count)
               {
               $userVotes=$this->Sjvotes->find("all", ['conditions'=>['month'=>$month,'year'=>$year,'category_id'=>$result->id]])->
                select(['count' => $this->Sjvotes->find()->func()->count('*'), 'category_id','user_id','fname'=>'Users.firstname','lname'=>'Users.lastname', 'profile_image'=>'Users.profile_image'])
                ->contain(['Users'])
                ->group(['user_id','category_id'])
                ->having(['count'=>$count->count])
                ->order(['count'=>'desc'])
                ->toList();
                $temp=$count->count;

                if($key==0)
                {
                   $class="gold";
                }else 
                    if($key==1)
                    {
                        $class="silver";
                    }else 
                        {
                            $class="bronz";
                        }

                $votes[$result->id][$class]=$userVotes;
               } 
            }
      }     

        $voteToppers=$this->Sjvotes->find("all", ['conditions'=>['month'=>$month,'year'=>$year,'category_id in ' =>$categoriesArray]])->
            select(['count' => $this->Sjvotes->find()->func()->count('*'), 'category_id','user_id','fname'=>'Users.firstname','lname'=>'Users.lastname','profile_image'=>'Users.profile_image'])
            ->contain(['Users'])
            ->group(['user_id'])
            ->order(['count'=>'desc'])
            ->toList();
        $temp=0;
        $changes=0;
        foreach($voteToppers as $key => $topper)
        {
              if($temp!=$topper->count)
              {
                if($changes==0)
                {
                   $class="gold";
                }else 
                    if($changes==1)
                    {
                        $class="silver";
                    }else 
                        {
                            $class="bronz";
                        }

                    $temp=$topper->count;
                    $changes++;
              }
              if($changes>3)
              {
                break;
              }

              $topUsers[$class][]=$topper;
        }
       $this->set(compact("results"));
       $this->set(compact("votes"));
       $this->set(compact(["topUsers", "userDetails", "profile"]));

}

//april

唯一的区别是 $month 变量的值?

public function january() {
    return $this->_month(1, 'January');
}

public function february() {
    return $this->_month(2, 'February');
}

// etc...

protected function _month($month, $monthName) {
    // Existing implementation of each month's function here,
    // except without initializing the $month variable

    $this->set(compact('results', 'votes', 'topUsers', 'userDetails', 'profile', 'month', 'monthName'));
    $this->render('month');
}

并将 january.ctp 重命名为 month.ctp,您现在可以使用 $month$monthName 变量来告诉您它是几月份的。

您可以做的一件事是。获取当前月份然后查看当前月份,然后根据当前月份发送给那个月份的ctp文件。像这样-

$month = date('M');
if ($month == 'Mar') {
    $this->render('/march');
}
if ($month == 'Jan'){
    $this->render('/jan'); 
}