I would like to create my own php artisan command so when I write
php artisan env:[variable]
I want to get that variable, so I can check fast if I'm getting the right variable from .env file
Is this possible? How to do it?
You can create a console command with:
php artisan make:console
As per the docs.
You are probably better off creating a static signature and passing in the field you want to return.
protected $signature = 'env:return {field}';
This will allow you to type php artisan env:return APP_DEBUG.
You can retrieve the input like so:
public function handle()
{
$envVariable = $this->argument('field');
//
}
You can then write some code to output the field, perhaps something like:
$this->info(env($envVariable));
This could all be condensed down into something like the below, as an example only:
protected $signature = 'env:return {field}';
public function handle()
{
$this->info(env($this->argument('field')));
}
I would suggest you read the docs I linked above as this will help you understand what is happening here a lot more.
This should get you started though, and you can then shape it to be exactly what you need.
Related
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
I have created the following Artisan command in Laravel:
class draft_cron extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'draft_cron';
//etc
And in start/artisan.php I added:
Artisan::add(new draft_cron);
However, when I try to run the command, I get an error saying Class 'draft_cron' not found and pointing to the above line from start/artisan.php.
Any ideas what the problem might be?
2 things: 1 is what user RDelorier said. DraftCron instead of draft_cron. The other thing is, did you do composer dump-autoload?
Most likely culprit is PSR cant figure out where the class is, any particular reason you used snake_case over StudlyCase?
I'm creating a custom artisan command (foo) for my Laravel 4 application. I can see from the userguide how to accept arguments and options, e.g.
php artisan foo argument --option
But what if I want to have a custom artisan command that has many 'methods', in the same style as some built-in artisan commands, e.g. migrate:install?. I want to make something like:
php artisan foo:baz argument --option
What is the recommended way to implement different methods based on the : colon separator? All I've found to try so far is to make an entirely new artisan command for each 'method'. Is there a more efficient way?
You're correct, you do need a new Artisan command (or rather a class) for each method. However all you have to do is register each file in app/Console/Kernel.php, if you decide to change the syntax later than Laravel will pick it up any changes to $signature automatically without you needing to rename any files.
For the Laravel example you mention, migrate, there's a directory with a separate file for each command name (including the one that has no colon):
/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations
migrate -> MigrateCommand.php
migrate:install -> InstallCommand.php
migrate:refresh -> RefreshCommand.php
migrate:reset -> ResetCommand.php
migrate:rollback -> RollbackCommand.php
migrate:status -> StatusCommand.php
If you have code you want to reuse (DRY) note that if you examine the above commands, some of them use traits, e.g. Illuminate\Console\ConfirmableTrait – which contains a confirmToProceed() method which, if it's running in production, will ask the user if they really want to continue.
NB: the Artisan syntax changed in 5.1, from $name (with a rather complicated way of specifying arguments and options) to the much simpler $signature, it's backwards compatible. More info
You just have to set the name:
protected $name = 'foo:baz';
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
Hey all. I am writing a program that will transform some data in our database, and then call Doctrine to build YAML files from said Mysql Database structure. I have Doctrine working from within PHP. However I can't figure out how to call the CLI commands from within PHP. Following is the Doctrine 2 CLI command that does what I need.
php ./doctrine orm:convert-mapping --filter="users" --from-database yml ./test
This command works from the Linux command line, but how to I do this same thing via Doctrine objects? I don't want to just use the PHP exec statement to send a command to the shell. I wish to use the Doctrine object model.
Don!:
Apparently this is not a very common programming method. However, I have used Doctrine from PHP by calling it via the PHP EXEC command. I know you said that you would not like to do it this way. However, it actually works quite well. Below is an example of such a solution.
$cmd_string = "php ./doctrine orm:generate-entities --generate-annotations=1 --regenerate-entities=1 $this->entity_file_dir";
$result = array();
exec($cmd_string, &$result);
Hope this helps,
-Don!
I stumbled upon this question when trying to execute a command directly from a PHP script, without using the CLI.
Particularly, I was needing to call orm:ensure-production-settings.
Each Doctrine command has its own class: http://www.doctrine-project.org/api/orm/2.4/namespace-Doctrine.ORM.Tools.Console.Command.html
I solved it the following way:
$entityManager = ...; // Get the entity manager somehow in your application
// Creates the helper set
$helperSet = \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
// Initializes the desired command and sets the helper set
// In your case it should be ConvertMappingCommand instead
$command = new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand();
$command->setHelperSet($helperSet);
// Initializes the input
// Alternatives: http://api.symfony.com/2.0/Symfony/Component/Console/Input.html
$input = new \Symfony\Component\Console\Input\ArgvInput(); // Input coming from the CLI arguments
// Initializes the output
// Alternatives: http://api.symfony.com/2.0/Symfony/Component/Console/Output.html
$output = new \Symfony\Component\Console\Output\ConsoleOutput(); // Prints the output in the console
// Runs the command
$command->run($input, $output);
I'm new to Doctrine so I'm not exactly sure how this works, but it does. Any comment is appreciated.