GithubHelp home page GithubHelp logo

laravel-reactjs's Introduction

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)

<?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.

laravel-reactjs's People

Contributors

beeblebrox3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-reactjs's Issues

Config always returning empty values

After using the command php artisan config:publish sigep/laravel-reactjs and edited the config file, laravel keep returning empty config values...

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"
    ;
}

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()

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.