Code Monkey home page Code Monkey logo

cakephp-react-backend's Introduction

CakePHP with a React Page Embedded in a View

CakePHP 3.x Middleware to allow for development and API access

src/Middleware/HttpOptionsMiddleware.php from https://www.munderwood.ca/index.php/2017/02/28/responding-to-http-options-requests-in-cakephp/

with additions inspired by https://stackoverflow.com/a/7454204

Setup

You need composer and node

git clone https://github.com/jmcd73/cakephp-react-backend.git
cd cakephp-react-backend
composer install #  installs CakePHP, DebugKit
git submodule init
git submodule update
cd cakephp-react-frontend
yarn install
yarn start

Create a database (DB name is "cake") using the config/schema/cake.sql file

mysql -uroot -p <yourpassword> < config/schema/cake.sql

Copy config/app.default.php to config/app.php and change the DB connection details to suit the above database

Run the default cake server

bin/cake server

Login to the CakePHP app (assuming you serving it from the web root)

http://localhost:8765/users/login

Email: [email protected]

password: password

src/Template/Articles/react.ctp contains the code to mount the react SPA (Single Page Application)

View the react() function block in src/Controller/ArticlesController.php to see how the react js and css is loaded from webroot/react/asset-manifest.json

For allowing fetch() to have access to the CakePHP endpoints see changes in

Cake File Change
config/routes.php Added resource route and json extension
src/Middleware/HttpOptionsMiddleware.php Middleware to respond to OPTIONS and allow CORS
src/Application.php Place middleware is hooked into cake

The CsrfProtectionMiddleware has been switched off because I couldn't get it working so I have commented it out in src/Application.php

/*
 ->add(new CsrfProtectionMiddleware([
                'httpOnly' => true
            ]));
*/

cakephp-react-backend's People

Contributors

toggenation avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.