问题描述
我刚开始学习 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');
});
在我的网络浏览器中访问根目录 (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@索引')代码>.这将同时处理
POST
和 GET
,而不是分别写出它们.
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,我该如何修复它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!