allantatter/laravel-react

在Laravel Blade视图中无缝渲染React.js组件。

v0.1.0 2015-09-02 09:19 UTC

This package is auto-updated.

Last update: 2020-09-11 18:59:14 UTC


README

Laravel React

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

无缝为Laravel Blade模板添加React.js支持。

安装

将以下内容添加到项目的composer.json

"require": {
	"allantatter/laravel-react": "0.1.*"
},

然后运行

$ composer update

服务提供者 & 别名

将以下内容添加到config/app.php中的providers数组

'AllanTatter\React\ReactServiceProvider',

将以下内容添加到config/app.php中的aliases数组

'React' => 'AllanTatter\React\Facades\React',

使用Node.js应用程序设置服务器端渲染

在这个存储库中,你可以找到一个名为example的目录,里面是resources。将内容复制粘贴到自己的项目resources目录中,并将其中的package.json文件从example目录合并到自己的项目package.json中。

然后在项目根目录中运行

$ npm install -g nodemon

$ npm install
$ npm start

如果你将server.js放在另一个位置,你可能需要在该文件中配置此行

require('dotenv').load({path: '../../.env'});

用法

在你的Blade视图中,你可以像这样渲染React组件

{!! React::component('ComponentName', $props) !!}

要渲染react-router路由,你可以使用这个

{!! React::router('routesFileName', $props) !!}

配置

如果你对默认配置不满意,你可以通过.env文件管理一些变量,以下为默认值

REACT_SERVER_URL=https://127.0.0.1
REACT_SERVER_PORT=3000
REACT_COMPONENTS_DIR=components
REACT_EXTENSION=jsx

有用的代码片段

使用react-router处理404页面,通过在app/Exceptions/Handler.phprender()方法中添加此代码片段来渲染你的react应用程序

if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
{
	return response(view('react.app.view'));
}