如何在Phalcon PHP框架中从另一个控制器调用函数。下面是CakePHP的例子 http:/sherwinrobles.blogspot.com201302cakephp-calling-function-from-other.html。
根据你提供的链接,据我所知,没有直接的方法可以在另一个控制器中使用request对象调用函数。然而,实例化控制器并调用函数,就像在CakePHP中一样。
$newController = new \MyNS\Controllers\NewController();
$newController->myFunc();
如果你想,你可以在控制器内部使用一个静态函数,并调用它的
\MyNS\Controllers\NewController::myFunc();
这已经被测试过了
对于没有使用CakePHP的人来说,另一种方法是做一个帮助文件夹,然后写动作,在这种情况下,方法。
publicindex.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();
}
}