Laravel run package artisan command in controller - php

I need to run this command when user change something in translation file
php artisan export:messages-flat
I need to add it in may controller
so I'm using this code
\Artisan::call('export:messages-flat');
but it return error saying that
The command "export:messages-flat" does not exist.
but when I
php artisan list
it's in the list
I also try to run other command
\Artisan::call('cache:clear');
and it works
this is the package I'm using link
kindly help me, sorry for may poor english

The package only allows you to run the commands from the CLI. You can see from the source code, they only register the Artisan commands if the application is running from the console.
As an alternative, you can call ExportLocalization::export()->toFlat() according to their documentation.

Related

Deployer PHP - Custom command task always failed

in my Symfony 5.1 project I am trying to declare my own task which I would like to use when deploying. I created it like this:
task('update_database', function () {
if($_ENV['DATABASE_URL'])
{
cd('{{release_path}}');
writeln('<info>Updating database</info>');
run('{{bin/php}} {{bin/console}} d:d:c --if-not-exists');
run('{{bin/php}} {{bin/console}} doctrine:schema:update --force');
}
})->desc('Make database updates');
The "cd" and the "writeln" work. But every time I try to run a command it always fails
I tried with just the command php bin/console instead of using {{bin/php}} {{bin/console}}.
I even tried a silly php bin/console cache:clear command to see, but same error.
Yet I am in the right directory.. I followed this tutorial https://www.codepicky.com/php-automatic-deploy/
And I do the exact same thing though
UPDATE :
I connected to my server, I went to the last release of my project, and when I tried commands, absolutely nothing was happening. No mistake, but they didn't do anything. Even if I try the command to create an entity, it is "ignored".
The -vvv flag didn't give anything either
Here is what I have in the console:

OVH CRON: How to execute a Symfony command?

How to execute a Symfony command with OVH cron?
I created a command on my symfony project:
php bin/console cron:test
I defined my Cron in the OVH table "Scheduled Tasks - Cron":
Command: cron/test.sh
Language: Other
test.sh is executable (chmod 700).
In test.sh I do not know what to write.
I tested several code found on the internet without success, including this one (with a php file):
OVH cron jobs / Symfony Command
I am using php 7.1.
What is the logic that applies to find this code? Thanks in advance for the help.
I got my answer. Thank you #Tomasz for your help.
phpinfo() showed me the way.
The solution:
#!/bin/bash
/usr/local/php7.1/bin/php /homez.ovhNumber/myWebsite/symphonyProject/bin/console cron:test
In command field you should be able to enter command you want to execute. But because you don't know from where it will be executed you have to specify full path to command for example:
php /var/www/my-project/bin/console cron:test
But, I'm not familiar with OVH Tools so if you need to create bash file which will be run by cron, then your file should like like this:
#!/bin/bash
php /var/www/my-project/bin/console cron:test
Here is the same principal with the full path to your bin/console file.

Laravel Artisan - reload .env variables or restart framework

I'm trying to write a command as a quickstart to build a project. The command asks for input for database details and then changes the .env file. The problem is that the command needs to do some Database queries afterwards, but the .env variables are not reloaded.
My question would be, is there a way to reload or override .env variables runtime. And if not, is there a way to call another Artisan command freshly, so framework bootstraps again?
In my command I tried doing $this->call('build:project') in my actual command, but even in the second call the variables are not reloaded.
Is there a way to achieve this without making the user manually call multiple commands?
Thanks!
i had the same problem with reloaded .env variables, and i fixed it with this command line which allow you to clear the configuration :
php artisan config:clear
Hope that helped. Regards.
Laravel uses config files which take data from .env file. So, what you can do is to override configuration values at runtime:
config(['database.default' => 'mysql']);
Try to clear cache it helped me (couldn't ssh into the server)
Is there a {app route}/bootstrap/cache/config.php file on the production server? Delete it.
This helped me
As OP I'm trying to bootstrap a Laravel project build by running console command and asking for the database credentials in the middle of the process.
This is a tricky problem and nothing I read was able to fix it : reset config, cache, Dotenv reload, etc... It seems that once the console command / operation is initialized the initial database connection is kept all over until the end.
The working solution I found is to bypass, after database modification are done, this cached state by using native shell exec command and passing the php artisan command as parameter :
passthru('php artisan migrate');
So, the order will be :
php artisan project:build (or whatever is your console command)
prompt the user for database credentials
replace values in .env file (your search & replace algorythm)
run php artisan config:cache
passthru('php artisan migrate'); ro run your migrations
shell_exec would do the same but in silent mode while passthru will return the output generated by console.
https://www.php.net/manual/en/function.passthru.php
Done successfully on Laravel 8.52

How to use composer, artisan ...(Laravel) php commands manually

I have one problem I have no ssh access to the server, so I cannot use php artisan, composer and other commands.
As I can quess they do nothing other than modifying files or just copying php src files to specific directories.
In order to understand that process better and because of no access via ssh to the server I am looking for tutroial, manual or an article how can I perform this commands manually.
For example I need to execute
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
What should I do in this case, it would be grate to find some document describes what should I do manually to get the same result.
Laravel provides a handy Facade for artisan commands.
Just use Artisan::call('your-command') from where you need.
Example:
Route::get('artisan-command/{command}', function($command) {
Artisan::call($command);
});
Your URL looks like this: http://yourhost.com/artisan-command/db:seed
More specific for your use-case:
Route::get('vendor-publish/{provider}', function($provider) {
Artisan::call('vendor:publish', ['provider' => $provider]);
});
And the URL: http://yourhost.com/vendor-publish/Tymon\JWTAuth\Providers\JWTAuthServiceProvider
Reference: Artisan Console in the Laravel Docs
You can execute command lines from your app. You can do something like this:
Route::get('execute/my/command', function(){
exec("php path/to/your/project/artisan your-command",$resultLines);
Foreach($resultLines as $resultLine){
echo $resultLine;
}
});
First exec property is your command and second is variable to save the result.
I hope that help you

Select from table using command prompt (not browser) in Laravel framewok

I want to select all data from 1 table in command prompt (not from browser) using Laravel framework. Is there anyone know what the good references is? please give me link, thankyou
You can create an Artisan Command.
Check the Laravel 5.0 docs:
http://laravel.com/docs/5.0/commands
Then, you can run you command. Example:
php artisan mycommand:list
Laravel 4.2:
http://laravel.com/docs/4.2/commands
You can run artisan commands from "cmd" (Windows). (Edit your environment variables first! Add you PHP path).
Try:
php artisan tinker
Then:
DB::table('users')->get()

Categories