【问题标题】:Phalcon - Calling function from other controllerPhalcon - 从其他控制器调用函数
【发布时间】:2015-02-25 03:51:03
【问题描述】:

如何从 Phalcon PHP 框架中的另一个控制器调用函数。这是 CakePHP 的示例http://sherwinrobles.blogspot.com/2013/02/cakephp-calling-function-from-other.html

【问题讨论】:

    标签: php controller phalcon


    【解决方案1】:

    这已经测试过了

    对于不使用 CakePHP 的人 另一种方法是创建一个辅助文件夹并编写操作,在这种情况下为方法。

    public/index.php

    添加路径助手

    $loader->registerDirs(
        [
            APP_PATH . '/helper/'
        ]
    );
    

    在应用中添加辅助订单

    └── apps
        ├── controllers
           └─ exampleController.php  
        ├── models
        └── helpers
           └─ myHelper.php
        ...
    

    在 myHelper.php 中

     <?php
    
        use Phalcon\Di\Injectable;
    
        class myHelper extends Injectable
            {
              function myNameFunction() {   
              // here you should write your function
    
            }
        }
    

    在你想要调用其他动作的 exampleController 中,在这种情况下是函数

    <?php
    
    use Phalcon\Mvc\Controller;
    
    class exampleController extends Controller {
      public function yourAction() {
        //your code   
        //there are 2 ways of calling the function. either use this one
        $myHelper = new myHelper();
        $myHelper->myNameFunction();
    
        //or this one
        (new UnmatchHelper())->myNameFunction();
      }
    }
    

    【讨论】:

      【解决方案2】:

      根据您提供的链接,据我所知,没有直接方法可以使用请求对象在另一个控制器中调用函数。然而,实例化控制器和调用函数将像在 CakePHP 中那样工作

      $newController = new \MyNS\Controllers\NewController();
      $newController->myFunc();
      

      如果你愿意,你可以在控制器内部使用静态函数并调用它

      \MyNS\Controllers\NewController::myFunc();
      

      【讨论】:

      • Tnx Nikolaos :) 我认为这种方式应该比使用请求对象更快
      • 这个可行,但如果是 $_POST 请求呢?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      相关资源
      最近更新 更多