未定义类型 'Symfony\Component\Process\Process

问题描述 投票:0回答:1

我无法定义课程,所以必须为此做什么?

我下载

symfony/process
后,发生了:

Symfony\Component\Process\Process::__construct(): 参数 #1 ($command) 必须是数组类型,给定的字符串,在 C:\xampp\htdocs\BAT pp\Http\Controllers\Admin\ChartController 中调用.php 第 18 行

<?php

namespace App\Http\Controllers\Admin;

use App\Models\Order;
use App\Models\OrderItem;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process ;

class ChartController extends Controller
{

    public function PythonScript()
    {
        $process = new Process("C:\Users\ismail\Downloads\Untitled4.py");
        $process->run();

        if (!$process->isSuccessful()) {
            throw new ProcessFailedException($process);
        }

        $data = $process->getOutput();

        dd($data);
    }
}
laravel symfony process undefined
1个回答
1
投票

错误在告诉你答案

Symfony\Component\Process\Process::__construct():
Argument #1 ($command) must be of type array, string given,
called in C:\xampp\htdocs\BAT\app\Http\Controllers\Admin\ChartController.php on line 18

你在

C:\xampp\htdocs\BAT\app\Http\Controllers\Admin\ChartController.php
的第18行调用的是这样的:

$process = new Process("C:\Users\ismail\Downloads\Untitled4.py")

这调用了

__construct()
的构造函数(
Symfony\Component\Process\Process
)。您只传递一个参数,错误告诉您第一个参数应该是
array
,但您传递的是
string

这是构造函数在原始类中的样子。

namespace Symfony\Component\Process;
...
class Process implements \IteratorAggregate
{
    ...

    /**
     * @param array          $command The command to run and its arguments listed as separate entries
     * @param string|null    $cwd     The working directory or null to use the working dir of the current PHP process
     * @param array|null     $env     The environment variables or null to use the same environment as the current PHP process
     * @param mixed          $input   The input as stream resource, scalar or \Traversable, or null for no input
     * @param int|float|null $timeout The timeout in seconds or null to disable
     *
     * @throws LogicException When proc_open is not installed
     */
    public function __construct(array $command, string $cwd = null, array $env = null, mixed $input = null, ?float $timeout = 60)
    {
        ...
    }
    ...
}

基本上你需要添加 python 可执行文件(或者你想要运行的任何东西

Untitled4.py

new Process(['path/to/python.exe', 'path/to/Untitled4.py']);
© www.soinside.com 2019 - 2024. All rights reserved.