带有上传文件的脚本的呼叫路由

时间:2018-10-16 17:11:03

标签: laravel

我正在制作一个命令行脚本来调用项目中的路由,以执行一些管理任务。它可以与文本参数配合使用:

    $request = Request::create('/api/v1/boards', 'POST',
        ['board_title' => 'asdfasdf' . md5(rand(1,9999999)),
        'board_description' => 'asldfjaklsdfj']);
    $response = app()->handle($request);
    $responseBody = $response->getContent();
    $responseData = json_decode($response->getContent());
    $board_id = $responseData->board_id;

我想发送文件上传,但没有成功。到目前为止,这是我的代码:

    $image_filename = './scrapes/image_700x1000.jpg';
    $file = new UploadedFile($image_filename, 'imgUpload', filesize($image_filename), 'image/jpeg', null, false);
    $request = Request::create('/api/v1/boards/' . $board_id, 'PUT',
        [ $file]);
    $response = app()->handle($request);
    $responseBody = $response->getContent();
    var_dump($responseBody);

结果是控制器响应未找到图像。通过JQuery将图像发送到它时,它可以正常工作。这是控制器的部分代码:

public function update(Request $request, Board $board)
{
    //Check for valid user
    if (!$request->user())
    {
        return response()->json(['message' => 'Not authorized'], 401);
    }

    //Check if user is permitted to update board
    //Could use Gates as alternative
    if (!$request->user()->is_admin && $request->user()->id !== $board->board_owner_id)
    {
        return response()->json(['message' => 'Not authorized'], 403);
    }

我希望能帮助您确定如何发送图像。我意识到这是不寻常的,但这可能是管理脚本的本质。

0 个答案:

没有答案