folded / history
Manipulate the browser history in your web app.
Requires
- php: >=7.4.0
- folded/exception: 0.4.*
Requires (Dev)
- friendsofphp/php-cs-fixer: 2.*
- pestphp/pest: 0.3.*
- phpunit/phpunit: 9.*
This package is auto-updated.
Last update: 2024-10-17 17:05:31 UTC
README
Manipulate the browser history in your web app.
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.
- folded/action: A way to organize your controllers for your web app.
- folded/config: Configuration utilities for your PHP web app.
- folded/crypt: Encrypt and decrypt strings for your web app.
- folded/exception: Various kind of exception to throw for your web app.
- folded/http: HTTP utilities for your web app.
- folded/orm: An ORM for you web app.
- folded/routing: Routing functions for your PHP web app.
- folded/request: Request utilities, including a request validator, for your PHP web app.
- folded/session: Session functions for your web app.
- folded/view: View utilities for your PHP web app.
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
- 2. Get the last history
- 3. Get all the URLs in the history
- 4. Check if an index in the history is present
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"; }