Laravel 4 除了 Home 之外的所有路由都导致 404 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13514990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:21:04  来源:igfitidea点击:

Laravel 4 All Routes Except Home Result in 404 Error

laravellaravel-4routinghttp-status-code-404laravel-routing

提问by Amit Erandole

I installed Laravel 4 using Composer and also set up a virtual host. Currently, only the root route is working:

我使用 Composer 安装了 Laravel 4 并设置了一个虚拟主机。目前,只有根路由在工作:

<?php

Route::get('/', function()
{
    return View::make('hello');
});

This is not:

这不是:

Route::get('/hello', function()
{
    return View::make('hello');
});

What I'm trying to hit is TasksControllerat /tasks:

我试图打是TasksController/tasks

Route::resource('tasks', 'TasksController');

This is giving me 404 error as well. What could I be doing wrong? I have a default .htaccess file at the root of my project:

这也给了我 404 错误。我可能做错了什么?我的项目根目录中有一个默认的 .htaccess 文件:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

I am using localhost on a Mac.

我在 Mac 上使用本地主机。

回答by GaryJ

Just for a laugh, see if /index.php/helloworks.

只是为了笑,看看是否/index.php/hello有效。

If so, then most likely it's a .htaccessproblem.

如果是这样,那么很可能这是一个.htaccess问题。

回答by Rutger

Had the same problem running Laravel 4 on WAMP (Windows 8).
The solution that worked for me was:

在 WAMP (Windows 8) 上运行 Laravel 4 时遇到了同样的问题。
对我有用的解决方案是:

  1. Open apache httpd.conf and find this line :

    #LoadModule rewrite_module modules/mod_rewrite.so
    
  2. Uncomment this line (remove the #)
  3. Save httpd.conf
  4. Restart WAMP
  1. 打开 apache httpd.conf 并找到这一行:

    #LoadModule rewrite_module modules/mod_rewrite.so
    
  2. 取消注释此行(删除#
  3. 节省 httpd.conf
  4. 重启 WAMP

It should be working!

它应该工作!

回答by manujas

I had the same problem and the solution was enable the rewrite mod on Apache2

我遇到了同样的问题,解决方案是在 Apache2 上启用 rewrite mod

In a terminal use the following commands:

在终端中使用以下命令:

$ sudo a2enmod rewrite
$ sudo service apache2 restart

And magic!

还有魔法!

回答by Orhan

Had exactly the same problem.

有完全相同的问题。

Two things I needed to do to fix this:

我需要做两件事来解决这个问题:

  1. enable rewrite_modulein Apache
  2. Change the AllowOverridefrom None to All, example (Apache 2.4.9):

    <Directory "c:/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    
  1. 在 Apache 中启用rewrite_module
  2. AllowOverride从 None更改为All,例如(Apache 2.4.9):

    <Directory "c:/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    

FYI:
use Laravel Homestead together with VirtualBox and Vagrant instead of WAMP. It contains Nginx instead of Apache but everything works by default as it is configured for Laravel explicitly.

仅供参考:
将 Laravel Homestead 与 VirtualBox 和 Vagrant 一起使用,而不是 WAMP。它包含 Nginx 而不是 Apache,但默认情况下一切正常,因为它是为 Laravel 显式配置的。

回答by MartinGomez

Even after enabling mod_rewrite, you may face this problem if you are aliasing your laravel/public folder.

即使在启用 mod_rewrite 之后,如果您为 laravel/public 文件夹设置别名,您也可能会遇到这个问题。

Adding

添加

RewriteBase /your_apache_alias

to .htaccess does the trick.

.htaccess 可以解决问题。

回答by BassMHL

FOR UBUNTU USERS- tested for Ubuntu 18.04

对于UBUNTU 用户- 已针对 Ubuntu 18.04 进行测试

1- Enable mod rewrite

1- 启用模组重写

sudo a2enmod rewrite

2- Change AllowOverride in apache conf file:

2- 更改 apache conf 文件中的 AllowOverride:

sudo nano /etc/apache2/apache2.conf

Change the AllowOverride from None to All in this block

在此块中将 AllowOverride 从 None 更改为 All

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

3- Restart Apache

3-重新启动Apache

sudo service apache2 restart

回答by davzie

You don't need a / when defining anything other than home:

定义除 home 以外的任何内容时,您不需要 / :

Route::get('hello', function()
{
    return View::make('hello');
});

Should work.

应该管用。

回答by Dylan Glockler

I tried all these with no success then i found another post and changed my .htaccess to this:

我尝试了所有这些但没有成功,然后我找到了另一篇文章并将我的 .htaccess 更改为:

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L]
</IfModule>

回答by Caleb Mbakwe

There was a little flaw in a previous answer. This might help out.

之前的回答有一点瑕疵。这可能会有所帮助。

$ sudo a2enmod rewrite
$ sudo service apache2 restart

Hope it does.

希望确实如此。

回答by Niklas Modess

Since Laravel 4 is autoloading files from a map in a static file, you need to update that file when you add a new controller. Run this command to rebuild the static file:

由于 Laravel 4 会从静态文件中的地图自动加载文件,因此您需要在添加新控制器时更新该文件。运行此命令以重建静态文件:

php composer.phar dump-autoload