我的路线返回了404,我该如何解决? [英] My Routes are Returning a 404, How can I Fix Them?

查看:67
本文介绍了我的路线返回了404,我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Laravel框架,但是路由方面却遇到了问题.

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

唯一有效的路由是开箱即用的连接到Laravel的默认本地路由.

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

我在Windows上使用WAMP,它使用PHP 5.4.3和Apache 2.2.22,并且还启用了mod_rewrite,并从application.php配置文件中删除了"index.php",以保留空字符串.

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.

我创建了一个名为 User 的新控制器:

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');
    }
}

我已经在application/views/user/中创建了一个名为 index.php 的视图文件,其中包含一些基本的HTML代码,并在route.php中添加了以下内容:

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');
});

当在Web浏览器中访问根目录(http://localhost/mysite/public)时,第一条路由工作正常,但是当我尝试使用http://localhost/mysite/public/user进入第二条路由时,出现404 Not Found错误.为什么会这样?

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?

推荐答案

您是否尝试过将其添加到路由文件中,而不是Route::get('user', "user@index")?

Have you tried adding this to your routes file instead Route::get('user', "user@index")?

在这种情况下,在@user之前的一段文本会将页面定向到用户控制器,而在@index之后的一段文本会将脚本定向到user函数public function get_index().

The piece of text before the @, user in this case, will direct the page to the user controller and the piece of text after the @, index, will direct the script to the user function public function get_index().

我看到您正在使用$restful,在这种情况下,您可以将Route设置为Route::any('user', 'user@index').这将同时处理POSTGET,而不是将它们分别写出.

I see you're using $restful, in which case you could set your Route to Route::any('user', 'user@index'). This will handle both POST and GET, instead of writing them both out separately.

这篇关于我的路线返回了404,我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆