i'm a newbie in using codeigniter cli
i'm using xampp and my project located in
c:\xampp\htdocs\mycli\
my code is like this
public function myfunction($to = "WORLD"){
echo "HELLO {$to}!".PHP_EOL;
}
i trying to use this cmd line base on codeigniter tutorial on cli
c:\xampp\htdocs\my_cli\index.php mycontroller myfunction "test"
but it doesn't work it just showing index.php file. i also using htacess to remove the index.php in the url.
try the following code
cd /path/to/project
php index.php mycontroller myfunction test
Either you can execute php index.php controller action
or you can call it thorough the http protocol
wget http://example.com/example/test/bar/
curl http://example.com/example/test/bar/
Reference: http://mildcoder.com/handling-codeigniter-in-cli-and-cron-jobs/
step1: cmd
step2: check your cmd ->
"php -V"
if step2 got error: your php.exe is not in your sys's path{
step3:
c:\xampp\php\php c:\xampp\htdocs\my_cli\index.php mycontroller myfunction "test"
}
if step2 show like this: php 5.3.6 ...............{
step3 is:
php c:\xampp\htdocs\my_cli\index.php mycontroller myfunction "test"
}
Related
I'am trying to retrieve EXAMPLE_URL=www.google.com from .env in controller and everytime get null. Where is the problem because the same code works on another application. This function doesn't work after php artisan cache:clear.
Controller code
$hostname = env("EXAMPLE_URL");
dump($hostname);
You should not use env() outside of the config files.
Read: https://laravel.com/docs/8.x/configuration
You should add the env variable to a config file and use config('example.url');.
The example.php would look like:
return [
'url' => env('EXAMPLE_URL', 'https://example.com'),
];
Always use the app file as the middleman
see : https://laravel.com/docs/9.x/configuration
I am creating my own artisan command and I want to use ENV variables, but when I use $_ENV['VariableName'] I get and error.
local.ERROR: Undefined index: VariableName
The same code works perfectly in a controller and error as this one is not being generated.
I am creating my commands with php artisan make:command CommandName
How can I start using ENV variables there? Thank you! I want to use the variables in a private function which is inside:
class CommandName extends Command but outside the public function handle()
Since the .env file is not pushed to the repository, the best approach is to use config files instead. So in the config directory, create your custom file for example: custom.php with the following content:
<?php
return [
'variable' => env('VARIABLE_NAME', 'DEFAULT_VALUE')
];
and in your .env you should put:
VARIABLE_NAME=something
Then to use it you use config('custom.variable');
You can use the Laravel Helper to access environment variables with something like this:
env('VariableName')
you can also specify a default value if the environment variable is not set
env('VariableName', 'myName')
Laravel Docs 5.8 Helpers
Ok I looked at every other thread and have done exactly what they've done and what it says in the manual and I can NOT figure this out for the life of me.
The results of the cron job are being emailed to one of my emails. ALL it is doing is printing out the html markup of the layout... And printing the base page content... It's like it's not registering anything.
php /home/jdstable/public_html/dev/index.php cron decrease_pets_stats
That's my command line.. I tried replacing php with the user/local/bin/php thing as well and it didn't work. The thing is is that I have other cron jobs running off procedural PHP code that work FINE with php path/to/cron.php... But it won't work with CI..
My controller is Cron and my method is decrease_pets_stats..
//decrease pets stats
public function decrease_pets_stats() {
$this->load->model('Cron_model', 'cron');
$this->cron->decrease_pets_stats();
echo 'Decreased pet stats';
}
And here is the logic of the method:
//decrease pets stats
//runs every hour
public function decrease_pets_stats() {
$this->db->set('hunger', 'hunger - 5');
$this->db->set('happiness', 'happiness - 5');
$this->db->set('loyalty', 'loyalty - 5');
$this->db->update('user_creature');
}
Does anyone have any idea why it's just printing the layout markup? My constructor looks like this:
public function __construct() {
parent::__construct();
if( ! $this->input->is_cli_request()) show_error('Direct access is not allowed');
$this->load->model('Cron_model', 'cron');
}
And my parent constructor holds quite a bit of stuff (loading helpers and libraries along with getting the user information to appear on each page if they are logged in.
Does it matter if this is at the top of the controller before even opening the controller Cron class?
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
The default PHP install you are using was probably compiled as CGI-FCGI, not for CLI. It depends on your host and/or server, but you'll need to search for your PHP install for the command line interface, and then use that in your cron job. I had the exact same problem on Hostmonster, and my cron command ended up being:
/ramdisk/bin/php5-cli ~/public_html/sitefolder/index.php controller method
For me, the PHP I needed was in /ramdisk/bin/php5-cli.
For CodeIgniter 2.2.0
You can try this two method:
php-cli /home/username/public_html/index.php controller method
wget http://www.example.com/controller/method
or at your case
php-cli /home/username/public_html/index.php Cron decrease_pets_stats
wget http://www.example.com/Cron/decrease_pets_stats
It works fine with me..
Cheers!
Had the same issue and after trying whatever I could thing the obvious worked...
/usr/local/bin/php /absolute/path/to/index.php cron
/usr/local/bin/php /home/jdstable/public_html/dev/index.php cron decrease_pets_stats
This fixed it.
Here is solution first you need to find path from phpinfo document_root file name
php5 /home/abc/public_html/index.php folder_name controller function
I have created a shell script as follows
<?php
class EmailShell extends AppShell
{
public function main()
{
$this->out('Hello world.');
}
}
When i navigate to the Console folder in command line and type cake email i get the following error.
Error: Shell class EmailShell could not be found.
#0 C:\wamp\www\gitgrow\lib\Cake\Console\ShellDispatcher.php(167): ShellDispatche
r->_getShell('email')
#1 C:\wamp\www\gitgrow\lib\Cake\Console\ShellDispatcher.php(69): ShellDispatcher
->dispatch()
#2 C:\wamp\www\gitgrow\app\Console\cake.php(33): ShellDispatcher::run(Array)
#3 {main}
create a shell for use in the Console. For this example, we’ll create a simple Hello world shell. In you applications Console/Command directory create EmailShell.php. Put the following code inside it:
class EmailShell extends AppShell {
public function main() {
$this->out('Hello world.');
}
}
Then run this command :
Console/cake email
or
cake email
Run it at C:\wamp\www\gitgrow\app\. It should work.
cd C:\wamp\www\gitgrow\app
Console\cake email
If your shell class is in the right place, then it might be a problem that cake does not know where your app root is. You can specify this using the -app argument.
cake -app ../app email
See the following link about how to run Cake shells in cron:
http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html
Your cron command basically calls cd into the app directory and the cake command to run the shell together.
I am a newer to cakephp .I config the cakephp shell as the cakephp
handbook says,when I run the HelloShell with the command cake
Hello ,I got the error information as follows:
Error: Shell class HelloShell could not be found.
1#G:\htdocs\cakedemo\lib\Cake\Console\ShellDispatcher.php(191):ShellDispatcher>_getShell('hello')
2#G:\htdocs\cakedemo\lib\Cake\Console\ShellDispatcher.php(69):ShellDispatcher->dispatch()
3#G:\htdocs\cakedemo\app\Console\cake.php(33):ShellDispatcher::run(Array) {main}
my cakephp version :
Welcome to CakePHP v2.2.0-beta Console
App : Console
Path: G:\htdocs\cakedemo\app\Console\
anyone who is helpful can give me a advice,plea.
there is your mistake.
you should always be in your APP path to execute the cake console.
...app/>../lib/Cake/Console/cake MyShell
or (using the APP Console folder):
...app/>Console/cake MyShell
and MyShell should then be in ...app/Console/Command/.
Thats all there is to it.
Error: Shell class HelloShell could not be found appear because: typo mistake or run command at wrong directory.
Solution:
1. Setup path for php.exe, cake.exe
2. For example, my Cake website root is:
C:\tools\xampp1.8.3\htdocs\cakephp-2.5.5
Create new file in folder C:\tools\xampp1.8.3\htdocs\cakephp-2.5.5\app\Console\Command\HelloShell.php with content:
class HelloShell extends AppShell {
public function main() {
$this->out('Hello world.');
}
}
3. Open cmd, type:
cd /d C:\tools\xampp1.8.3\htdocs\cakephp-2.5.5\app
cake hello
We use hello in command line to call HelloShell class, because "Convention over configuration".
Reference:
http://book.cakephp.org/2.0/en/console-and-shells.html#creating-a-shell
Make sure you give cake folder path in /var/www/html/Console/cake.php
ini_set('include_path', $root . PATH_SEPARATOR . 'Cake' . $ds . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
Then go to root folder. In my case the location will be
/var/www/html/
then give the shell file name; hello is my shell name, that would be
/var/www/html/Console/cake hello
combining together
/var/www/html$ /var/www/html/Console/cake hello
Your shell will be executed.