1

When i delete or create data from database I save messages in session like this

/**
 * Function that saves the response to the session
 * @param Bool $bResult
 * @param      $sAction
 */
private function storeResultToSession($bResult, $sAction)
{
    Session::forget([
        constResponse::SESSION_KEY_RESULT,
        constResponse::SESSION_KEY_MESSAGE
    ]);

    $sMessage = '';

    switch ($sAction) {
        case constCommon::ACTION_CREATE:
            $sAction = 'create_slide';
            $sMessage = $bResult === constCommon::BOOL_TRUE ? constResponse::SUCCESS_CREATE_SLIDE : constResponse::ERR_CREATING_SLIDE;
            break;
        case constCommon::ACTION_DELETE:
            $sAction = 'delete_slide';
            $sMessage = $bResult === constCommon::BOOL_TRUE ? constResponse::SUCCESS_DELETE_SLIDE : constResponse::ERR_DELETE_SLIDE;
            break;
        default:
            break;
    }

    Session::put([
        constResponse::SESSION_KEY_RESULT  => $bResult,
        constResponse::SESSION_KEY_MESSAGE => $sMessage,
        constResponse::SESSION_KEY_ACTION  => $sAction,
    ]);
}

on success of the action, i use window.location.href in the frontend for redirecting to home page where i would toast the result of the action. The Message would then be deleted on page reuquest with this method

public function getMessageToToast()
{
    if (session()->has(constResponse::SESSION_KEY_RESULT) === false && session()->get(constResponse::SESSION_KEY_RESULT) === null) {
        return null;
    }

    $bResult = session()->get(constResponse::SESSION_KEY_RESULT);
    $sMsg = session()->get(constResponse::SESSION_KEY_MESSAGE);
    $sAction = session()->get(constResponse::SESSION_KEY_ACTION);

    Session::forget([
        constResponse::SESSION_KEY_RESULT,
        constResponse::SESSION_KEY_MESSAGE,
        constResponse::SESSION_KEY_ACTION
    ]);
    Session::put([
        constResponse::SESSION_KEY_RESULT  => null
    ]);
    Session::save();
    return [
        constResponse::SESSION_KEY_RESULT  => $bResult,
        constResponse::SESSION_KEY_MESSAGE => $sMsg,
        constResponse::SESSION_KEY_ACTION  => $sAction
    ];
}

but Session is acting very wierd, it onlu forgets the items after 2-3 page reload or redirect.

0 Answers0