escribiendocodigo/phalcon-react-project-skeleton

Phalcon React项目骨架

v5.0.0 2023-10-03 01:21 UTC

This package is not auto-updated.

Last update: 2024-10-01 12:14:33 UTC


README

使用Phalcon框架Vite + React构建的骨架应用。

要求

  • PHP >= 7.4.1
  • Phalcon >= 5.0.0
  • Node >= 18.0.0

结构

my-project/
  ¦
  ├-- backend/
  ¦
  └-- frontend/

后端

Phalcon微应用,提供一个运行在8000端口的RESTful API

前端

Vite + React应用,运行在5173端口

后端代理

https://:5173/api -> https://:8000

// vite.config.js

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

const BACKEND_PORT = process.env.BACKEND_PORT || 8000;

export default defineConfig({
  plugins: [react()],
  server: {
    host: "0.0.0.0",
    proxy: {
      "/api": {
        target: `http://127.0.0.1:${BACKEND_PORT}`,
        // changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ""),
      },
    },
  },
});

通过Composer安装

composer create-project escribiendocodigo/phalcon-react-project-skeleton my-project

安装后,您可以使用PHP内置的Web服务器立即进行测试

cd my-project

运行后端

cd backend
composer serve
# OR use the composer alias:
composer serve-backend

运行前端

cd frontend
npm run dev
# OR use the composer alias:
composer serve-frontend

构建前端

cd frontend
npm run build
# OR use the composer alias:
composer build-frontend

Web服务器设置

Nginx

server {
    listen 80;
    server_name my-domain www.my-domain;

    index index.html index.php;

    access_log /var/log/nginx/my-domain.access.log;
    error_log /var/log/nginx/my-domain.error.log;

    location / {
        root /var/www/my-project/frontend/dist;

        try_files $uri $uri/ /index.html;
    }

    location /api {
        rewrite ^/api(.*)$ /index.php;
        set $request_url $1;
    }

    location ~ \.php$ {
        root /var/www/my-project/backend/public;

        include /etc/nginx/snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param REQUEST_URI $request_url;
    }

    location ~ /\.ht {
        deny all;
    }

}