Using an external library within Yii the right way

Hello, I’ve been building an app with yii for the past month or so and have stumbled on something I can’t seem to figure out.

I built a simple interface to store crontab entries in the database and execute them automatically via page loads. My approach uses a field that is regenerated each time a cron runs which stores the next "run date" into the database.

To do this I needed for a way to convert the crontab string into a timestamp so it can be stored in the database.

I found a solid class I could use, available here: github.com/mtdowling/cron-expression but I am struggling getting this working in Yii.

I managed to interact with the library by placing the files under a ‘cron’ directory under ‘components’ and I was able to load the files after adding the ‘protected.components.cron.*’ entry in my config file.

Despite being able to load the class and its dependencies I haven’t been able to get it working, the script seems to be interrupted when I try loading the main class and does not return any error. I can tell something is interrupting the processing because the page exits its loading at that point in time and will not execute any further code past this.

So my question is, what is the proper way of using an external library such as this one into yii so I can utilize its functions?

I apologize if this seems too trivial, this is my first time trying out yii.

Thanks in advance!

I think it’s okay to put this code in components, like you did, but I usually use ‘vendors’ or ‘extensions’ folder (or ‘libs’ folder) - I think it’s not that important and may be chosen depends on your exact project and taste.

Which is more important point in your situation is [color=#1C2837]

[/color]

[color=#1C2837]So I’d first figured out which error stops the execution - so check you have error_reporting switched on, yii debugging and logging switched on and start from that point. Or try to debug code execution with xDebug to see what’s going on. While debugging try to narrow down problematic place, finally you’d be able to find exact troublesome line of code.[/color]

Hi momon,

Welcome to the forum.

How do you deploy the library and how do you use it from your app?

Do you deploy it like the following using phar file?




protected

  components

    cron

      cron.phar



In this case, you should be able to use the library in the way the README shows.




require_once Yii::getPathOfAlias('application.components.cron') .  DIRECTORY_SEPARATOR . 'cron.phar';

$cron = Cron\CronExpression::factory('@daily');

...



Or do you deploy it like the following using the source files?




protected

  components

    cron

      AbstractField.php

      CronExpression.php

      ...



If you are using this style, then you should rename the directory from ‘cron’ to ‘Cron’.

Then I hope the autoloading of those classes will work.

I don’t know if it is required to include ‘application.components.Cron.*’ to ‘import’ property of the application configuration.

http://www.yiiframework.com/doc/guide/1.1/en/basics.namespace#namespace