Laravel路线不工作?

我刚刚开始学习Laravel框架,并且遇到了路由问题。

唯一正在工作的路线是Laravel开箱即用的默认主路线。

我在Windows上使用WAMP,它使用PHP 5.4.3和Apache 2.2.22,并且我也启用了mod_rewrite,并从application.phpconfiguration文件中删除了'index.php',留下一个空string。

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

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

我已经创build了一个视图文件在应用程序/视图/用户/所谓的index.php与一些基本的HTML代码,并在routes.php我已经添加了以下内容:

 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错误。 为什么会这样呢?

你有没有试过把这个添加到你的路线文件,而不是Route::get('user', "user@index")

在这种情况下, user @之前的public function get_index()文本将把页面引导到用户控制器, @index之后的文本将引导脚本到user函数public function get_index()

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

使用WAMP点击wamp图标 – > apache-> apache modules->滚动并查看rewrite_module重新启动一个LoadModule rewrite_module

请注意,一旦启用“rewrite_module”,服务器应用程序将自动重新启动

在我的Ubuntu LAMP安装,我解决了这个问题,以下2个变化。

  1. 在Apache服务器上启用mod_rewrite: sudo a2enmod rewrite
  2. 编辑/etc/apache2/apache2.conf ,更改/ var / www目录(这是我的主文档根目录)的“AllowOverride”指令: AllowOverride All

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

你有没有试图检查是否

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

在工作? 如果是这样,请确保所有path的文件夹都没有任何大写字母。 我遇到了同样的情况,把信件转换成小写字母。

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

我最终看起来像这样

 <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> 

路线

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

控制器

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

解决您的问题

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

不要为您的控制器操作创build路由,只需注册您的控制器:

 Route::controller('user'); 

现在你的控制器已经注册了,你可以导航到http://localhost/mysite/public/user ,你的get_index将会运行。

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

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

您可以尝试将root/public/.htaccess移动到root/.htaccess ,它应该可以工作

不要忘了public/.htaccess的“ RewriteBase ”:

例如 :

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

好吧,在这个问题上,我花了一天的时间猛冲了一下头……我起床了,做了我昨天应该做的事情,并且debugging了一下发生了什么事情!

Laravel正在尝试在这里做什么,就是将文件index.php插入作为Route的path前面。 举例来说,如果你指定了一个Route::get('/account/create', ...,并在你的浏览器上执行localhost/laravel/authenticate/public/account/create来执行你的程序,那么laravel就要执行localhost/authenticate/public/index.php/account/create ,但要做到这一点…. Apache需要通过/wamp/www/laravel/laravel/authentication/public来查看请求(你的path可能有所不同,具体取决于在哪里你的laravel应用程序实际上安装,但尾随public是替代需要发生的地方)必须有一个“重写规则”适用。

谢天谢地,laravel在正确的.htaccess文件中提供正确的Rewrite规则,就在你的应用的public文件夹中。 问题是,“.htaccess”文件中的代码不能用WAMPconfiguration的方式工作。 这个SEEMS的原因是muvera在这个线程的顶部提出的问题 – rewrite_module代码需要在RewriteRule东西工作之前由Apache加载。 这是有道理的。

那些没有意义的部分:简单地stoppingrestarting Apache服务将不会拿起WAMP为您的RewriteRule做正确的事情所必须的改变 – 我知道,我尝试了很多次!

什么工作:进行muverabuild议的更改(请参阅线程顶部)以加载正确的模块。 然后,重新设置整个Windows会话,从而将Apache从内存中彻底删除。 重新启动(重新加载)WAMP和VOILA! 修复工程,正确的RewriteRule应用,亚达,亚达; 从此以后,我幸福地生活着。

好消息是:我现在知道更多关于.htaccessRewriteRulehttpd.conf文件。 将逻辑从应用程序的public .htaccess文件中移出,并将其放入您的Apache“bin”文件夹中的httpd.conf的Directory ...部分(特别是如果您有权访问该文件夹)。

尝试在你的php.ini中启用短php标签。 WAMP通常会closures它们,并且laravel需要它们。

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

改变以上

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

您必须在路线最后使用“/”(主/默认)

你必须使用Laravel 5的命令

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

并在routes.php

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

Laravel 5命令更改视图和控制器请参阅我以前有同样的错误的文档

只需在你的terminal中运行。

  composer dump-autoload 

路由不能工作的主要问题是在macos中有mod_rewrite.so模块,在configuration的httpd.conf文件中没有启用linux,所以可以使用.htaccess工作。 我通过取消注释来解决这个问题:

LoadModule rewrite_module libexec / apache2 / mod_rewrite.so

从上面的行httpdf.conf中删除#。 然后它会起作用。 请享用!

我想你已经删除了laravel公用文件夹中的默认.htaccess文件。 上传文件,它应该解决您的问题。