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

Laravel路由指向public目录问题求助:Blade视图路由异常带public路径

解决Laravel Blade路由指向public文件夹的问题

看起来你遇到的是Laravel Blade路由生成的URL带public路径,但Vue路由正常的情况,这大概率是服务器根目录配置不对或者Laravel的环境变量设置有误导致的,下面一步步来排查解决:

1. 检查服务器(Apache/Nginx)的根目录配置

这是最常见的原因:你的服务器应该把根目录指向Laravel项目的public文件夹,而不是项目根目录。

Nginx配置示例:

如果用Nginx,找到你的站点配置文件,确保root指向项目的public目录:

server {
    listen 80;
    server_name omis.ezytrade.africa;
    root /path/to/your/laravel-project/public; # 这里要指向public文件夹

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # 根据你的PHP版本调整
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Apache配置示例:

如果用Apache,确保DocumentRoot指向public文件夹,并且启用了mod_rewrite:

<VirtualHost *:80>
    ServerName omis.ezytrade.africa
    DocumentRoot "/path/to/your/laravel-project/public" # 指向public

    <Directory "/path/to/your/laravel-project/public">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

修改后记得重启服务器服务(sudo systemctl restart nginx或者sudo systemctl restart apache2)。

2. 检查Laravel的环境变量(.env)

打开项目根目录下的.env文件,确保APP_URL设置正确,不要包含public或者多余的路径:

APP_URL=https://omis.ezytrade.africa

修改后,记得清除Laravel的配置缓存:

php artisan config:clear
php artisan cache:clear

3. 检查Blade中的路由写法

确保你在Blade模板里使用Laravel的路由辅助函数来生成URL,而不是硬编码路径。比如:

  • 正确的写法:
<a href="{{ route('login') }}">登录</a>
<!-- 或者用url辅助函数 -->
<a href="{{ url('/Login') }}">登录</a>
  • 错误的写法(硬编码带public的路径):
<a href="/omis/public/Login">登录</a>

如果是命名路由,先确认routes/web.php里的路由定义正确,比如:

Route::get('/Login', [LoginController::class, 'show'])->name('login');

4. 检查public目录的.htaccess文件(Apache)

确保Laravel项目public目录下的.htaccess文件是默认的Laravel配置,没有被修改导致路径错误:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

按照上面的步骤逐一排查,应该就能解决Blade路由指向public的问题了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 11:32:48