![]() |
Hi,
I have created a filter to trigger a process After a controller. My controller (below) is very simple. Essentially, the controller receives data from an external source (webhook), assigns a variable to the data, and returns an HTTP 200 (Success) to the external server. PHP Code: namespace App\Controllers; After the above controller, the filter is executing perfectly, and running my process (below). My process (below) obviously fails as $bar does not exist, PHP Code: use CodeIgniter\Model; In this instance, Codeigniter is returning an HTTP 500 response to the external server, instead of the HTTP 200 as defined in the controller. I do not understand how this is possible as I should have already returned HTTP 200 from the controller, and my process (Updater) should only be called AFTER the controller has finished, is this correct? If this is not correct, is there a way to respond with HTTP 200 from the controller (this would be correct as I have received the data), and then process the data, handling any subsequent errors (data or framework) in alternative ways? Many Thanks,
If C.I is returniing 500 to the external site user then there is an exception error from your end. You can check your error log to find out what the problem is.
I don't understand what the Updater class is.
It is not called from the controller, so it seems to be never called. And $bar is obviously not defined, so no wonder that it causes an error.
09-11-2024, 06:08 AM
(This post was last modified: 09-11-2024, 06:09 AM by 68thorby68. Edit Reason: typo ) (08-26-2024, 03:46 PM)kenjis Wrote: I don't understand what the Updater class is. Hi Kenjis, Many thanks for your continued help, perhaps a bit of context will add some clarity. When I receive a payload from an external server I have a very short time to respond with either HTTP 200 (payload received) or HTTP 500 (error receiving payload). However, I have some pretty heavy lifting to do once I have received the payload. Therefore, I was hoping that by defining a filter, I could run some processes after the controller receiving the payload had determind the payload was received or was NOT received. Assuming payload is received OK and the controller has finished I have defined a filter that I hoped would: 1) immediately send an HTTP 200 response to the external server (I have received the payload OK that is all the external server needs to know). then, 2) pass the payload to my Updater class that will complete the necessary heavy lifting. To test this, I intentionally created an error ($foo=$bar) in the Updater class so the Updater class fails. As a result of the Updater failure, I was expecting, 1) the HTTP 200 response would be sent to the external server (I have received the payload and all is OK), then 2) the Updater class would run and report any subsequent errors in normal manner. But this is not the case. If Updater class fails (as in my test), a HTTP 500 reponse is sent the exteral server, not the HTTP 200 response, as defined in the filter. I hope this clarify things? Many thanks.
You misunderstand how php and http work. After returning 200, the script continues to work. The most recent stage is the Updater call. You need a Queue and Tasks
https://codeigniter.com/user_guide/libra...tasks-beta or https://www.rabbitmq.com/
You seem to misunderstand how PHP or the framework works.
> As a result of the Updater failure, I was expecting, 1) the HTTP 200 response would be sent to the external server (I have received the payload and all is OK), then 2) the Updater class would run and report any subsequent errors in normal manner. I don't know why you think like that. The Updater failure just causes 500 response. If you want to return 200 response, you must return 200 response without any error. If you want to execute the Updater class after returning 200 response, you need to use a Queue system like https://queue.codeigniter.com/
(09-11-2024, 06:25 PM)kenjis Wrote: You seem to misunderstand how PHP or the framework works. Thank you kenjis and ozornick, My sincere apologies, I completely understand how PHP and HTTP work, however, I was convinced I read somewhere in the documantation that an After filter would run after a controller has executed and the page had been rendered. Thus, my confusion as to why the HTTP response was not returning as expected. Obviously this is not the case !!! I will take a look at implmenting queues. Thank you. |
Welcome Guest, Not a member yet? Register Sign In |