PHPackages                             samsonasik/ci4-react - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. samsonasik/ci4-react

ActiveProject

samsonasik/ci4-react
====================

CodeIgniter4 starter app with react.js Integration

1.0.0(3mo ago)3831115MITPHPPHP ^8.2CI passing

Since Jul 2Pushed 3mo ago3 watchersCompare

[ Source](https://github.com/samsonasik/ci4-react)[ Packagist](https://packagist.org/packages/samsonasik/ci4-react)[ Docs](https://codeigniter.com)[ RSS](/packages/samsonasik-ci4-react/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (7)Versions (17)Used By (0)

Example Using react.js in CodeIgniter 4 application
===================================================

[](#example-using-reactjs-in-codeigniter-4-application)

[![ci build](https://github.com/samsonasik/ci4-react/workflows/ci%20build/badge.svg)](https://github.com/samsonasik/ci4-react/workflows/ci%20build/badge.svg)[![Mutation testing badge](https://camo.githubusercontent.com/485c06a22871c2ae1efebb9792d33f783c979fa468f481ac89b1ffb933055280/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d25324673616d736f6e6173696b2532466369342d72656163742532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/samsonasik/ci4-react/master)[![Code Coverage](https://camo.githubusercontent.com/68e816edefc30e5a2992dafc988f315c5aa4f8c092509aee0f708b9efa066796/68747470733a2f2f636f6465636f762e696f2f67682f73616d736f6e6173696b2f6369342d72656163742f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/samsonasik/ci4-react)[![Total Downloads](https://camo.githubusercontent.com/d519b3cc5948d9487899b322c4ec917958148c08f70270fe8da96d3bedffb7e8/68747470733a2f2f706f7365722e707567782e6f72672f73616d736f6e6173696b2f6369342d72656163742f646f776e6c6f616473)](//packagist.org/packages/samsonasik/ci4-react)

Introduction
------------

[](#introduction)

A CodeIgniter 4 Skeleton Application with react.js integration.

Features
--------

[](#features)

- SPA application with React Router DOM with cached pages after visited.
- Using server side template from CodeIgniter 4, eval'd with DOMPurify it first.
- Webpack support for [production](#production)

Setup
-----

[](#setup)

*1.* Run composer create-project command:

```
composer create-project samsonasik/ci4-react
```

*2.* Copy file `ci4-react/env` to `ci4-react/.env`:

```
cp ci4-react/env ci4-react/.env
```

*3.* Set environment and app configuration

Open `ci4-react/.env` and set `CI_ENVIRONMENT`, `app.baseURL`, `app.indexPage`:

```
# file ci4-react/.env
CI_ENVIRONMENT = development

app.baseURL    = 'http://localhost:8080'
app.indexPage  = ''
```

*4.* Run PHP Development server

```
# go to ci4-react directory
cd ci4-react

# run php development server inside ci4-react directory
php spark serve
```

*5.* Open web browser

Production
----------

[](#production)

For deploy to production purpose, it has `webpack.config.js` in root directory that when we run `webpack` command, we can get `public/js/dist/bundle.js` after run it. If you don't have a `webpack` installed yet in your system, you can install nodejs and install `webpack` and `webpack-cli`:

```
sudo npm install -g webpack
sudo npm install -g webpack-cli
```

So, we can run:

```
webpack

Hash: e1fc11a2dd1c76b96bee
Version: webpack 4.43.0
Time: 506ms
Built at: 07/02/2020 10:09:56 PM
                   Asset      Size  Chunks             Chunk Names
public/js/dist/bundle.js  2.37 KiB       0  [emitted]  main
Entrypoint main = public/js/dist/bundle.js
[0] ./public/js/create-page.js 1.15 KiB {0} [built]
[1] ./public/js/app.js + 2 modules 2.28 KiB {0} [built]
    | ./public/js/app.js 1.63 KiB [built]
    | ./public/js/about.js 130 bytes [built]
    | ./public/js/Navigation.js 529 bytes [built]
```

After it generated, we can update `.env` file as follow:

```
# file .env
CI_ENVIRONMENT = production

app.baseURL    = 'https://www.your-website.com'
app.indexPage  = ''
```

In `app/Views/layout.php`, we have a `ENVIRONMENT` check to use `js/app.js` when on development, and use `/js/dist/bundle.js` on production when exists.

```
// src/App/templates/layout/default.phtml

// ...
