folded/history

Manipulate the browser history in your web app.

v0.1.3 2020-10-03 12:49 UTC

This package is auto-updated.

Last update: 2024-11-17 17:22:49 UTC


README

Manipulate the browser history in your web app.

Packagist License Packagist PHP Version Support Packagist Version Build Status Maintainability TODOs

Summary

About

I created this library to be able to easily come back to the browsed page after editing a resource. The possibility are endless with this package, which is easy to integrate as it is loosely coupled.

Folded is a constellation of packages to help you setting up a web app easily, using ready to plug in packages.

Features

  • Stores the GET requests, by making sure there is not duplicate if the user browse again the same URL
  • Can check if an history index exist
  • Can get an URL by its history index, or get all the urls in the history
  • Uses the PHP session to store the history

Requirements

  • PHP version >= 7.4.0
  • Composer installed

Installation

1. Install the package

In your root folder, run this command:

composer required folded/history

2. Add the bootstrap code

As early as possible, and by making sure the code is ran before your "controller" or "action" code, add this:

use function Folded\addRequestedUrlToHistory;

session_start();

addRequestedUrlToHistory();

Every time your user browse an URL, it will get added if it matches a GET request.

Examples

1. Get an history by its index

In this example, we will get the second last URL by its index in the history.

use function Folded\getHistory;

echo getHistory(-1);

This history starts from 0, and to get the earlier URL, you need to use negative indexes.

  • An history of 0 will get the last URL
  • An history of 1 will get the second last URL
  • An history of 2 will get the URL before the second last
  • ...

2. Get the last history

In this example, we will not use any argument to get the last URL in the history.

use function Folded\getHistory;

echo getHistory();

3. Get all the URLs in the history

In this example, we will get all the history as an array.

use function Folded\getAllHistory;

$urls = getAllHistory();

foreach ($urls as $url) {
  echo $url;
}

4. Check if an index in the history is present

In this example, we will check if an url in the history at a specific index exists.

use function Folded\hasHistory;

if (hasHistory(-1)) {
  echo "has history -1";
} else {
  echo "has not history -1";
}

Version support