I built a plugin for Craft 3 and need to run one of its actions via CRON. I know I can set this up as a console command, and will do that, but before I get there, I'd like to solve this issue.
I noticed my CRON was failing, and when I attempted to run the URL myself, at http://campaigns.gcx.loc/actions/gcxCampaignTools/stats/syncCampaignsRemote
, I got this error:
yii\base\InvalidRouteException: Unable to resolve the request "gcxCampaignTools/stats/syncCampaignsLocal". in /Users/Chad/Sites/gcx-campaigns/vendor/yiisoft/yii2/base/Module.php:537
Stack trace:
#0 /Users/Chad/Sites/gcx-campaigns/vendor/craftcms/cms/src/web/Application.php(299): yii\base\Module->runAction('gcxCampaignTool...', Array)
#1 /Users/Chad/Sites/gcx-campaigns/vendor/craftcms/cms/src/web/Application.php(566): craft\web\Application->runAction('gcxCampaignTool...', Array)
#2 /Users/Chad/Sites/gcx-campaigns/vendor/craftcms/cms/src/web/Application.php(278): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#3 /Users/Chad/Sites/gcx-campaigns/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#4 /Users/Chad/Sites/gcx-campaigns/public_html/index.php(21): yii\base\Application->run()
#5 {main}
Next yii\web\NotFoundHttpException: Page not found. in /Users/Chad/Sites/gcx-campaigns/vendor/craftcms/cms/src/web/Application.php:570
Stack trace:
#0 /Users/Chad/Sites/gcx-campaigns/vendor/craftcms/cms/src/web/Application.php(278): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#1 /Users/Chad/Sites/gcx-campaigns/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#2 /Users/Chad/Sites/gcx-campaigns/public_html/index.php(21): yii\base\Application->run()
#3 {main}
The action works from a front end link with the following code when I am logged in (user must be logged in to use the entire app, so login is required to run the action as well):
<a href="/actions/gcx-campaign-tools/stats/sync-campaigns-local">Sync Campaigns</a>
Because it worked when logged in, I thought I may need to allow anonymous in the controller, but that was already in place. I tried both protected $allowAnonymous = true;
and protected $allowAnonymous = ['actionSyncCampaignsRemote'];
with the same results.
Here is a screenshot of my plugin file structure and controller actions: screenshot
As you can see there I did put URL: /actions/gcx-campaign-tools/stats/sync-campaigns-remote
in the code comments when I built this. Loading that URL produces the same error.
What am I missing here?
protected $allowAnonymous = ['sync-campaigns-remote'];