问题
I'm working on a project based on the Laravel 4 framework. The framework is awesome, the website works great, no problem with that.
But I also have to write several scripts, mostly DB maintenance tasks that will be scheduled in a crontab.
I'm looking for a way to write these scripts in the same flavour as the web-site code. Or at least, re-use the models and if possible some other framework features.
Is there a way to do that ? Thanks in advance :)
Answer : Artisan Commands are indeed the way to go. Everything you need to know about these is available in the Artisan Commands documentation.
回答1:
What you are looking for are Laravel 'tasks'. You can write scripts with the full toolset of your Laravel project and run them from the command line using the 'artisan' tool.
回答2:
Of course.
You can even make a route for your cron job, and call the route in your cron job command. Something like this:
wget http://yourdomain.com/cron/your-job
This way, you can use everything that Laravel 4 provides.
来源:https://stackoverflow.com/questions/16220785/using-existing-eloquent-models-and-possibly-other-laravel4-features-in-cli-scr