You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Valet项目require_once资源打开失败及React替换Vue配置问题

Hey there! Let’s Fix Your Laravel & React Setup Issues

First, let's break down your two problems and walk through solutions step by step—no confusing jargon, just straight fixes.


1. Fixing the require_once() Resource Failure in Laravel Valet

This error almost always comes down to path mismatches or autoload glitches, especially with how Valet routes your site. Here's what to check:

  • Use Laravel's helper functions for absolute paths: Ditch relative paths like require_once('../vendor/autoload.php')—they can break because Valet uses your public folder as the entry point. Instead, use Laravel's built-in helpers to target your project root directly. For example, in public/index.php, make sure you have:
    require_once base_path('vendor/autoload.php');
    
  • Refresh Composer's autoload cache: Outdated autoload files often cause path mix-ups. Run this in your project root to reset it:
    composer dump-autoload
    
  • Check file permissions: Ensure the file you're trying to require has read access. Run this safe, standard command to fix general permissions (avoid 777—it’s a security risk):
    chmod -R 755 ./your-project-folder
    
  • Verify Valet's site link: Make sure Valet is properly connected to your project. Run valet links to see active sites; if yours isn’t listed, run valet link in your project root to re-establish the connection.

2. Setting Up React (via create-react-app) & Ditching Laravel's Frontend

Since you prefer the React ecosystem and want to drop Laravel Mix, here’s a clean setup to get you going and fix related require issues:

  • Clean up unused Laravel frontend files (optional but recommended): Declutter your project by deleting these:
    • resources/js
    • resources/css
    • webpack.mix.js
  • Set up your React app in the ui folder:
    1. In your Laravel root, run:
      npx create-react-app ui
      
    2. Navigate to the ui folder with cd ui—you can now develop your React app normally with npm start.
  • Fix "require" issues in React:
    • Create React App supports both ES6 import (preferred for React) and CommonJS require. For example:
      // ES6 style
      import React from 'react';
      import { BrowserRouter } from 'react-router-dom';
      
      // Or CommonJS if you prefer
      const React = require('react');
      
    • If you get missing module errors, double-check you’ve installed the package first. For React Router, run:
      npm install react-router-dom
      
  • Connect React to Laravel's API:
    • To avoid CORS errors during development, add a proxy to your React app’s package.json (replace with your Valet site domain):
      "proxy": "http://your-laravel-site.test"
      
      Now, API calls like fetch('/api/posts') from React will automatically forward to your Laravel backend.
    • For production, build your React app with npm run build, then copy the contents of ui/build into Laravel’s public folder. Add a fallback route in routes/web.php to serve React’s index.html for all non-API routes:
      Route::fallback(function () {
          return file_get_contents(public_path('index.html'));
      });
      

内容的提问来源于stack exchange,提问作者ardev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:18:20