简体   繁体   中英

Run artisan command in laravel 5

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM