[英]CakePHP - Shell command can't bypass beforeFilter
I'm trying to build a cronjob that calls a specific action in a controller. 我正在尝试建立一个cronjob来调用控制器中的特定动作。 The shell script that I write doesn't seem to be able to bypass the controller's beforeFilter call.
我编写的Shell脚本似乎无法绕过控制器的beforeFilter调用。
I thought of using session but there's no luck so far. 我考虑过使用会话,但到目前为止还没有运气。 Here is the code:
这是代码:
<?php
if (!isset($_SESSION)) session_start();
class MyShell extends AppShell
{
public function main() {
$_SESSION['toBypassBeforeFilter'] = "true";
CakeLog::write('debug', 'Calculating WIP...');
$this->requestAction('/account/wip_reports');
CakeLog::write('debug', 'Finishing WIP calculations...');
}
}
The debug log writes out the first one before the action call. 调试日志在动作调用之前写出第一个。
2015-10-05 02:08:49 Debug: Calculating WIP...
But not the second one when that action call is done. 但是,当该动作调用完成时,第二个不是。 What have I missed?
我错过了什么?
EDIT 编辑
It looks like that requestAction() call to the route is not recognizable even though I can still run it in the browser. 看起来对路由的requestAction()调用是无法识别的,即使我仍然可以在浏览器中运行它。 Maybe it should have been a different route?
也许应该是一条不同的路线?
Generally speaking using requestAction
is an indicator of poor application architecture. 一般来说,使用
requestAction
指示应用程序体系结构不佳。 The right approach to running cron jobs with CakePHP is to write a cli process that calls the methods that actually do things directly, not having a cli process that simulates a http request. 使用CakePHP 运行cron作业的正确方法是编写cli进程 , 该cli进程直接调用实际执行操作的方法,而没有cli进程模拟http请求。
In a cli context, the shell is a controller - controller code should always be kept to a minium. 在CLI环境中,外壳是控制器-控制器代码应始终保持最小。 Whereas currently you've got this:
而目前您已经拥有了:
class MyShell extends AppShell
{
public function main()
{
$this->requestAction('/account/wip_reports');
}
}
And this: 和这个:
class AccountsController extends AppController
{
public function wip_reports()
{
... some code ...
}
}
In principle what you should have is this: 原则上,您应该具有以下内容:
class MyShell extends AppShell
{
public function main()
{
$account = ClassRegistry::init('Account');
$account->something();
}
And this: 和这个:
class AccountsController extends AppController
{
public function wip_reports()
{
$this->Account->something();
}
}
With all the relevant logic in a model: 在模型中包含所有相关逻辑:
class Account extends AppModel
{
public function something()
{
... some code ...
}
}
In this way the shell does not depend on, or even interact with, the controller at all - it just calls the same methods the controller action method does. 这样,shell根本不依赖于控制器,甚至根本不与控制器交互-它仅调用与控制器操作方法相同的方法。
Well, you're abusing a controller for something it is not thought for and you now have to face the results of that abuse. 好吧,您正在滥用控制器,这是我们不希望的事情,现在您必须面对滥用的结果。 A session won't be available in a shell environment either.
会话在Shell环境中也将不可用。
Refactor your code so that the business logic resides in a model or another class that is callable from a shell environment and has no dependencies on a web environment. 重构代码,以使业务逻辑驻留在可从Shell环境中调用且不依赖于Web环境的模型或其他类中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.