Storage Class

If you need to store something across requests.

Imagine that we have a controller that grabs some data from the database. Depending on the data it receives from the database, we want to pass that data to another controller by causing a redirect to the URL which is handled by the target controller. But the issue is that how can we persist data across requests. The answer to this question is using Sessions.

The Storage Class which can be found at sirJuni\Framework\Helper\Storage is used to store items which are needed across requests and between controllers. It uses Sessions to store data persistently. Its methods are all static so we don't need to create an instance of this class.

An example of its usage would be

<?php
require_once __DIR__ . "\\..\\vendor\\autoload.php";
use sirJuni\Framework\Middleware\Auth;
use sirJuni\Framework\Helper\Storage;

class UserController {

    public function show($request) {
        $store = Storage::dump();
        VIEW::init('profile.html', $store);
    }
}

class AuthController {
        // login
        public function login($request) {
            if ($request->formData('login')) {
                $email = $request->formData('username');
                $pass = $request->formData('password');
    
                $db = new DB();
    
                $user = $db->getUser($email);
                if ($user['password'] == $pass) {
                    Auth::login($user);  // login the user by creating session and storing data there
                    Storage::init();     // initiate the key store
                    Storage::addBucket($user);
                    HelperFuncs::redirect("/mypro/profile");
                }
                else {
                    HelperFuncs::redirect("/mypro/auth?message=wrong");
                }
            }
            else {
                HelperFuncs::redirect("/mypro");
            }
        }
        
        // logout
        public function logout($request) {
            Auth::logout();
            Storage::release();
            HelperFuncs::redirect("/mypro/auth");
        }
}
?>

In the above code example, the login method of AuthController logs the user in and then stores user details in the Storage. The redirect causes the another request but its handled by a different controller, in this case UserController. The show method in UserController dumps the Storage and creates Context out of this data and then passes it to VIEW::init.

Interface

  1. init()

This method creates an array inside of the $_SESSION array. This array is used to store all the keys that are added to this Storage. This makes sure we only access what we add to the Storage rather than having access to entire $_SESSION array.

Always make sure you call it before adding data to the Storage class if it has not been called already. It is better to just call Storage::init() in the index.php file.

Storage::init();
Storage::addBucket($arr);
  1. addData($key, $value)

This method is used to add a single key,value pair to the Storage.

Storage::addData('hello', 'hi');
  1. addBucket($arr)

This method adds an entire associative array to the Storage.

$nameage = ['shoaib'=>30,'hacker'=>40];
Storage::addBucket($nameage);
  1. getData($key)

This method returns the stored data corresponding to the provided $key.

Storage::getData('shoaib');     // returns 30
  1. dump()

This method returns an associative array contains all the data that was stored in the Storage.

Storage::dump();
  1. release()

This method is used to unset all the variables or associative entries in the Storage. Once we are done using the data, we should release it using this method.

It should be called when data is no longer needed. In above case, it should be called in the handler that logs the users out.

Storage::release();

Last updated