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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11791375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:06:50  来源:igfitidea点击:

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

phplaravelroutinglaravel-routinglaravel-3

提问by JasonMortonNZ

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.

唯一有效的路由是默认的 home 路由,它开箱即用地附加到 Laravel。

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.

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

I've created a new controller called User:

我创建了一个名为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.phpwith some basic HTML code, and in routes.php I've added the following:

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

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/userI get a 404 Not Found error. Why would this be happening?

http://localhost/mysite/public在 Web 浏览器中访问根 ( )时,第一条路线工作正常,但是当我尝试转到第二条路线时,http://localhost/mysite/public/user出现 404 Not Found 错误。为什么会发生这种情况?

采纳答案by PapaSmurf

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

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

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

之前的一段文字@user在这种情况下,直接将网页的用户控制器和之后的一段文字@index将直接脚本的user功能public function get_index()

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

我看你使用$restful,在这种情况下,你可以设置你RouteRoute::any('user', 'user@index')。这将同时处理POSTand GET,而不是分别写出它们。

回答by Andrew Vickers

On my Ubuntu LAMP installation, I solved this problem with the following 2 changes.

在我的 Ubuntu LAMP 安装中,我通过以下 2 个更改解决了这个问题。

  1. Enable mod_rewrite on the apache server: sudo a2enmod rewrite.
  2. Edit /etc/apache2/apache2.conf, changing the "AllowOverride" directive for the /var/www directory (which is my main document root): AllowOverride All
  1. 启用Apache服务器的mod_rewrite: sudo a2enmod rewrite
  2. 编辑/etc/apache2/apache2.conf,更改 /var/www 目录(这是我的主文档根目录)的“AllowOverride”指令:AllowOverride All

Then restart the Apache server: service apache2 restart

然后重启Apache服务器: service apache2 restart

回答by Mario Uvera

Using WAMP click on wamp icon ->apache->apache modules->scroll and check rewrite_module Restart a LoadModule rewrite_module

使用 WAMP 单击 wamp 图标 ->apache->apache modules->scroll 并检查 rewrite_module 重新启动 LoadModule rewrite_module

Note, the server application restarts automatically for you once you enable "rewrite_module"

请注意,启用“rewrite_module”后,服务器应用程序会自动为您重新启动

回答by user1930566

Have you tried to check if

你有没有试过检查

http://localhost/mysite/public/index.php/user 

was working? If so then make sure all your path's folders don't have any uppercase letters. I had the same situation and converting letters to lower case helped.

在工作吗?如果是这样,请确保所有路径的文件夹都没有任何大写字母。我遇到了同样的情况,将字母转换为小写有帮助。

回答by Simon East

I was getting the same problem using EasyPHP. Found that I had to specify AllowOverride Allin my <Directory>block in httpd.conf. Without this, Apache sometimes ignores your .htaccess.

我在使用 EasyPHP 时遇到了同样的问题。发现我必须AllowOverride All在我的<Directory>块中指定httpd.conf. 没有这个,Apache 有时会忽略您的.htaccess.

Mine ended up looking like this...

我的最终看起来像这样......

<Directory "D:/Dev">
    Options FollowSymLinks Indexes
    #### NEXT IS THE CRUCIAL LINE ####
    AllowOverride All                  
    Order deny,allow
    Allow from 127.0.0.1
    Deny from all
    Require all granted     
</Directory>

回答by Wasim A.

You could try to move root/public/.htaccessto root/.htaccessand it should work

你可以尝试移动root/public/.htaccessroot/.htaccess它应该工作

回答by David Barker

Routes

路线

Use them to define specific routes that aren't managed by controllers.

使用它们来定义不受控制器管理的特定路由。

Controllers

控制器

Use them when you want to use traditional MVC architecture

当您想使用传统 MVC 架构时使用它们

Solution to your problem

解决您的问题

You don't register controllers as routes unless you want a specific 'named' route for a controller action.

除非您想要控制器操作的特定“命名”路由,否则您不会将控制器注册为路由。

Rather than create a route for your controllers actions, just register your controller:

无需为您的控制器操作创建路由,只需注册您的控制器:

Route::controller('user');

Now your controller is registered, you can navigate to http://localhost/mysite/public/userand your get_indexwill be run.

现在您的控制器已注册,您可以导航到http://localhost/mysite/public/userget_index运行您的控制器。

You can also register all controllers in one go:

您还可以一次性注册所有控制器:

Route::controller(Controller::detect());

回答by Mathieu Perino

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

不要忘记RewriteBase您的public/.htaccess:

For example :

例如 :

Options +FollowSymLinks
RewriteEngine On
RewriteBase /your/folder/public

回答by mdg

OK, so after bashing my head on this problem for a little over a day... I got up and did what I SHOULD have done yesterday, and DEBUGGED what was going on!

好的,所以在这个问题上猛烈抨击了一天多一点之后......我站起来做了我昨天应该做的事情,并调试了发生了什么!

What Laravel is TRYING to do here, is insert the file index.phpright in front of the path given as a Route. SO for instance, if you specified a Route::get('/account/create', ...,and execute your app from say localhost/laravel/authenticate/public/account/createon your browser, then laravel wants to execute localhost/authenticate/public/index.php/account/create, but to do that.... Apache needs to see that requests through /wamp/www/laravel/laravel/authentication/public(your path may vary somewhat, depending on where your laravel app is actually installed, but the trailing publicis where the substitution needs to take place) must have a 'RewriteRule' applied.

Laravel 在这里尝试做的是将文件插入到index.php作为 Route 给出的路径前面。例如,如果您在浏览器上指定 aRoute::get('/account/create', ...,并从 say 执行您的应用程序localhost/laravel/authenticate/public/account/create,那么 laravel 想要执行localhost/authenticate/public/index.php/account/create,但要做到这一点.... Apache 需要查看该请求/wamp/www/laravel/laravel/authentication/public(您的路径可能有所不同,具体取决于您的laravel 应用程序实际上已安装,但尾随public是需要进行替换的地方)必须应用“重写规则”。

Thankfully, laravel supplies the correct Rewrite rule in a handy .htaccessfile right there in your app's publicfolder. The PROBLEM is, the code in that '.htaccess' file won't work with the way WAMP is configured out of the box. The reason for this SEEMS to be the problem suggested by muvera at the top of this thread -- the rewrite_module code needs to be loaded by Apache before the RewriteRulestuff will work. Heck this makes sense.

幸运的是,laravel.htaccess在你应用程序public文件夹中的一个方便的文件中提供了正确的重写规则。问题是,'.htaccess' 文件中的代码不能与 WAMP 开箱即用的配置方式一起使用。这个 SEEMS 是这个线程顶部的 muvera 建议的问题的原因 - rewrite_module 代码需要由 Apache 加载,然后RewriteRule才能工作。哎呀这是有道理的。

The part that DOESN'T make sense: simply stoppingand restartingApache services will not pick up the changes necessary for WAMP to do the right thing with your RewriteRule -- I know, I tried this many times!

没有意义的部分:简单地说stoppingrestartingApache 服务不会选择 WAMP 使用您的 RewriteRule 做正确事情所需的更改——我知道,我试过很多次了!

What DOES work: make the changes suggested by muvera (see top of thread) to load the correct modules. Then, reset your whole Windows session, thus dumping Apache out of memory altogether. Restart (reload) WAMP, and VOILA! the fix works, the correct RewriteRule is applied, yada, yada; I'm living happily ever after.

什么工作:进行 muvera 建议的更改(请参阅线程顶部)以加载正确的模块。然后,重置整个 Windows 会话,从而完全将 Apache 倾倒出内存。重新启动(重新加载)WAMP,瞧!修复有效,应用了正确的 RewriteRule,yada,yada;从此我过上了幸福的生活。

The good news out of all this: I know a LOT more about .htaccess, RewriteRule, and httpd.conffiles now. There is a good (performance) argument for moving the logic from your app's public.htaccessfile, and putting it into a Directory ...section of your httpd.conf in your Apache 'bin' folder BTW (especially if you have access to that folder).

我知道了很多有关:好消息了这一切.htaccessRewriteRulehttpd.conf现在的文件。有一个很好的(性能)参数可以将逻辑从您的应用程序public.htaccess文件中移出,并将其放入Directory ...Apache 'bin' 文件夹 BTW 中的 httpd.conf 部分(特别是如果您有权访问该文件夹)。

回答by Rohan Kalra

you must be using Laravel 5 the command

你必须使用 Laravel 5 命令

  class User_Controller extends Controller {
  public $restful = true;
  public function get_index(){
  return View('user.index');
  }
  }

and in routes.php

并在routes.php

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

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

Laravel 5 command changes for view and controller see the documentation i was having same error before

视图和控制器的 Laravel 5 命令更改请参阅我之前遇到相同错误的文档