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

clat23's avatar

Creating dynamic notification details

My app is interacting with an external API that will sometimes return error codes. I want my app to be able to handle the error code and send a notification to the user when an error is encountered. I am storing the different error codes in a database so that when new error codes are created by the external API, I don't have to update my code to handle new errors; instead I add the new error in my error codes table.

public function notify($error, $sale)
{
    $this->notifications->create($user, [
        'icon' => $error->icon,
        'body' => $error->body,
        'action_text' => $error->text,
        'action_url' => $error->url,
    ]);
}

The action_url might contain something like this:

/sale/

So if I want the action_url to direct the user to see the sale I would do something like this 'action_url' => $error->url . $sale,

But what if the sale ID is not always at the end, such as when I want to direct the user to edit the sale:

/sale/1/edit

My initial thought is to store the URL in the database as something like this:

/sale/{sale_id}/edit

..and replace {sale_id}:

if (strpos($error->url, '{sale_id}') != FALSE) {
            $url = str_replace('{sale_id}', $sale_id->id, $error->url);
        } else {
            $url = $error->url;
        }

public function notify($error, $sale)
{
    $this->notifications->create($user, [
        'icon' => $error->icon,
        'body' => $error->body,
        'action_text' => $error->text,
        'action_url' => $url,
    ]);
}

But then what if I want to refer to any other $sale details in the other portions of the notification. Such as the notification body containing a more dynamic body:

The sale titled "Foo Bar" cannot be processed.

I'd want to store this body in the database like this:

The sale titled "$sale->title" cannot be processed.

Is there any way to accomplish this?

0 likes
0 replies

Please or to participate in this conversation.