Created
April 22, 2016 10:51
-
-
Save bekatom/a0f2b85aaf64b2f26b232e7691d22157 to your computer and use it in GitHub Desktop.
ReactHelperClass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: beka | |
* Date: 4/4/16 | |
* Time: 11:59 AM | |
*/ | |
namespace LSCore; | |
use GuzzleHttp\Client; | |
class ReactHelper | |
{ | |
private static $initialized = false; | |
private static $client; | |
private static function initialize() | |
{ | |
if (self::$initialized) | |
return self::$client; | |
global $configArr; | |
self::$client = new Client(['base_uri' => $configArr['node_stock_host'], 'verify' => false]); | |
self::$initialized = true; | |
return self::$client; | |
} | |
public static function get_component($view_name,$package_name) | |
{ | |
$c = self::initialize(); | |
$response = $c->get('/v1/render/get/' . $package_name .'/'. $view_name); | |
$contents = $response->getBody()->getContents(); | |
return $contents; | |
} | |
public static function render_component($render_url){ | |
$c = self::initiaalize(); | |
$response = $c->get($render_url); | |
$contents = $response->getBody()->getContents(); | |
return $contents; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by beka on 4/1/16. | |
*/ | |
'use strict'; | |
require('babel-core/register'); | |
var config = require('../../../config'); | |
var React = require('react'); | |
var ReactDOMServer = require('react-dom/server'); | |
var path = require('path'); | |
var debug = require('debug')('api'); | |
module.exports = { | |
get_react: get_react | |
}; | |
// Get React component , VIEW AS SERVICE | |
function get_react(req, res) { | |
try { | |
var view = path.resolve(config.REACT_DIR + '/' + req.params.package_name + "/" + req.params.view_name); | |
var component = require(view).default; | |
var props = req.body || null; | |
res.status(200).send( | |
ReactDOMServer.renderToString( | |
React.createElement(component, props) | |
) | |
); | |
} catch (err) { | |
console.log("ERR : ",err); | |
debug(err); | |
res.badRequest(err); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var renderController = require('./renderController.v1.js'); | |
module.exports = { | |
'/v1/render' : { | |
// render as service only DOM , than we can pass data from PHP or | |
// from another resource , we can eat React component from other third party resource | |
'/get/:package_name/:view_name' : { | |
all : renderController.get_react | |
}, | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment