Running CI without a web server (e.g., for cron scripts or unit tests) |
[eluser]Unknown[/eluser]
I've created a little script which bootstraps the CI superobject and loader, allowing one to use CI outside a cgi/mod_php context and without a Controller object. This has been especially handy for cronjobs and unit tests. Since I take a different, more direct approach than what I saw on the wiki, I've posed my code here. Note that my approach does not require a web server or even php-cgi at all. To use it, modify the CI_REALBASE constant and $system_folder and $application_folder variables to point to your real CI install. Then simply require_once the fake-codeigniter.php file at the top of your script, then use get_instance() to access and start using the CI superobject. Additional notes: * The script assumes the use of PHP5. PHP4 will require modifications (you'll have to check the php version and load Base4 instead of Base5). * As little as possible is loaded, and the autoloader is not run, so you will have to load a lot of things yourself. * This was written for CI 1.6.3, but will probably work with 1.7.0 with little or no modification. * It makes some simplifying assumptions, so you may have to tweak things further if you have an unusual install. Code: <?php
[eluser]Phil Sturgeon[/eluser]
What are the benefits of using this method over these two: http://codeigniter.com/wiki/CI_on_the_command_line/ and http://phpstarter.net/2008/12/run-codeig...-line-ssh/ They both load a pretty normal CI instance and don't encounter any problems. Is this just for extra-light loading?
[eluser]Unknown[/eluser]
Quote:What are the benefits of using this method over these two? I wasn't able to find those--if I had I probably would have used one of them just to save time. However, the advantage of this method over those is the following: * Those still use the entire framework. They will run all hooks and load everything that would normally be loaded--which, depending on what you're doing, might be an advantage, but for unit tests it is a definite bummer. * Those require you to create controllers for everything you might run on the command line. That means if you have a web project, you will somehow need to restrict access to those controllers from a normal web instance, but make them accessible to the CLI context. (Best way is probably with a check for a constant only defined by the CLI.) This method allows you to use CI like a big library, and doesn't require controllers for anything.
[eluser]awells527[/eluser]
Quote:* Those require you to create controllers for everything you might run on the command line. That means if you have a web project, you will somehow need to restrict access to those controllers from a normal web instance, but make them accessible to the CLI context. (Best way is probably with a check for a constant only defined by the CLI.) That's the whole idea ![]() I believe testing for the presence of $_SERVER['REMOTE_ADDR'] is the best way to know if the script is being called from the web browser or the command line.
[eluser]Kabotyn[/eluser]
WOW! Thanks a lot! It's exacly what i need! I was trying to connect Joomla and CI, and that code helped me! |
Welcome Guest, Not a member yet? Register Sign In |