18Laravel ReactJS Package to simplify sending data from Laravel back-end to front-end built to Facebook ReactJS.

Overview

Laravel ReactJS

This is a package that we wrote to use on our Laravel applications that use React from Facebook. Our goal is deal with the SEO problem that JavaScript based applications have and make easier to send data from back-end to JavaScript without making more requests.

The project that motivated this package wasn't a SPA and React wasn't used on all pages. It help us to keep the code of our views clean.

Attention: this package is on development state, so... be careful ;)

We based our code on this package from Facebook.

Requirements

Installing

Add the dependency on your composer.json:

{
    [...]
    "require": {
        "sigep/laravel-reactjs": "*",
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/cohros/laravel-reactjs"
        }
    ],
}

Configure the service provider and alias on your application config (/app/config/app.php)


return array (
    [...],
    'providers' => array (
        [...],
        'Sigep\LaravelReactJS\ReactJSServiceProvider',
    ),
    'alias' => array (
        [...],
        'ReactJS' => 'Sigep\LaravelReactJS\ReactJSFacade',
    )
);

Usage

You have two options here. Use the first (A) when you want to provide your source files without any kind of dependency management system. The second (B) was tested with Browserify, tool that we use, but we want to test with others too.

Separated files (A)

You have to configure just two things on this approach. react_src: path to react source file; src_files: array with all your source files. Keep in mind that all files will be included in the order that you declare it.

Using with browserify (B)

You probably will use three configs: src_files: path to your bundle; react_prefix: probably you will use the standalone option of browserify and exports a variable (named Application, for example). You need to pass that variable name in react_prefix config. Look on examples directory to see how we do this; components_prefix: like the above rule, that is a prefix to your components. Can be the same as the react_prefix, but we keep it separated like:

module.exports = {
    libs: {
        React: require('react')
    },
    components: {
        MyComponent: require('mycomponent')
    }
}

Getting the markup

With ReactJS you can get the html code that React generates when you ask it to render some component. What will happens here is: ReactJS will use the v8 engine to run your code and get the html markup of your component. You will put the result on your page and the client (let's say google) will get the content without have to run any JavaScript code.

First you have to define the component that will be used:

ReactJS::component('ComponentName');

If you need to pass props to your component, use the data method:

ReactJS::data(['prop_a' => 'value a', 'prop_b' => 'value_b']);

Now you just have to call the markup method to get the html code:

ReactJS::markup();

Tip: If you need to render several times the same component, the component method doesn't need to be called multiple times:

ReactJS::component('Foo');

ReactJS::data(['xpto' => '100']);
echo ReactJS::markup();

ReactJS::data(['xpto' => '200']);
echo ReactJS::markup();
[...]

Getting the JavaScript code

You need to tell React to render you component to the events and data-bidings work properly on the client browser. The js method will generate the necessary code to do that:

ReactJS::component('Foo');
ReactJS::data(['xpto' => '100']);

echo ReactJS::js('#target-element');

Note that if the server-rendering fails, the code on front-end will create the elements normaly, so if something goes wrong on the server, the page will have the components.

API

Method Parameters Description
ReactJS::setErrorHandler() callable $errorHandler Setup the function to call when error occurs
ReactJS::component() string $name = null Set the component name if provided or returns the current value
ReactJS::data() array $data = null Set the component props if provided or returns the current value
ReactJS::markup() Get the markup generated by react after render the component
ReactJS::js() string $element (selector of the container for the component)
$return_val = null (if you provide a name, a variable will be created with the component)
Get js markup to call React.renderComponent()

Configs

Config Type Description
basepath string (optional) basepath to your source files
react_src string (optional) path to react_js source file. If you use Browserify, leave this empty
src_files array list of source files necessary to run your code. If you use Browserify, pass only the bundle.
react_prefix string (optional) If exists a path to access React object, pass the prefix in here (ex: App.libs).
components_prefix string (optional) If exists a path to access your components, pass the prefix in here (ex: App.components).

Change Log

[1.0.2] - 2015-03-23

  • Support for react 0.13 API changes

Written with StackEdit.

You might also like...
A package for building Admin-Interfaces that help maintaining the data of your applications
A package for building Admin-Interfaces that help maintaining the data of your applications

A package for building Admin-Interfaces that help maintaining the data of your applications. It provides an intuitive interface and the tools needed to manage your project's Users, Models and free Forms for Pages, Settings etc.

Exemplary RealWorld backend API built with Laravel PHP framework.
Exemplary RealWorld backend API built with Laravel PHP framework.

Example of a PHP-based Laravel application containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld API spec.

A Web Artisan list of categorized OPEN SOURCE PROJECTS built with Laravel PHP Framework.

Laravel-Open-Source-Projects A Web Artisan list of categorized OPEN SOURCE PROJECTS built with Laravel PHP Framework. This repository includes a compr

Admin One is simple, beautiful and free Laravel admin dashboard (built with Vue.js, Bulma & Buefy).
Admin One is simple, beautiful and free Laravel admin dashboard (built with Vue.js, Bulma & Buefy).

Admin One — Free Laravel Vue Bulma Dashboard Admin One is simple, beautiful and free Laravel admin dashboard (built with Vue.js, Bulma & Buefy). Built

LaraAdmin is a Open source Laravel Admin Panel / CMS which can be used as Admin Backend, Data Management Tool or CRM boilerplate for Laravel with features like Advanced CRUD Generation, Module Manager, Backups and many more.
LaraAdmin is a Open source Laravel Admin Panel / CMS which can be used as Admin Backend, Data Management Tool or CRM boilerplate for Laravel with features like Advanced CRUD Generation, Module Manager, Backups and many more.

LaraAdmin 1.0 LaraAdmin is a Open source CRM for quick-start Admin based applications with features like Advanced CRUD Generation, Schema Manager and

A simple CRUD built in PHP, Bootstrap and MySQL
A simple CRUD built in PHP, Bootstrap and MySQL

✨ Notes-CRUD ✨ A simple CRUD built in PHP, Bootstrap and MySQL 📑 Table of Contents Usage Contribute Screenshots 🤖 Usage Add the project to your envi

Reverse proxy skeleton built for docker with traefik, showcasing a Symfony + React application

Decoupled Backend(Symfony) + Frontend(React ts) built with Traefik & Docker Reverse proxy skeleton built for docker with traefik, showcasing a decoupl

Pterodactyl is an open-source game server management panel built with PHP 7, React, and Go
Pterodactyl is an open-source game server management panel built with PHP 7, React, and Go

Pterodactyl Panel Pterodactyl is an open-source game server management panel built with PHP 7, React, and Go. Designed with security in mind, Pterodac

Powerful data objects for Laravel
Powerful data objects for Laravel

Powerful data objects for Laravel This package enables the creation of rich data objects which can be used in various ways. Using this package you onl

Comments
  • Recent changes to React API

    Recent changes to React API

    Console errors get logged when the ReactJS::js method is called, including:

    Warning: Something is calling a React component directly. Use a factory or JSX instead. See: http://fb.me/react-legacyfactory

    I also had problems getting this method working at all, but that may have been due to other factors. Anyway, I rewrote it to suppress the errors, like this:

    public function js($element, $return_var = null)
    {
        $component_name = $this->component;
        $react = $this->react_prefix . 'React';
        $data = json_encode($this->data);
        $assignment = $return_var ? "$return_var = " : '';
        return "var component = $react.createFactory($component_name);\n" .
        "$assignment $react.render(component($data), document.querySelector('$element'));\n"
        ;
    }
    
    opened by alfaguru 2
  • Facade issue

    Facade issue

    Hi, when I try to place something like this in a view partial: {{ ReactJs::component('example'); }}

    I'm getting an error saying:

    Call to undefined method Sigep\LaravelReactJS\ReactJSFacade::component()
    
    opened by grayxr 1
Owner
Cohros
Cohros
A PHP notebook application build with PHP Symfony as back-end API and VueJS/Vuetify front-end.

PHPersonal Notes ?? - BETA RELEASE PHPersonal notes is an application to store your personal notes! PHPersonalnotes is build with Symfony/VueJS/Vuetif

Robert van Lienden 3 Feb 22, 2022
A Laravel dashboard front-end scaffolding preset for Tailwind CSS - Support RTL out of the box.

?? Laravel tailwind css dashboard preset A Laravel dashboard front-end scaffolding preset for Tailwind CSS - Support RTL out of the box. Usage Fresh i

Miaababikir 343 Dec 7, 2022
A lightweight full-stack component layer that doesn't dictate your front-end framework

Airwire A lightweight full-stack component layer that doesn't dictate your front-end framework Demo Introduction Airwire is a thin layer between your

ARCHTECH 199 Nov 23, 2022
Package for using ReactJS with Laravel

react-laravel With react-laravel you'll be able to use ReactJS components right from your Blade views, with optional server-side rendering, and use th

Talysson de Oliveira Cassiano 900 Jan 3, 2023
Wordpress, ReactJS, GUTENBERG, plugin

Плагін для Wordpress, який розширює функціонал редактора Gutenderg, реалізуючи можливість створювати клієнтську частину використовуючи потужні можливо

turovskiy 1 Nov 25, 2021
A @laravel based RAD platform for back-office applications, admin/user panels, and dashboards.

For the full documentation, visit orchid.software. Introduction Orchid is a free Laravel package that abstracts standard business logic and allows cod

Laravel Orchid 3.4k Jan 1, 2023
Microservice for sending emails with Lumen

Microservice for send email Microservice developed in Lumen for sending emails in real time or by queue. Current Features Send email in real time Send

Rodrigo Lacerda 75 Jan 9, 2023
Web Sekolah yang dibuat diatas CMS Popoji dengan base Laravel 6. Web Sekolah ini sudah diintegrasikan dengan template semesta-front.

Web Sekolah yang dibuat diatas CMS Popoji dengan base Laravel 6. Web Sekolah ini sudah diintegrasikan dengan template semesta-front.

Muhamad Ramdani Hidayatullah 1 Feb 6, 2022
Until 2018, Backpack v3 used this Base package to offer admin authentication and a blank admin panel using AdminLTE. Backpack v4 no longer uses this package, they're now built-in - use Backpack/CRUD instead.

Note: This package is only used by Backpack v3. Starting with Backpack v4, everything this package does is included in Backpack/CRUD - one package to

Backpack for Laravel 845 Nov 29, 2022
A Laravel Starter Kit for Laravel. Built with Laravel 8.

Laravel Get Started Project Laravel Get Started Project is a basic crud app built with laravel 8. In this app a basic product crud created. Features i

Nazmul Hasan Robin 8 Nov 24, 2022