Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

callback after response for custom Controller #1050

Open
clysss opened this issue Jan 15, 2025 · 1 comment
Open

callback after response for custom Controller #1050

clysss opened this issue Jan 15, 2025 · 1 comment

Comments

@clysss
Copy link
Contributor

clysss commented Jan 15, 2025

hello,
I've severall custom controllers ; each function/route create the Response return by api->handle and send as Response to the call.
I'd like to make post operations (after the response has been send) that can be time consuming like reindexing DB or making backup (few seconds) and I don't want to block the response..
Of course, I could add this function to the last line of the my api.php
`<?php
namespace Tqdev\PhpCrudApi;
use Tqdev\PhpCrudApi\Api;
use Tqdev\PhpCrudApi\Config\Config;
use Tqdev\PhpCrudApi\RequestFactory;
use Tqdev\PhpCrudApi\ResponseUtils;

require 'api.include.php';
require 'eCtrl.php';
require_once 'eConf.php';

$config = new Config([
'driver' => CONF_driver,
'address' => CONF_address,
'port' => CONF_port,
'database' => CONF_database,
'username' => CONF_username,
'password' => CONF_password,
'cachePath' => CONF_tempDir,
'cacheType'=>'TempFile',
'controllers'=>'cache', //"records,status', // for /status/ping
//'middlewares'=>'', // cors disabled xsrf don't work with cors disabled ?
'middlewares'=>'cors', // for dev only
'customControllers' => 'eCtrl',
]);
$request = RequestFactory::fromGlobals();
$api = new Api($config);
$response = $api->handle($request);
ResponseUtils::output($response);

file_put_contents('request.log',RequestUtils::toString($request)."===\n",FILE_APPEND);
file_put_contents('request.log',ResponseUtils::toString($response)."===\n",FILE_APPEND);
`
but in this case, how to call in this case a function of eCtrl class (not a static one?)

@clysss
Copy link
Contributor Author

clysss commented Jan 16, 2025

After different tries, and as I can't use pcntl_fork() on my shared hosting, I directly called
Tqdev\PhpCrudApi\ResponseUtils::output($this->responder->success(['results' => ":):)OK"]));
in my custom controler function
for example :
public function me(ServerRequestInterface $request): ResponseInterface
{
Tqdev\PhpCrudApi\ResponseUtils::output($this->responder->success(['results' => ":):)OK"]));
fastcgi_finish_request();
return $this->responder->success(['results' => 'OK']);
}

I still let the return to do (and so the second output in the main) but as the socket is close with fastcgi_finish_request(); I don't have any issue

@mevdschee : what do you think about it ?
I tried to add a test in the main to do only the output($response) if $response-> correctly defined but it seems to be a stream and not so easy to test..
Perhaps could we add a method in responder to disable the send of respond ?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant