如何在Apache服务器上集成Reactjs前端和php codeigniter应用程序?
作者:互联网
CodeIgniter应用程序开发得更早,但当时没有计划集成ReactJS.添加了后来的要求以将另一个ReactJS项目与此后端集成并替换当前前端(视图).
CodeIgniter应用程序不是作为RESTful API完成的. .php视图文件无法替换为reactjs应用程序的.js文件,因为服务器是Apache.
运行nodejs服务器不会呈现CodeIgniter视图.
BootIrap,jquery和简单的javascript可以包含在CodeIgniter应用程序的视图中.但是有可能用JavaScript文件替换CodeIgniter中的PHP视图文件吗?
解决方法:
PHP视图文件不需要用js文件替换.使用< script>可以轻松地将JavaScript添加到PHP文件中标签.以下是CodeIgniter应用程序中的Add React in One Minute演示.
要将React演示集成到CodeIgniter中,请使用简单的控制器 – React.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class React extends CI_Controller
{
public function index()
{
$this->load->view('react_view');
}
}
“视图”文件直接来自React演示,但它放在.php文件而不是.html中.
对演示代码所做的唯一更改是在页面底部的脚本标记中.我的资源文件夹与CodeIgniter的/ application文件夹处于同一级别. css,js和images的资源中有子文件夹.
/public_html
/application
/system
/assets
/js
/css
/img
所以我更改了加载like_button.js的标记的src以使用我的文件布局.
“view”文件react_view.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Add React in One Minute</title>
</head>
<body>
<h2>Add React in One Minute</h2>
<p>This page demonstrates using React with no build tooling.</p>
<p>React is loaded as a script tag.</p>
<p>
This is the first comment.
<!-- We will put our React component inside this div. -->
<div class="like_button_container" data-commentid="1"></div>
</p>
<p>
This is the second comment.
<!-- We will put our React component inside this div. -->
<div class="like_button_container" data-commentid="2"></div>
</p>
<p>
This is the third comment.
<!-- We will put our React component inside this div. -->
<div class="like_button_container" data-commentid="3"></div>
</p>
<!-- Load React. -->
<!-- Note: when deploying, replace "development.js" with "production.min.js". -->
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<!-- Load our React component. -->
<script src="assets/js/like_button.js"></script>
</body>
</html>
/assets/js/like_button.js
'use strict';
const e = React.createElement;
class LikeButton extends React.Component {
constructor(props) {
super(props);
this.state = { liked: false };
}
render() {
if (this.state.liked) {
return 'You liked comment number ' + this.props.commentID;
}
return e(
'button',
{ onClick: () => this.setState({ liked: true }) },
'Like'
);
}
}
// Find all DOM containers, and render Like buttons into them.
document.querySelectorAll('.like_button_container')
.forEach(domContainer => {
// Read the comment ID from a data-* attribute.
const commentID = parseInt(domContainer.dataset.commentid, 10);
ReactDOM.render(
e(LikeButton, { commentID: commentID }),
domContainer
);
});
标签:php,reactjs,codeigniter,apache,grocery-crud 来源: https://codeday.me/bug/20190910/1801542.html