Run artisan command in laravel 5 - php

I have controller like this
public function store(Request $request)
{
Artisan::call("php artisan infyom:scaffold {$request['name']} --fieldsFile=public/Product.json");
}
Show me error
There are no commands defined in the "php artisan infyom" namespace.
When I run this command in CMD it work correctly

You need to remove php artisan part and put parameters into an array to make it work:
public function store(Request $request)
{
Artisan::call("infyom:scaffold", ['name' => $request['name'], '--fieldsFile' => 'public/Product.json']);
}
https://laravel.com/docs/5.2/artisan#calling-commands-via-code

If you have simple job to do you can do it from route file. For example you want to clear cache. In terminal it would be php artisan cache:clear In route file that would be:
Route::get('clear_cache', function () {
\Artisan::call('cache:clear');
dd("Cache is cleared");
});
To run this command from browser just go to your's project route and to clear_cache. Example:
http://project_route/clear_cache

Apart from within another command, I am not really sure I can think of a good reason to do this. But if you really want to call a Laravel command from a controller (or model, etc.) then you can use Artisan::call()
Artisan::call('email:send', [
'user' => 1, '--queue' => 'default'
]);
One interesting feature that I wasn't aware of until I just Googled this to get the right syntax is Artisan::queue(), which will process the command in the background (by your queue workers):
Route::get('/foo', function () {
Artisan::queue('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
});
If you are calling a command from within another command you don't have to use the Artisan::call method - you can just do something like this:
public function handle()
{
$this->call('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
}
Source: https://webdevetc.com/programming-tricks/laravel/general-laravel/how-to-run-an-artisan-command-from-a-controller/

Remove php artisan part and try:
Route::get('/run', function () {
Artisan::call("migrate");
});

Command Job,
Path : {project-path}/app/Console/Commands/RangeDatePaymentsConsoleCommand.php
This is the Job that runs with the artisan command.
class RangeDatePaymentsConsoleCommand extends Command {
protected $signature = 'batch:abc {startDate} {endDate}';
...
}
web.php,
Path : {project-path}/routes/web.php
web.php manage all the requests and route to relevant Controller and can have multiple routes for multiple controllers and multiple functions within the same controller.
$router->group(['prefix' => 'command'], function () use ($router) {
Route::get('abc/start/{startDate}/end/{endDate}', 'CommandController#abc');
});
CommandController.php,
Path : {project-path}/app/Http/Controllers/CommandController.php
This Controller is created for handle artisan commands and name can be vary but should be same to web.php Controller name and the function name.
class CommandController extends Controller {
public function abc(string $startDate, string $endDate) {
$startDate = urldecode($startDate);
$endDate = urldecode($endDate);
$exitCode = Artisan::call('batch:abc',
[
'startDate' => $startDate,
'endDate' => $endDate
]
);
return 'Command Completed Successfully. ';
}
Request : http://127.0.0.1:8000/command/abc/start/2020-01-01 00:00:00/end/2020-06-30 23:59:59
It's can be access through the web browser or Postman after startup the server. Run this command to start the php server at {project-path}
php -S 127.0.0.1:8080 public/index.php

Method #1: Using route
Route::get('run-it', function () {
(new \App\Console\Commands\ThisIsMyCommand())->handle();
});
Method #2: Using command line
php artisan command:this_is_my_command

Related

Target class [DataTypesTableSeederCustom] does not exist

i'm using laravel6, and voyager, i'm creating artisan command for installation use cmd following php artisan make: command EcommerceInstall
, I recopy DataTypesTableSeeder from database/seeder and I rename it by DataTypesTableSeederCustom, I modify my file which I wrote EcommerceInstall.php but it gives me error class [DataTypesTableSeederCustom] does not exist.
i think i forget import the class but i don't know where .
EcommerceInstall.php
public function handle()
{
if ($this->confirm('this well delete all you current data and install the dummy default data, Are you sure ?')) {
File::deleteDirectory(public_path('storage/products/dummy'));
$this->callSilent('storage:link');
$copySuccess = File::copyDirectory(public_path('img/products'),public_path('storage/products/dummy'));
if($copySuccess){
$this->info('images succefully copied to storage folder');
}
$this->call('migrate:fresh', [
'--seed' => true,
]);
$this->call('db:seed', [
'--class' => 'DataTypesTableSeederCustom'
]);
$this->info('Dummy data installed');
}

Running custom artisan command in laravel controller

I have custom command like:
php artisan down --message="this is my custom message."
Now I want to use this command in my controller with input fields.
I know I can use Call method like Artisan::call('down'); but my issue is how to add --message="" part into that call method?
Data
this is what I'm sending to controller currently:
array:3 [▼
"_token" => "wqHyTNmDhArtonB0gwhIbCipSsStv0WnoASQm34u"
"maintenance_message" => "this is my custom message."
"maintenance" => "active"
]
Now based on maintenance value i will call Artisan::call('up'); or Artisan::call('down'); but the question is how do i add maintenance_message into it?
Code
This is my current function.
public function MaintenanceMode(Request $request){
if($request->input('maintenance') == 'active'){
//maintenance_message
Session::flash('danger', 'Site is successfully in maintenance mode.');
return Artisan::call('down');
}else{
//maintenance_message
Session::flash('success', 'Site is ONLINE.');
return Artisan::call('up');
}
}
Any idea?
The documentation has some examples of this:
https://laravel.com/docs/5.7/artisan#programmatically-executing-commands
The call method accepts either the command's name or class as the first argument, and an array of command parameters as the second argument. The exit code will be returned:
Route::get('/foo', function () {
$exitCode = Artisan::call('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
});
Solved
Here is what I did to add my message part into artisan command
return Artisan::call('down', ['--message' => $request->input('maintenance_message')]);
Hope it help others.

Call a Controller method from a Command in Laravel

I have a Command that is listening via Redis Pub/Sub. When a Publish is received, I want to call a controller method so that I can update the database.
However, I have not been able to find any solution on how to call a controller method with parameters from inside of the project but outside of the routes. The closest thing I have seen is something like:
return redirect()->action(
'TransactionController#assignUser', [
'transId' => $trans_id,
'userId' => $user_id
]);
My complete command that I've tried looks like this:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
class RedisSubscribe extends Command
{
protected $signature = 'redis:subscribe';
protected $description = 'Subscribe to a Redis channel';
public function handle()
{
Redis::subscribe('accepted-requests', function ($request) {
$trans_array = json_decode($request);
$trans_id = $trans_array->trans_id;
$user_id = $trans_array->user_id;
$this->assignUser($trans_id, $user_id);
});
}
public function assignUser($trans_id, $user_id)
{
return redirect()->action(
'TransactionController#assignUser', [
'transId' => $trans_id,
'userId' => $user_id
]);
}
}
However, this does not seem to work. When I run this Command, I get an error that assignUser() cannot be found (even though it exists and is expecting two paramters). I am also not sure a "redirect" is really what I am after here.
Is there some other way to call a controller function in a Command, or some other way that would make this possible to do?
If your controller does not have any required parameters, you can just create the controller as a new object, and call the function.
$controller = new TransactionController();
$controller->assignUser([
'transId' => $trans_id,
'userId' => $user_id
]);

Dingo/API when Unit Testing: The version given was unknown or has no registered routes

I built an API using dingo/api 0.10.0, Laravel 5.1 and lucadegasperi/oauth2-server-laravel": "^5.1".
All my routes work fine in Postman/Paw!
The problem appears when I try to test the API using PHPUnit.
This is part of my route-api.php file
<?php
$api = app('Dingo\Api\Routing\Router');
$api->version(['v1'], function ($api) {
$api->post('oauth/access_token', function () {
return response(
\LucaDegasperi\OAuth2Server\Facades\Authorizer::issueAccessToken()
)->header('Content-Type', 'application/json');
});
$api->group(['middleware' => ['oauth', 'api.auth']], function ($api) {
$api->post('/register', 'YPS\Http\Controllers\Api\UserController#register');
});
And this is my test file UserRegistrationTest.php
class UserRegistrationTest extends ApiTestCase
{
public function setUp()
{
parent::setUp();
parent::afterApplicationCreated();
}
public function testRegisterSuccess()
{
$data = factory(YPS\User::class)->make()->toArray();
$data['password'] = 'password123';
$this->post('api/register', $data, $this->headers)
->seeStatusCode(201)
->seeJson([
'email' => $data['email'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
]);
}
public function testRegisterMissingParams()
{
$this->post('api/register', [], $this->headers, $this->headers, $this->headers)->seeStatusCode(422);
}
}
The ApiTestCase simply retrieves a token and sets the headers.
private function setHeaders()
{
$this->headers = [
'Accept' => 'application/vnd.yps.v1+json',
'Authorization' => 'Bearer ' . $this->OAuthAccessToken,
];
}
Now, the weird part is that the first test testRegisterSuccess runs perfectly and returns the response I expect. But the second one testRegisterMissingParams, even though it's the same route, returns this,
array:2 [
"message" => "The version given was unknown or has no registered routes."
"status_code" => 400
]
I tracked the error and it is in the Laravel adapter here:
public function dispatch(Request $request, $version)
{
// it seems that the second time around can't find any routes with the key 'v1'
if (! isset($this->routes[$version])) {
throw new UnknownVersionException;
}
$routes = $this->mergeExistingRoutes($this->routes[$version]);
$this->router->setRoutes($routes);
return $this->router->dispatch($request);
}
And further more, if i run one test at a time (eg comment one out, run test and then comment the other and run test) i see the result expected in both tests. The problem is when i run multiple tests.
Any thoughts on that?
Thank you!
Run php artisan api:routes to see full path you may have missed something for the URL, also if this working if you request your URL manually?
I had same problem with testing using Dingo & Lumen. This worked for me - remove bootstrap="bootstrap/app.php" from phpunit.xml file and change line processIsolation="false" to processIsolation="true".

How do I write to the console from a Laravel Controller?

So I have a Laravel controller:
class YeahMyController extends BaseController {
public function getSomething() {
Console::info('mymessage'); // <-- what do I put here?
return 'yeahoutputthistotheresponse';
}
}
Currently, I'm running the application using artisan (which runs PHP's built-in development web server under the hood):
php artisan serve
I would like to log console messages to the STDOUT pipe for the artisan process.
Aha!
This can be done with the following PHP function:
error_log('Some message here.');
Found the answer here: Print something in PHP built-in web server
The question relates to serving via artisan and so Jrop's answer is ideal in that case. I.e, error_log logging to the apache log.
However, if your serving via a standard web server then simply use the Laravel specific logging functions:
\Log::info('This is some useful information.');
\Log::warning('Something could be going wrong.');
\Log::error('Something is really going wrong.');
Or with current version of Laravel, like this:
info('This is some useful information.');
This logs to Laravel's log file located at /laravel/storage/logs/laravel-<date>.log (laravel 5.0). Monitor the log - linux/osx: tail -f /laravel/storage/logs/laravel-<date>.log
Laravel 5.0 http://laravel.com/docs/5.0/errors
Laravel 4.2: http://laravel.com/docs/4.2/errors
I haven't tried this myself, but a quick dig through the library suggests you can do this:
$output = new Symfony\Component\Console\Output\ConsoleOutput();
$output->writeln("<info>my message</info>");
I couldn't find a shortcut for this, so you would probably want to create a facade to avoid duplication.
It's very simple.
You can call it from anywhere in APP.
$out = new \Symfony\Component\Console\Output\ConsoleOutput();
$out->writeln("Hello from Terminal");
In Laravel 6 there is a channel called 'stderr'. See config/logging.php:
'stderr' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'formatter' => env('LOG_STDERR_FORMATTER'),
'with' => [
'stream' => 'php://stderr',
],
],
In your controller:
use Illuminate\Support\Facades\Log;
Log::channel('stderr')->info('Something happened!');
For better explain Dave Morrissey's answer I have made these steps for wrap with Console Output class in a laravel facade.
1) Create a Facade in your prefer folder (in my case app\Facades):
class ConsoleOutput extends Facade {
protected static function getFacadeAccessor() {
return 'consoleOutput';
}
}
2) Register a new Service Provider in app\Providers as follow:
class ConsoleOutputServiceProvider extends ServiceProvider
{
public function register(){
App::bind('consoleOutput', function(){
return new \Symfony\Component\Console\Output\ConsoleOutput();
});
}
}
3) Add all this stuffs in config\app.php file, registering the provider and alias.
'providers' => [
//other providers
App\Providers\ConsoleOutputServiceProvider::class
],
'aliases' => [
//other aliases
'ConsoleOutput' => App\Facades\ConsoleOutput::class,
],
That's it, now in any place of your Laravel application, just call your method in this way:
ConsoleOutput::writeln('hello');
Hope this help you.
If you want the fancy command IO from Laravel (like styling, asking and table) then I created this class below
Instructions
I have not fully verified everywhere that it is THE cleanest solution etc, but it works nice (but I only tested it from within a unit test case, under Laravel 5.5).
So most probably you can use it however you like:
$cmd = new ConsoleCommand;
$cmd->error("Aw snap!");
$cmd->table($headers, $rows);
$answer = $cmd->ask("Tell me, what do you need?");
//even Symfony's progress bar
$cmd->outputStyle->progressStart(5); //set n = 100% (here 100% is 5 steps)
$cmd->outputStyle->progressAdvance(); //you can call it n times
$cmd->outputStyle->progressFinish(); //set to 100%
Or course you can also wrap in your own facade, or some static singleton etc, or anyway you wish.
The class itself
class ConsoleCommand extends \Illuminate\Console\Command
{
protected $name = 'NONEXISTENT';
protected $hidden = true;
public $outputSymfony;
public $outputStyle;
public function __construct($argInput = null)
{
parent::__construct();
$this->input = new \Symfony\Component\Console\Input\StringInput($argInput);
$this->outputSymfony = new \Symfony\Component\Console\Output\ConsoleOutput();
$this->outputStyle = new \Illuminate\Console\OutputStyle($this->input, $this->outputSymfony);
$this->output = $this->outputStyle;
}
}
I wanted my logging information to be sent to stdout because it's easy to tell Amazon's Container service (ECS) to collect stdout and send it to CloudWatch Logs. So to get this working, I added a new stdout entry to my config/logging.php file like so:
'stdout' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'with' => [
'stream' => 'php://stdout',
],
'level' => 'info',
],
Then I simply added 'stdout' as one of the channels in the stack log channel:
'default' => env('LOG_CHANNEL', 'stack'),
'stack' => [
'driver' => 'stack',
'channels' => ['stdout', 'daily'],
],
This way, I still get logs in a file for local development (or even on the instance if you can access it), but more importantly they get sent to the stdout which is saved in CloudWatch Logs.
If you want to log to STDOUT you can use any of the ways Laravel provides; for example (from wired00's answer):
Log::info('This is some useful information.');
The STDOUT magic can be done with the following (you are setting the file where info messages go):
Log::useFiles('php://stdout', 'info');
Word of caution: this is strictly for debugging. Do no use anything in production you don't fully understand.
Bit late to this...I'm surprised that no one mentioned Symfony's VarDumper component that Laravel includes, in part, for its dd() (and lesser-known, dump()) utility functions.
$dumpMe = new App\User([ 'name' => 'Cy Rossignol' ]);
(new Symfony\Component\VarDumper\Dumper\CliDumper())->dump(
(new Symfony\Component\VarDumper\Cloner\VarCloner())->cloneVar($dumpMe)
);
There's a bit more code needed, but, in return, we get nice formatted, readable output in the console—especially useful for debugging complex objects or arrays:
App\User {#17
#attributes: array:1 [
"name" => "Cy Rossignol"
]
#fillable: array:3 [
0 => "name"
1 => "email"
2 => "password"
]
#guarded: array:1 [
0 => "*"
]
#primaryKey: "id"
#casts: []
#dates: []
#relations: []
... etc ...
}
To take this a step further, we can even colorize the output! Add this helper function to the project to save some typing:
function toConsole($var)
{
$dumper = new Symfony\Component\VarDumper\Dumper\CliDumper();
$dumper->setColors(true);
$dumper->dump((new Symfony\Component\VarDumper\Cloner\VarCloner())->cloneVar($var));
}
If we're running the app behind a full webserver (like Apache or Nginx—not artisan serve), we can modify this function slightly to send the dumper's prettified output to the log (typically storage/logs/laravel.log):
function toLog($var)
{
$lines = [ 'Dump:' ];
$dumper = new Symfony\Component\VarDumper\Dumper\CliDumper();
$dumper->setColors(true);
$dumper->setOutput(function ($line) use (&$lines) {
$lines[] = $line;
});
$dumper->dump((new Symfony\Component\VarDumper\Cloner\VarCloner())->cloneVar($var));
Log::debug(implode(PHP_EOL, $lines));
}
...and, of course, watch the log using:
$ tail -f storage/logs/laravel.log
PHP's error_log() works fine for quick, one-off inspection of simple values, but the functions shown above take the hard work out of debugging some of Laravel's more complicated classes.
Here's another way to go about it:
$stdout = fopen('php://stdout', 'w');
fwrite($stdout, 'Hello, World!' . PHP_EOL);
The PHP_EOL adds new line.
In command class
before class
use Symfony\Component\Console\Output\ConsoleOutput;
Inside the class methods
$output = new ConsoleOutput();
$output->writeln('my text that appears in command line ');
You can use echo and prefix "\033", simple:
Artisan::command('mycommand', function () {
echo "\033======== Start ========\n";
});
And change color text:
if (App::environment() === 'production') {
echo "\033[0;33m======== WARNING ========\033[0m\n";
}
From Larave 6.0+
$this->info('This will appear in console');
$this->error('This error will appear in console');
$this->line('This line will appear in console);
Documentation https://laravel.com/docs/6.x/artisan#writing-output

Categories