Laravel 4 All Routes Except Home Result in 404 Err

2020-01-24 11:59发布

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

<?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 TasksController at /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:

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

I am using localhost on a Mac.

18条回答
Animai°情兽
2楼-- · 2020-01-24 12:12

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

  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

It should be working!

查看更多
太酷不给撩
3楼-- · 2020-01-24 12:13

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

Adding

RewriteBase /your_apache_alias

to .htaccess does the trick.

查看更多
神经病院院长
4楼-- · 2020-01-24 12:14

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:

php composer.phar dump-autoload
查看更多
劫难
5楼-- · 2020-01-24 12:15

This comment may be a little late but it may help. I had the same problem when routing to another view like this:

Route::get('index', function () {
return view('index');
});

Route::get('login', function () {
return view('login');
});

But didn't work so I tried everything i found in all the related posts but wasn't enough to solve my problem so i found this lines on the httpd.conf:

<Files ".ht*">
Require all denied
</Files>

So i changed "denied" to "granted" and also commented this line on my .htaccess:

#RewriteBase /

And worked!! I think this lines make Apache not consider the .htaccess file of your project so turning it to granted made the difference. Hope this can help someone with the same problem.

Working on Apache Server 2 @ Manjaro Linux (based on Archlinux)

查看更多
男人必须洒脱
6楼-- · 2020-01-24 12:17

This update worked for me. In the .htaccess file simply add the following after the Rewriterules are turned on.

Options -Indexes
Options +FollowSymLinks

Worked like charm

查看更多
神经病院院长
7楼-- · 2020-01-24 12:18

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

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

Should work.

查看更多
登录 后发表回答