react和php结合,如何在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

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

Add React in One Minute

Add React in One Minute

This page demonstrates using React with no build tooling.

React is loaded as a script tag.

This is the first comment.

This is the second comment.

This is the third comment.

/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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值