[英]How to make a server call (PHP) using axios (react-redux) from localhost (using express server)?
I'm new to React. 我是React的新手。 I have a button in one of the components, on clicking the button, a server side (PHP) call should be made using axios GET call and the response should change the state of the store.
我在组件之一中有一个按钮,单击该按钮后,应使用axios GET调用进行服务器端(PHP)调用,并且响应应更改存储的状态。
I have set the app using webpack and using the express server. 我已经使用webpack和Express Server设置了应用程序。 What changes to their settings be made in order to run PHP code?
为了运行PHP代码,对其设置进行了哪些更改?
I have set the app using webpack and using the express server.
我已经使用webpack和Express Server设置了应用程序。 What changes to their settings be made in order to run PHP code?
为了运行PHP代码,对其设置进行了哪些更改?
You don't have to change any settings there. 您不必在那里更改任何设置。 These tools can't run PHP code.
这些工具无法运行PHP代码。 If you want to serve your static files (
.html
, .css
, .js
) with express you can leave that as it is. 如果您希望使用express服务静态文件(
.html
, .css
, .js
),则可以保留原样。 But you have to run another local webserver next to express that can run PHP code and that listens on another port. 但是您必须在旁边运行另一个本地网络服务器来表示可以运行PHP代码并在另一个端口上进行侦听。
PHP 5.4+
has a built-in development server. PHP 5.4+
具有内置的开发服务器。 Or you can use nginx
or apache
. 或者,您可以使用
nginx
或apache
。
You could use eg Laravel which is a popular web application framework. 您可以使用Laravel等流行的Web应用程序框架。 This makes it easy to run a local development server and to define a RESTful api which you will need to provide in order to communicate with your react app.
这使得运行本地开发服务器和定义RESTful API变得很容易,您需要提供该API才能与react app进行通信。 Laravel uses the built-in PHP web server.
Laravel使用内置的PHP Web服务器。 When you installed Laravel you can start it with:
当您安装Laravel时,可以使用以下命令启动它:
php artisan serve
This command will start a development server at http://localhost:8000
. 该命令将在
http://localhost:8000
启动开发服务器。 Express is per default running at http://localhost:3000
. Express默认情况下运行在
http://localhost:3000
。
Then you can define a REST api in Laravel and send requests to it. 然后,您可以在Laravel中定义REST api并将请求发送给它。
Of course you can use other PHP frameworks too if you don't want to use Laravel. 当然,如果您不想使用Laravel,也可以使用其他PHP框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.