Is it possible to call Shell commands (e.g for converting images ) from Laravel controller? If yes then how. I have searched on internet. Nothing seems relevant.
You can use the Process component provided by Symfony: http://symfony.com/doc/current/components/process.html
The component is used by Laravel itself, so your don't need to install it via composer separately. Just add use Symfony\Component\Process\Process; to your file.
If you use it(instead of php's exec() function), you'll be able to unit test the code that calls shell commands.
It all depends on what operating system you are using. php already has a few functions to execute shell commands.
Laravel has a build in task runner for ssh commands.
$FilenamePending = "files".csv";
if (File::exists(public_path('downloads/files/wstock/'.$FilenamePending))){
$PathFilesWip= public_path('downloads/files/wstock/'.$FilenamePending);
//$getCommand= "aws s3 cp $PathFiles s3://bucket-name/share/in/test/Transaction/";
$getCommandPending= "aws s3 cp $PathFilesWip s3://store_stocks/";
}else{
$getCommandPending= "";
}
$schedule->exec($getCommandPending) ->timezone('Asia/Kolkata')->dailyAt('00:38')->appendOutputTo(storage_path().'/logs/laravel_output.log');
Related
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.
Laravel has the following built-in npm run commands (among others):
npm run install
npm run watch
Is it possible to create custom npm run commands to run custom PHP scripts? For example, I want to create a command called npm run csv that will run a PHP script that imports a bunch of CSV data into a database.
Thanks.
Edit: After asking the question and seeing a lot of the responses, it has become overwhelming obvious that writing a php artisan command is probably the better way to go. As such, that's what I will do.
Thank you all for your responses. As for why I didn't ask that question, it's quite simple: I didn't know that that was a better approach. I'm still new to Laravel and learning. Thanks.
At first, you must write Artisan Console Command. Then you could run it using npm. But this is not a recommended way. you can run any artisan command like:
php artisan inspire
If you want to run this with npm just add this command in the package.json's script. For example:
{
"scripts": {
"inspire": "php artisan inspire"
}
}
Then run the command like this:
npm run inspire
It should be cleaner to create your own artisan command.
See https://laravel.com/docs/5.7/artisan#writing-commands
Then put your csv import code inside the handle() method.
You will just have to run : php artisan import-csv or something like
I love this side of Laravel. From what you are trying to achieve, may I advise on custom artisan commands? :)
https://laravel.com/docs/5.7/artisan
Recommend the reading, it is great for what you are looking :D
There's no need to use NPM to call PHP! Why not just create your own executable?
Using Laravel, there is a command system, so you can make your own artisan commands. But if you want something simpler, you can do this:
Example. I create a file in my project called bin/do_stuff
#!/usr/bin/env php
<?php
echo "Easy as that!\n";
Then make it executable:
chmod +x bin/do_stuff
Then you can run it with ./bin/do_stuff! Not hard at all! Now you can also pass arguments like so:
./bin/do_stuff--option1=value1 --option2
With or without values. To do this, we add the following:
foreach ($argv as $arg)
{
preg_match('/\-\-(\w*)\=?(.+)?/', $arg, $value);
if ($value && isset($value[1]) && $value[1])
{
$options[$value[1]] = isset($value[2]) ? $value[2] : null;
}
}
Great for in cron jobs and back end stuff. Give it a try!
I am trying to pass parameters to command line and get the values using getopt() function. It works fine when I use
php file.php -a arg
But it does not show any value when I use
yiic shell file.php -a arg
I think I am using the incorrect syntax but I didn't get any proper result when I googled for the correct answer.
Any help will be appreciated.
Thanks in advance!
You should use the name of your command instead of the file name. For example, if you have a MyCommandNameCommand class that extends from CConsoleCommand you have to execute the following command from the command line:
yiic shell mycommandname arg1 arg2 ...
Hope it helps.
Well you just have to create a Yii command.
Check this out:
http://www.yiiframework.com/doc/guide/1.1/en/topics.console
In order to create your own command with custom parameters, it should be quite easy, following this SitemapCommand example:
http://www.yiiframework.com/doc/guide/1.1/en/topics.console#console-command-action
Why are you trying to use parameters in yiic shell command? This is a special command that as far as I can see it takes only one parameter. You can see the core file at framework\cli\commands\ShellCommand.php, but again I do not understand why are you messing with this command.
>> yiic help shell
USAGE
yiic shell [entry-script | config-file]
DESCRIPTION
This command allows you to interact with a Web application
on the command line. It also provides tools to automatically
generate new controllers, views and data models.
It is recommended that you execute this command under
the directory that contains the entry script file of
the Web application.
PARAMETERS
* entry-script | config-file: optional, the path to
the entry script file or the configuration file for
the Web application. If not given, it is assumed to be
the 'index.php' file under the current directory.
Original Post
Good evening folks. I have a laravel setup and I'm trying to have a cronjob execute a php function to a file within the laravel project directory.
I am getting class and name space errors when I try to do something like this:
<?php
require_once('../laravel/app/Http/Controllers/NotificationsController.php');
and then calling the processQueuedNotifications() function.
This of course gives me errors, what is the correct way to call my function within the laravel directory? I need to call this function as this function has all the correct namespaces and extended controllers necessary to execute the function properly.
Update 1:
Thanks to #michael, I've been made aware of a component in Laravel called commands.
So I ran this code:
php artisan make:console processQueuedNotifications
and it created some files in the console directory.
Currently exploring on what to do next.
After checking out the Events class which the kernel.php file makes use of, I noticed that this class provides an easy to use interface for me to create cron jobs on the fly. Am I correct in think so?
I notice there is not function to run a cron job every minute, is it safe to edit the Events class file without it being overwritten by future make:console commands, or laravel updates?
I saw this code in the kernel.php file:
$schedule->command('inspire')
->hourly();
So is this the place you wanted me to add my function? as I notice that the inspire function is something automatically created for me to understand what's going on?
So I would write,
$schedule->command('processQueuedNotifications')
->everyMinute();
//Providing it's safe to edit the Event's class or figure out a clean way of doing so without my code being deleted in the future on Laravel updates.
A very convenient way is to use laravels console component. You can create a new command by issuing
php artisan make:console
And find it thereafter in your app/console directory. Make sure to enable the command in the Kernel.php file once created.
Simply call your class or whatever you want to run via cron from inside the command. The console command itself is callable via cli just as you would run one of laravels php artisan ... commands. You can set this in the file created for you. For example, you can then call the file from everywhere you want with
/usr/bin/php /path/to/file/artisan my:command
You can set options and arguments if you need to.
Here's the documentation: http://laravel.com/docs/5.0/commands / http://symfony.com/doc/current/components/console/introduction.html
There's an array in kernel.php you need to register your class (include the namespace) in. After that it is callable via cli. For a start, have a look on arguments and options you can initialize in case you need to make different requests on your controller class. (The filename you have chosen for your console command, is an argument. You can make them required or optional for your own commands. )
Within your file, you can create them by simply creating an array in the appropriate method with these values:
[$name, $mode, $description, $defaultValue]
have a look at the docs or Jeffrey's laracasts, they are very good.
To only call your class from the console command, it's enough to name your command in the above section of the file and call you controller like
(new namespace\controller)->method();
What you can do in your code, after your update, 2 choices :
Dispatching directly the command from your code using the Bus facade
first import it using the
use Illuminate\Support\Facades\Bus;
then in your code
Bus::dispatchNow(new YourCommandClass);
(don't forget to import your command class)
Dispatch it for queue process using the same bus facade:
(still importing the same way)
Bus::dispatch(new YourCommandClass);
(Note that in that case, you'll need to have the following command run by your cron job :
php artisan queue:listen
it can handle several options such as the --tries=X where is is the number of tries etc
Generally speaking, you can get more info from commands typing php artisan my:command -h
I have a problem figuring out how make my command work, because I only have access via ftp.
The action is pretty simple:
public function actionRun($action = "default") {
$this->xml = simplexml_load_file('db.xml');
return $this->{$action}(); // executes the default() method
}
All I need is to somehow execute the php index.php mycommand run, but I'm lost as to how. This command should only be run once in the life of the whole app.
My question is, is it possible to run such a command? Maybe somehow invoke it through php?
Yii command line command are designed to run through Yii.
$ cd protected
$ ./yiic --help
$ ./yiic mycommand
If you only have ftp access, you may be out of luck, and will have to use some workaround, for example running a cron job, or creating a web page that invokes the last command in the code sample I provided.
echo exec('/my_yii_dir/protected/yiic mycommand');