CakePHP implementation issue
wilsmex opened this issue · comments
Following along with the sample CakePHP implementation (I'm using latest CakePHP 4), but I get the following error when hitting my /tus endpoint
'Controller actions can only return ResponseInterface instance or null. Got Symfony\Component\HttpFoundation\Response instead.'
Any ideas?
Hi @wilsmex, the documentation is possibly outdated however it gives you the idea about the integration.
I haven't worked with CakePHP for a long time but looking at the error it looks like CakePHP controllers now requires PSR-7 compatible response object. But this package returns symfony HTTPFoundation Response.
So something like this should do the trick for now (untested).
<?php
// composer require symfony/psr-http-message-bridge
namespace App\Controller;
use Cake\Controller\Controller;
use TusPhp\Tus\Server as TusServer;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
class TusController extends Controller
{
/**
* Create tus server.
*
* @return Psr\Http\Message\ResponseInterface
*/
public function server()
{
$server = new TusServer('redis');
$server
->setApiPath('/tus')// tus server endpoint
->setUploadDir(WWW_ROOT . 'uploads'); // upload dir
$symfonyResponse = $server->serve();
$psr17Factory = new Psr17Factory();
$psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
$psrResponse = $psrHttpFactory->createResponse($symfonyResponse);
return $psrResponse;
}
}
This issue has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs. Thank you
for your contributions.
Closing because of inactivity.