inv*_*tus 4 php cron silverstripe
嗨,我想通过cronjob执行一个函数来启动csv导入.目前,通过访问浏览器tld.de/Update中的控制器来触发导入
控制器的代码为http://pastie.org/8351266
如何通过Cronjob 执行函数init()?
谢谢!
在SilverStripe中,您还可以通过在命令行中运行cli-script.php来访问可通过HTTP访问的任何路由
还有sake一个围绕cli-script.php的bash包装器(但需要安装清酒)
因此,从项目目录中,您可以运行两个将执行相同操作的命令(在本例中,运行dev/build):
php framework/cli-script.php dev/build
sake dev/build
Run Code Online (Sandbox Code Playgroud)
请参阅silverstripe的命令行说明文档:http://doc.silverstripe.org/framework/en/topics/commandline
问题的第二部分(如何从控制器调用方法)实际上更多的是在silverstripe中进行路由的问题,而与调用它的方式无关(cronjob)
我假设您的控制器是Page_Controller或其子类(因此绑定到SiteTree模型),然后为您完成路由(它采用您在CMS中设置的URL).所以让我们看一些示例代码,并假设您有一个包含URLSegment的页面about:
class Page_Controller extends ContentController {
private static $allowed_actions = array('something');
public function init() {
// the init method will run before every action
// this means this code will run, no matter if you visit /about or /about/something
}
public function index() {
// this is the default action (this code is optional and can be removed),
// and will be called if you visit website.com/about
return $this;
}
public function something() {
// this is the somethingaction,
// and will be called if you visit website.com/about/something
// do something here
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以调用run来获得结果index():
php framework/cli-script.php about
Run Code Online (Sandbox Code Playgroud)
这样得到的结果是something():
php framework/cli-script.php about/something
Run Code Online (Sandbox Code Playgroud)
注意:init方法本身不能通过URL访问,它是在操作之前运行的"设置"
注意:除了index()必须通过添加它们之外的所有操作$allowed_actions(还要注意你需要在添加后刷新= 1)要$allowed_actions重新加载配置缓存)
编辑:这实际上是对你的第一个问题的回应,看到你的代码示例后,这个补充:
对于独立控制器,它的工作方式相同,只需要定义路由,并确保$Action在路由中something()可以调用
你可以在没有Silverstripe清酒的情况下做到这一点.安装curl并通过cronjob调用URL,即:
0 0 * * * curl --silent http://tld.de/Update
Run Code Online (Sandbox Code Playgroud)
执行此操作的正确方法是编写Silverstripe任务,并从任务中调用控制器.我没有测试过这段代码,但它会是这样的:
class YourTask extends BuildTask {
public $description = "...";
//...
public function run($request) {
YourController::init();
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用以下方法调用它:
0 0 * * * /path/to/framework/sake dev/tasks/YourTask
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3289 次 |
| 最近记录: |