3

I want to be able to access a plugin page without having to login (for a cron job).

I have added this to the plugin's controller:

protected $allowAnonymous = true;

However, this url still requires login:

/admin/[plugin]

(If I login to admin, the above url works.)

If I try the url without /admin:

/[plugin]

The page doesn't work at all (logged in or not):

Page Not Found
The requested URL was not found on this server.

How do I make it so I can access my plugin page without login?

BTW, I also have this in the [plugin]Plugin.php file:

public function registerCpRoutes()
{
  return array(
    '[plugin]' => array('action' => '[Plugin]/[Action]'),
  );
}

2 Answers 2

2

It sounds like you just want a cron job to trigger an action on your plugin.

You don't need to register any Control Panel routes for that, so you can remove your registerCpRoutes method.

If your plugin only has one controller, the URL your cron job would want to target would be:

yoursite.com/actions/pluginHandle/pluginAction

You can read more about it in the plugin controller docs.

4
  • That worked! Thanks! In the plugin controller docs it might be helpful to add a cron link example that shows actions/ in the path because none of the current examples show actions/ in the path (because actions/ is not needed in any of the other examples): <input type="hidden" name="action" value="cocktailRecipes/ingredients/saveIngredient"> Commented Nov 19, 2015 at 3:33
  • Fair enough... I'll get to that in the morning!
    – Brad Bell
    Commented Nov 19, 2015 at 3:34
  • Thanks for the very helpful and responsive support as always! Commented Nov 19, 2015 at 3:41
  • And maybe also note that camel case is essential for the plugin handle, even though it's all lowercase in other areas. Commented Nov 19, 2015 at 3:56
2

If the cron jobs are triggered at the server where your Craft installation is hosted, it might be better to implement a consolecommand instead to execute the logic you want. They are well suited for that.

With a console command your crontab entry would look like:

*/1 * * * * php /opt/craft-app/current/craft/app/etc/console/yiic PLUGINNAME ACTIONNAME myParam=myValue

(where PLUGINNAME is the name of your plugin, and ACTIONNAME is a method defined in your consolecommand; myParam are arguments passed to your action)

An example of a plugin that makes use of a consolecommand: https://straightupcraft.com/craft-plugins/taskrunner

(beware if you make use of environmental specific variables you might have to do so in your plugin to get the right configuration)

Activation of 'cron actions' through 'url requests' may be considered bad practice;

  • you'd have to open up a section of your admin panel publicly, or implement another authentication mechanism (i.e. api key)
  • hence, the 'action' may be vulnerable to various kinds of attacks
  • by activation through cron you can log success and error output more easily to 'cron logs' or get notified by email on failure (through crontab settings)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.