My Routes are Returning a 404, How can I Fix Them?

2019-01-30 21:43发布

I've just started learning the Laravel framework and I'm having an issue with routing.

The only route that's working is the default home route that's attached to Laravel out of the box.

I'm using WAMP on Windows and it uses PHP 5.4.3, and Apache 2.2.22, and I also have mod_rewrite enabled, and have removed the 'index.php' from the application.php config file to leave an empty string.

I've created a new controller called User:

class User_Controller extends Base_Controller {

    public $restful = true;

    public function get_index() 
    {
        return View::make('user.index');
    }
}

I've created a view file in application/views/user/ called index.php with some basic HTML code, and in routes.php I've added the following:

Route::get('/', function () {
    return View::make('home.index');
});

Route::get('user', function () {
    return View::make('user.index');
});

The first route works fine when visiting the root (http://localhost/mysite/public) in my web browser, but when I try to go to my second route with http://localhost/mysite/public/user I get a 404 Not Found error. Why would this be happening?

17条回答
我只想做你的唯一
2楼-- · 2019-01-30 22:41

the simple Commands with automatic loads the dependencies

composer dump-autoload

and still getting that your some important files are missing so go here to see whole procedure

https://codingexpertise.blogspot.com/2018/11/laravel-new.html

查看更多
Root(大扎)
3楼-- · 2019-01-30 22:43

If you're using Vagrant though Homestead, it's possible there was an error mounting the shared folder. It looks like Vagrant takes your files from that folder and swaps out the files that are actually on the host machine on boot, so if there was an error, you're essentially trying to access your Laravel installation from when you first made it (which is why you're only getting "home"- that was generated during installation).

You can easily check this by sshing into your vm and checking the routes/web.php file to see if it's actually your file. If it isn't, exit out and vagrant halt, vagrant up, and look for errors on boot.

查看更多
Luminary・发光体
4楼-- · 2019-01-30 22:46

You could try to move root/public/.htaccess to root/.htaccess and it should work

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-30 22:48

Don't forget the "RewriteBase" in your public/.htaccess :

For example :

Options +FollowSymLinks
RewriteEngine On
RewriteBase /your/folder/public
查看更多
放荡不羁爱自由
6楼-- · 2019-01-30 22:50
Route::get('/', function()
{
return View::make('home.index');
});

Route::get('user', function()
{
return View::make('user.index');
});

change above to

Route::get('user', function()
{
return View::make('user.index');
});

Route::get('/', function()
{
return View::make('home.index');
});

You have to use '/'(home/default) at the end in your routes

查看更多
登录 后发表回答