沃梦达 / 编程问答 / php问题 / 正文

我的路由返回 404,我该如何解决?

My Routes are Returning a 404, How can I Fix Them?(我的路由返回 404,我该如何解决?)

本文介绍了我的路由返回 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 代码,并在 routes.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 会将页面定向到用户控制器,而 @<之后的一段文本/code>, 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@索引').这将同时处理 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,我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:我的路由返回 404,我该如何解决?

基础教程推荐