简体   繁体   中英

How to call a zend controller action from an independent php file?

I am having a controller IndexController.php in which action is something like this

class IndexController extends CustomControllerAction {


public function preDispatch() {


    if (!$this->view->authenticated) {


        $this->_redirect('/users/login');


    }


}

public function indexemailAction() {

  //somecode which calculates certain things

}

}

NOw,I need to call the action "indexmailAction" inside the IndexController.php with an independent php file

The php file is indextest.php

 <?php
   //Need to write some code to call indexmailAction in IndexController.php

 ?>

What should I write in this file ......

Thanks in advance

I know this is a few years old, and this may not be the intended use of the classes/functions, but I've found the following quite useful in isolated files that are called from the command line.

The problem this solves for me is that it eliminates spawning of Apache processes. The solution is great because I can access the some Controller/Action needed that I would from the URL.

In almost any ZF1 based app, you can copy your index file and keep everything the same and just comment out the following line.

$application->run();

Anything below this line you can access with your autoloaders etc. It's crude, but it works. Unfortunately, you'll soon find yourself with limited access to a lot of the files your application has, and the feeling the only way you can access the files needed is through a Controller/Action.

Instead, I use the following in a new file below $application->bootstrap() ( still removing the $application->run() ):

$front = Zend_Controller_Front::getInstance();

// You can put more here if you use non-default modules
$front->setControllerDirectory(array(
        'default' => APPLICATION_PATH.'/controllers'
));

$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setNeverRender(true);

Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

$req = new Zend_Controller_Request_Http("http://anydomain.tld/controller/action");

// Example just to see how this can be extended
$req->setParam("someVar", "someValue");

$front->setRequest($req);

$front->dispatch();

In the end you have a isolated PHP file that bootstraps everything the same as your main index.php for the web, but you can manually trigger a controller/action as needed, giving you easier access to the rest of the files with how ZF1 intended you to access them.

Controllers are designed to be used in an MVC, not by scripts. Your controller should assemble request variables, direct them to models and return an HTTP response of some sort. Your scripts should act directly on the models instead.

Anyhow, if you insist, you can instantiate a controller class and call methods just like any other class as long as you inject any dependencies that the MVC would have.

You should not have to call a controller action for this, your logic should reside in your models. Then you can create a new instance of your model and invoke the appropriate methods. example :

require_once '/path/to/mymodel.php';
$mymodel = new Mymodel();
$data = $mymodele->fetchAll();

PS: Maybe you should think of creating a restful api to handle calls from outside your application

UPDATE :

ok, I see now what you need, The best way to achieve it is to call a url instead of a file (eg website.com/emails/send), if you are worried about security you can use some preshared key to make sure the request comes from you, send it with the request and check if it's correct in your action.

If you want logic used in multiple places in your actions, then it should go in an action helper or if very generic code, then in a custom library (/library/custom/)

NB: The authentication would be better suited in a plugin rather than the pre-dispatch method in every controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM