【问题标题】:phalcon controller indexAction break downphalcon 控制器索引动作分解
【发布时间】:2016-04-23 12:17:38
【问题描述】:

我是 Phalcon 框架的新手。我只是对它有了基本的了解。每个控制器都有具有多个特定操作的方法。我写了一个巨大的 indexAction 方法,但现在我想用多个私有方法分解它,以便我可以重用这些功能。但是,当我尝试创建任何没有操作后缀的方法时,它会返回错误(找不到页面)。
如何将其分解为多个方法?

【问题讨论】:

    标签: php phalcon


    【解决方案1】:
    <?php
    
    use Phalcon\Mvc\Controller;
    
    class PostsController extends Controller
    {
        public function indexAction()
        {
            $this->someMethod();
        }
    
        public function someMethod()
        {
            //do your things
        }
    }
    

    【讨论】:

      【解决方案2】:

      控制器必须具有后缀“Controller”而动作必须具有后缀“Action”。控制器示例如下:

      <?php
      
      use Phalcon\Mvc\Controller;
      
      class PostsController extends Controller
      {
          public function indexAction()
          {
      
          }
      
          public function showAction($year, $postTitle)
          {
      
          }
      }
      

      要调用另一个方法,你可以直接使用它

      <?php
      
      use Phalcon\Mvc\Controller;
      
      class PostsController extends Controller
      {
          public function indexAction()
          {
              echo $this->showAction();
          }
      
          private function showAction()
          {
              return "show";
          }
      }    
      

      Docs.

      【讨论】:

      • 我知道我必须创建带有 Action 后缀的动作。但我想将我的 indexAction 分解为多个方法,以便我可以减小 indexAction 方法的大小并在下次重用这些方法
      【解决方案3】:

      你到底想要什么?答案对我来说似乎微不足道。

      class YourController extends Phalcon\Mvc\Controller
      {
          // this method can be called externally because it has the "Action" suffix
          public function indexAction()
          {
             $this->customStuff('value');
             $this->more();
          }
      
          // this method is only used inside this controller
          private function customStuff($parameter)
          {
      
          }
      
          private function more()
          {
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-24
        • 1970-01-01
        • 2021-12-11
        • 1970-01-01
        • 2019-01-02
        • 1970-01-01
        相关资源
        最近更新 更多