我的路线返回 404,我该如何修复它们?

2023-10-31php开发问题
0

本文介绍了我的路线返回 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');
});

在我的网络浏览器中访问根目录 (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,我该如何修复它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17