Skip to content

Response could not be converted to string #219

@monoland

Description

@monoland

PHP Version

8.3.25

Swoole Extension Version

6.0.2

Database Driver & Version

No response

Description

I’m trying to build a package similar to Laravel Fortify, since Hypervel doesn’t have one yet. When I try to return a response class, an error occurs.

Object of class App\\Http\\Responses\\Sample could not be converted to string at method transferToResponse in class Hypervel\\Http\\CoreMiddleware

problem solve when I override that method to

protected function transferToResponse($response, ServerRequestInterface $request): ResponsePlusInterface
    {
        if ($response instanceof Renderable) {
            if ($response instanceof ViewInterface) {
                if ($this->container->get(ConfigInterface::class)->get('view.event.enable', false)) {
                    $this->container->get(EventDispatcherInterface::class)
                        ->dispatch(new ViewRendered($response));
                }
            }

            return $this->response()
                ->setHeader('Content-Type', $this->container->get(RenderInterface::class)->getContentType())
                ->setBody(new SwooleStream($response->render()));
        }

        // Additional code
        if ($response instanceof \Hypervel\Support\Contracts\Responsable) {
            $hypervelRequest = new \Hypervel\Http\Request($request);
            $psrResponse = $response->toResponse($hypervelRequest);
            return new ResponsePlusProxy($psrResponse);
        }

        if (is_string($response)) {
            return $this->response()->addHeader('content-type', 'text/plain')->setBody(new SwooleStream($response));
        }

        if ($response instanceof ResponseInterface) {
            return new ResponsePlusProxy($response);
        }

        if (is_array($response) || $response instanceof Arrayable) {
            return $this->response()
                ->addHeader('content-type', 'application/json')
                ->setBody(new SwooleStream(Json::encode($response)));
        }

        if ($response instanceof Jsonable) {
            return $this->response()
                ->addHeader('content-type', 'application/json')
                ->setBody(new SwooleStream((string) $response));
        }

        if ($this->response()->hasHeader('content-type')) {
            return $this->response()->setBody(new SwooleStream((string) $response));
        }

        return $this->response()
            ->addHeader('content-type', 'text/plain')
            ->setBody(new SwooleStream((string) $response));
    }

Steps To Reproduce

I create a response class

<?php

declare(strict_types=1);

namespace App\Http\Responses;

use Hypervel\Http\Request;
use Psr\Http\Message\ResponseInterface;
use Hypervel\Support\Contracts\Responsable;

class Sample implements Responsable
{
    public function toResponse(Request $request): ResponseInterface
    {
        return response()->json([
            'success' => true,
            'message' => 'Hello from Sample!',
        ]);
    }
}

in controller

/**
 * Display a listing of the resource.
 */
public function index()
{
    return app(Sample::class);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions