How to call function from another controller in Phalcon PHP framework. Here is example for CakePHP http://sherwinrobles.blogspot.com/2013/02/cakephp-calling-function-from-other.html
Based on the link you provided, to my knowledge there is no direct way to call a function in another controller using the request object. However instantiating the controller and calling the function will work just it does in CakePHP
$newController = new \MyNS\Controllers\NewController();
$newController->myFunc();
If you want you can use a static function inside the controller and call it
\MyNS\Controllers\NewController::myFunc();
-
1Tnx Nikolaos:) i think this way should be faster than using request object – booldog Dec 29 '14 at 20:05
-
1
This is already Tested
For the people that aren't using CakePHP another way to do this would be to do a helper Folder and write actions, in this case methods.
public/index.php
Add the path helper
$loader->registerDirs(
[
APP_PATH . '/helper/'
]
);
Add a helper Order in apps
└── apps
├── controllers
└─ exampleController.php
├── models
└── helpers
└─ myHelper.php
...
In myHelper.php
<?php
use Phalcon\Di\Injectable;
class myHelper extends Injectable
{
function myNameFunction() {
// here you should write your function
}
}
In exampleController where you want to call the other action, in this case function
<?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();
}
}