Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MerryChristmas's avatar

I don't understand behavior of returning from a function

This will return file with a content. Everything OK.

// SomeController.php
public function index(){
        $stream = Storage::disk('public_folder')->readStream($this->filename);
        return response()->stream(
            function () use ($stream) {
                fpassthru($stream);
            },
            200,
            ['Content-Type' => 'application/xml']
        );
}

Code stays the same. But I will move it into another function. It will return empty file:

// SomeController.php
public function index(){
		$this->generateItBySomeOtherFunction();
}

public function generateItBySomeOtherFunction(){
        $stream = Storage::disk('public_folder')->readStream($this->filename);
        return response()->stream(
            function () use ($stream) {
                fpassthru($stream);
            },
            200,
            ['Content-Type' => 'application/xml']
        );
}
0 likes
1 reply
SilenceBringer's avatar
Level 55

@merrychristmas because your index method does not return the result

public function index(){
		return $this->generateItBySomeOtherFunction();
}

Please or to participate in this conversation.