未找到 Laravel 路由,默认根路由除外

Posted

技术标签:

【中文标题】未找到 Laravel 路由,默认根路由除外【英文标题】:Laravel routes not found, except for the default root route 【发布时间】:2018-09-29 05:51:33 【问题描述】:

我已经使用 Homestead 安装了 Laravel,但由于某种原因,我的路由无法正常工作(404 错误),只有默认的“/”可以。

这些是我创建的其他路线:

Route::get('test', function() // it doesn't work like so either: "/test" 
    return "Test";
);

Route::get('user', function()
    return "User";
);

没有一个到达。

如果我列出所有路线,这就是我得到的:

D:\web\laravel>php artisan route:list
+--------+----------+----------+------+---------+--------------+
| Domain | Method   | URI      | Name | Action  | Middleware   |
+--------+----------+----------+------+---------+--------------+
|        | GET|HEAD | /        |      | Closure | web          |
|        | GET|HEAD | api/user |      | Closure | api,auth:api |
|        | GET|HEAD | test     |      | Closure | web          |
|        | GET|HEAD | user     |      | Closure | web          |
+--------+----------+----------+------+---------+--------------+

知道为什么这些不起作用吗?据我所知,Homestead 中的 web 服务器和 php 不需要任何额外的手动配置来允许这些基本代码工作。

编辑

如果我列出来自 VM 内的路由,我的路由不存在!:

vagrant@homestead:~/code$ php artisan route:list
+--------+----------+----------+------+---------+--------------+
| Domain | Method   | URI      | Name | Action  | Middleware   |
+--------+----------+----------+------+---------+--------------+
|        | GET|HEAD | /        |      | Closure | web          |
|        | GET|HEAD | api/user |      | Closure | api,auth:api |
+--------+----------+----------+------+---------+--------------+

这是我在 Homestead.yaml 中的共享文件夹配置:

folders:
    - map: D:/web/laravel
      to: /home/vagrant/code

网站设置:

sites:
    - map: homestead.test
      to: /home/vagrant/code/public

如果我从 /home/vagrant/code 执行 ls

vagrant@homestead:~/code$ ls
app      bootstrap      composer.lock  database      phpunit.xml  readme.md  routes      storage  vendor
artisan  composer.json  config         package.json  public       resources  server.php  tests    webpack.mix.js

这是我执行“vagrant up --provision”时的输出:

PS C:\Homestead> vagrant up --provision
Bringing machine 'homestead-7' up with 'virtualbox' provider...
==> homestead-7: Checking if box 'laravel/homestead' is up to date...
==> homestead-7: Clearing any previously set forwarded ports...
==> homestead-7: Clearing any previously set network interfaces...
==> homestead-7: Preparing network interfaces based on configuration...
    homestead-7: Adapter 1: nat
    homestead-7: Adapter 2: hostonly
==> homestead-7: Forwarding ports...
    homestead-7: 80 (guest) => 8000 (host) (adapter 1)
    homestead-7: 443 (guest) => 44300 (host) (adapter 1)
    homestead-7: 3306 (guest) => 33060 (host) (adapter 1)
    homestead-7: 4040 (guest) => 4040 (host) (adapter 1)
    homestead-7: 5432 (guest) => 54320 (host) (adapter 1)
    homestead-7: 8025 (guest) => 8025 (host) (adapter 1)
    homestead-7: 27017 (guest) => 27017 (host) (adapter 1)
    homestead-7: 22 (guest) => 2222 (host) (adapter 1)
==> homestead-7: Running 'pre-boot' VM customizations...
==> homestead-7: Booting VM...
==> homestead-7: Waiting for machine to boot. This may take a few minutes...
    homestead-7: SSH address: 127.0.0.1:2222
    homestead-7: SSH username: vagrant
    homestead-7: SSH auth method: private key
    homestead-7: Warning: Connection aborted. Retrying...
    homestead-7: Warning: Connection reset. Retrying...
    homestead-7: Warning: Connection aborted. Retrying...
    homestead-7: Warning: Connection reset. Retrying...
    homestead-7: Warning: Connection aborted. Retrying...
    homestead-7: Warning: Connection reset. Retrying...
    homestead-7: Warning: Connection aborted. Retrying...
    homestead-7: Warning: Connection reset. Retrying...
    homestead-7: Warning: Connection aborted. Retrying...
    homestead-7: Warning: Remote connection disconnect. Retrying...
    homestead-7: Warning: Connection aborted. Retrying...
    homestead-7: Warning: Remote connection disconnect. Retrying...
    homestead-7: Warning: Connection aborted. Retrying...
    homestead-7: Warning: Connection reset. Retrying...
    homestead-7: Warning: Connection aborted. Retrying...
    homestead-7: Warning: Remote connection disconnect. Retrying...
    homestead-7: Warning: Connection aborted. Retrying...
==> homestead-7: Machine booted and ready!
==> homestead-7: Checking for guest additions in VM...
    homestead-7: The guest additions on this VM do not match the installed version of
    homestead-7: VirtualBox! In most cases this is fine, but in rare cases it can
    homestead-7: prevent things such as shared folders from working properly. If you see
    homestead-7: shared folder errors, please make sure the guest additions within the
    homestead-7: virtual machine match the version of VirtualBox you have installed on
    homestead-7: your host and reload your VM.
    homestead-7:
    homestead-7: Guest Additions Version: 5.0.18_Ubuntu r106667
    homestead-7: VirtualBox Version: 5.2
==> homestead-7: Setting hostname...
==> homestead-7: Configuring and enabling network interfaces...
==> homestead-7: Mounting shared folders...
    homestead-7: /vagrant => C:/Homestead
==> homestead-7: Running provisioner: file...
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: inline script
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: inline script
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: inline script
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: inline script
    homestead-7: Unable to mount one of your folders. Please check your folders in Homestead.yaml
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: C:/Users/[user]/AppData/Local/Temp/vagrant-shell20180424-1628-11jxfyp.sh
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: inline script
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: inline script
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: script: Creating Certificate: homestead.test
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: script: Creating Site: homestead.test
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: inline script
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: script: Checking for old Schedule
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: script: Clear Variables
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: script: Restarting Cron
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: script: Restarting nginx
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: script: Creating mysql Database: homestead
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: script: Creating Postgres Database: homestead
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: script: Update Composer
    homestead-7: You are running composer as "root", while "/home/vagrant/.composer" is owned by "vagrant"
    homestead-7: You are already using composer version 1.6.4 (stable channel).
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: C:/Users/[user]/AppData/Local/Temp/vagrant-shell20180424-1628-1j2nixt.sh
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: C:/Users/[user]/AppData/Local/Temp/vagrant-shell20180424-1628-ltovu3.sh

我看过这条线:

homestead-7: Unable to mount one of your folders. Please check your folders in Homestead.yaml

它没有提供详细信息。

D:/web/laravel/home/vagrant/code都有相同的内容,但是我注意到如果我创建一个随机文件,就叫它D :/web/laravel/deleteme.txt,即使我重新加载 vagrant,它也不会显示在 /home/vagrant/code 中。

【问题讨论】:

也许他们应该是/test 不。我已经在开始时尝试使用斜线,但也没有用。 D:\web\laravel> 对我来说似乎不是 linux 路径。 你的问题。您编辑了项目的本地版本,但它并未反映您的 VM 中的版本。检查Configuring Shared Folders 部分以获得帮助。 我希望我能帮助你,但距离我上次在 Homestead 中使用 Windows 已经有一分钟了,我绝不是这方面的专家。 【参考方案1】:

您是否检查过是否启用了 mod_rewrite?

另外,请检查您的公共目录

<Directory "/path/to/your/directory">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

【讨论】:

【参考方案2】:

我认为您需要对您的配置执行此操作:

来自:

folders:
- map: D:/web/laravel
  to: /home/vagrant/code

到:

folders:
- map: D:/web/laravel/code
  to: /home/vagrant/code

【讨论】:

没有 D:/web/laravel/code 目录。我的 Laravel 代码位于 D:/web/laravel 目录中。不管怎样,我试过了,但没有用。 你的 server.php 文件在哪里?无论在哪里,都是需要回答的目录。【参考方案3】:

你在窗户上,对吧?

改变

folders:
    - map: D:/web/laravel
      to: /home/vagrant/code

folders:
    - map: D:\web\laravel
      to: /home/vagrant/code

如果您只有一个项目,请在不使用 --provision 的情况下运行。

【讨论】:

【参考方案4】:

这个呢?

folders:
- map: ~/code
  to: /home/vagrant/code

网站: - 地图:homestead.test 到:/home/vagrant/code/public

或者有时这个问题是 .htaccess 问题尝试检查 mod_rewrite

<Directory "/path/to/your/directory">
   Options Indexes FollowSymLinks
   AllowOverride 'none' /** or something change to all */
   Require all granted
</Directory>

AllowOverride All

希望能给你一个想法。

【讨论】:

【参考方案5】:

我还会检查 .htaccess 文件是否存在,您可以在线找到 laravel 的默认 htaccess 文件。如果 .htaccess 文件存在,那么我会做一个 PHP artisan route:clear 和 PHP artisan config:clear,有时配置和路由会发挥作用。

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

    RewriteEngine On

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

    # Handle Front Controller...
    RewriteCond %REQUEST_FILENAME !-d
    RewriteCond %REQUEST_FILENAME !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %HTTP:Authorization .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%HTTP:Authorization]
</IfModule>

【讨论】:

以上是关于未找到 Laravel 路由,默认根路由除外的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5.6 '/' 找到路由但未找到其他路由 AWS EC2

Laravel - 如果未经授权,重定向到登录,即使是路由未注册

Laravel RegisterController 路由未找到

Laravel API 路由 404 未找到修复?

未找到 Laravel API 路由

未找到 Laravel API 路由