Using existing Eloquent models (and possibly other Laravel4 features) in CLI scripts?

痴心易碎 提交于 2019-12-12 03:09:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!