izniburak / redux
simple Redux implementation for PHP
Installs: 7
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/izniburak/redux
Requires
- php: >=7.1
 
This package is auto-updated.
Last update: 2025-10-11 00:38:08 UTC
README
  _____            _                         _            
 |  __ \          | |                       | |           
 | |__) | ___   __| | _   _ __  __    _ __  | |__   _ __  
 |  _  / / _ \ / _` || | | |\ \/ /   | '_ \ | '_ \ | '_ \ 
 | | \ \|  __/| (_| || |_| | >  <  _ | |_) || | | || |_) |
 |_|  \_\\___| \__,_| \__,_|/_/\_\(_)| .__/ |_| |_|| .__/ 
                                     | |           | |    
                                     |_|           |_|    
simple Redux implementation for PHP
Today (10 Feb 2020), I found a blog post on Reddit about "Redux in 30 lines of PHP". You can reach it via this link. I wondered it and read blog post. I liked it! Then, I wanted to create a PHP package about that.
Install
composer.json file:
{
    "require": {
        "izniburak/redux": "^1"
    }
}
after run the install command.
$ composer install
OR run the following command directly.
$ composer require izniburak/redux
Example Usage
<?php require __DIR__ . '/vendor/autoload.php'; use Buki\Redux\{Action, Reducer, Store}; // Define a Initial State $initialState = [ 'counter' => [ 'count' => 1, ], ]; // Define action constants const INCREMENT_ACTION = 'INCREMENT'; const DECREMENT_ACTION = 'DECREMENT'; const SUM_ACTION = 'SUM'; // Create an Action $actions = new Action([ 'increment' => function () { return [ 'type' => INCREMENT_ACTION, ]; }, 'decrement' => [ 'type' => DECREMENT_ACTION, ], 'sum' => function ($value) { return [ 'type' => SUM_ACTION, 'data' => $value, ]; }, ]); // Create a Reducer $reducer = new Reducer(function ($state, $action) { switch ($action['type']) { case INCREMENT_ACTION: return Action::updateState($state, [ 'counter' => ['count' => $state['counter']['count'] + 1], ]); case DECREMENT_ACTION: return Action::updateState($state, [ 'counter' => ['count' => $state['counter']['count'] - 1], ]); case SUM_ACTION: return Action::updateState($state, [ 'counter' => ['count' => $state['counter']['count'] + $action['data']], ]); default: return $state; } }); // Create a Redux Store $store = new Store($reducer, $initialState); // Add a listener $store->listen(function ($state) { print_r($state); }); // Dispatch actions $store->dispatch($actions->get('increment')); $store->dispatch($actions->get('increment')); $store->dispatch($actions->get('increment')); $store->dispatch($actions->get('decrement')); $store->dispatch($actions->get('sum')(5));
ToDo
- Write Test
 
Support
Licence
Contributing
- Fork it ( https://github.com/izniburak/redux.php/fork )
 - Create your feature branch (git checkout -b my-new-feature)
 - Commit your changes (git commit -am 'Add some feature')
 - Push to the branch (git push origin my-new-feature)
 - Create a new Pull Request
 
Contributors
- izniburak İzni Burak Demirtaş - creator, maintainer