简体   繁体   English

如何从独立的php文件调用zend控制器动作?

[英]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 我有一个控制器IndexController.php,其中的操作是这样的

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 现在,我需要使用独立的php文件在IndexController.php中调用动作“ indexmailAction”

The php file is indextest.php php文件是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. 这为我解决的问题是,它消除了Apache进程的产生。 The solution is great because I can access the some Controller/Action needed that I would from the URL. 该解决方案很棒,因为我可以从URL中访问所需的某些Controller / Action。

In almost any ZF1 based app, you can copy your index file and keep everything the same and just comment out the following line. 在几乎所有基于ZF1的应用程序中,您都可以复制索引文件并保持所有内容不变,而只需注释掉以下行即可。

$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. 不幸的是,您很快就会发现自己对应用程序拥有的许多文件的访问受到限制,并且感觉访问所需文件的唯一方法是通过Controller / Action。

Instead, I use the following in a new file below $application->bootstrap() ( still removing the $application->run() ): 相反,我在$ application-> bootstrap()下面的新文件中使用以下内容(仍在删除$ 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. 最后,您将获得一个隔离的PHP文件,该文件会引导与Web上的主index.php相同的所有内容,但是您可以根据需要手动触发控制器/操作,从而使您可以更轻松地使用ZF1来访问其余文件您可以访问它们。

Controllers are designed to be used in an MVC, not by scripts. 控制器被设计为在MVC中使用,而不是通过脚本使用。 Your controller should assemble request variables, direct them to models and return an HTTP response of some sort. 您的控制器应组装请求变量,将其定向到模型并返回某种HTTP响应。 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. 无论如何,只要您坚持,就可以实例化控制器类并像调用其他任何类一样调用方法,只要您注入MVC​​可能具有的任何依赖项即可。

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 PS:也许您应该考虑创建一个宁静的api来处理来自应用程序外部的调用

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. 好的,我现在明白了您的需求,实现此目标的最佳方法是调用url而不是文件(例如website.com/emails/send),如果您担心安全性,可以使用一些预共享的密钥来确保请求来自您,将其与请求一起发送,并检查您的操作是否正确。

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/) 如果要在操作的多个位置使用逻辑,则应将其放入操作帮助器或非常通用的代码中,然后放入自定义库(/ library / custom /)

NB: The authentication would be better suited in a plugin rather than the pre-dispatch method in every controller. 注意:身份验证将更适合于插件,而不是每个控制器中的预调度方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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