简体   繁体   English

在Kohana之外的文件中使用Kohana

[英]Using Kohana in File outside Kohana

Here's the situation: 情况如下:

I have a catch-all on my domain email (so *@domain.com) redirecting to a piping script located at /home/domain/scripts/piper.php. 我的域电子邮件(因此*@domain.com)重定向到/home/domain/scripts/piper.php上的管道脚本,因此具有一个“包罗万象”的功能。 This piper script is not within the Kohana ORM, but all of my other files are. 这个吹笛手脚本不在Kohana ORM内,但是我所有其他文件都在。 I want to try to use Kohana inside this piper.php file. 我想尝试在此piper.php文件中使用Kohana。

I have tried (unsuccessfully) all of the following: 我尝试了以下所有步骤(未成功):

Including Kohana 包括小花

I couldn't figure out what needed to be included, and more importantly how to override the url variable that Kohana uses to determine the right controller. 我无法弄清楚需要包括什么,更重要的是,如何覆盖Kohana用于确定正确控制器的url变量。 Also, this is a catch-all piper, so it isn't using HTTP (to my knowledge), so much as executing a command. 而且,这是一个万能的吹笛者,因此(据我所知)它不使用HTTP,而是执行命令。

Piping 管道

I tried piping to the following: 我尝试管道到以下内容:

/home/domain/public_html/index.php --uri="piper"

But cPanel makes this impossible, as you can only specify the destination script, and not the proper flags and such (unless I am missing something). 但是cPanel使得这不可能,因为您只能指定目标脚本,而不能指定适当的标记等(除非我遗漏了一些东西)。

PHP exec() PHP exec()

I tried using the following line: 我尝试使用以下行:

exec("php /home/domain/public_html/index.php --uri=\"/piper\"")

I was hoping that the stdin data would be maintained across the exec() command, but I could never get it to recognize the uri command, though I can run this on my localhost and it works just fine. 我希望stdin数据可以在exec()命令中维护,但是我永远无法获取它来识别uri命令,尽管我可以在本地主机上运行它,并且效果很好。

I was using http://www.coderelic.com/2011/10/creating-cron-jobs-in-kohana-3-x-is-a-piece-of-cake/ as a reference, but can't get anything to work. 我使用http://www.coderelic.com/2011/10/creating-cron-jobs-in-kohana-3-x-is-a-piece-of-cake/作为参考,但无法获取任何工作。

I'm happy with either one of these solutions such that I can see an incoming email, parse it, then send emails based on the parameters. 我对上述任何一种解决方案都很满意,这样我可以看到传入的电子邮件,进行解析,然后根据参数发送电子邮件。

Let me know if you need more information! 如果您需要更多信息,请与我们联系! I'm le stumped. 我很沮丧。

All you need to do is to: 您需要做的只是:

  1. modify your piper.php script to be a valid PHP class; 将您的piper.php脚本修改为有效的PHP类;
  2. place it in /application/classes/ folder; 将其放在/application/classes/文件夹中;

Kohana will automatically load your class file (like include ) during initialization. Kohana将在初始化期间自动加载您的类文件(例如include )。

Then you can use your piper class as usual class by $piper = new Piper; ... 然后,您可以通过$piper = new Piper; ...来像平常一样使用piper$piper = new Piper; ... $piper = new Piper; ... . $piper = new Piper; ...

UPD UPD

You have to serve your emails trough Kohana. 您必须通过Kohana服务您的电子邮件。

Create controller, for example pipe (route it with /pipe URL): 创建控制器,例如pipe (使用/pipe URL进行路由):

public function action_pipe() {

    $pipe = new Pipe; // This creates new Pipe object (your emails serving class)

    $pipe->serve(); // Sserve emails within `serve()` method of Pipe class

}

/home/domain/public_html/index.php --uri="piper" would be a valid way to do it. /home/domain/public_html/index.php --uri="piper"将是一种有效的方法。 If your host sucks and doesn't let you specify that, put it into a bash script instead and reference that. 如果您的主机很烂并且不允许您指定它,则将其放入bash脚本中并引用它。

If you are on any recent version of kohana (3.2 or 3.3), a better way to do this would be to use Minion to run the command line task. 如果您使用的是任何最新版本的kohana(3.2或3.3),则更好的方法是使用Minion运行命令行任务。 This is what Minion was designed for. 这就是Minion的目的。

Although admittedly, I'm not sure if these other answers are correct because I can't figure out how to reproduce the results. 尽管承认,但我不确定其他答案是否正确,因为我不知道如何重现结果。

What ended up working for my situation was to create a Controller_Piper class that is called in the /home/domain/scripts/piper.php . 最终适合我的情况的是创建了一个Controller_Piper类,该类在/home/domain/scripts/piper.php中被调用。 What I did was to copy the code from /home/domain/public_html/index.php and changed the following: 我所做的是从/home/domain/public_html/index.php复制代码并更改了以下内容:

echo Request::factory("/piper")
->execute()
->send_headers(TRUE)
->body();

This loads the piper controller and executes everything very nicely. 这将加载piper器控制器,并很好地执行所有操作。 Not sure if it's the cleanest, but it does work. 不知道它是否是最干净的,但是它确实有效。

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

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