首页 文章

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

提问于
浏览
61

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

唯一有效的路线是开箱即用的Laravel附加的默认本地路由 .

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

我创建了一个名为 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中我添加了以下内容:

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

16 回答

  • 5

    您是否尝试将此添加到路径文件而不是 Route::get('user', "user@index")

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

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

  • 3

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

    • 在apache服务器上启用mod_rewrite: sudo a2enmod rewrite .

    • 编辑 /etc/apache2/apache2.conf ,更改/ var / www目录的"AllowOverride"指令(这是我的主文档根目录): AllowOverride All

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

  • 28

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

    注意,一旦启用“rewrite_module”,服务器应用程序就会自动重启

  • 6

    你有没有试过检查一下

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

    工作?如果是这样,那么请确保所有路径的文件夹没有任何大写字母 . 我有同样的情况,并将信件转换为小写有帮助 .

  • 0

    我使用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>
    
  • 0

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

  • 0

    Routes

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

    Controllers

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

    Solution to your problem

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

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

    Route::controller('user');
    

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

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

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

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

    例如 :

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

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

    Laravel在这里尝试做的是将文件 index.php 插入路径前面的路径前面 . 例如,如果您在浏览器中指定 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 查看请求(您的路径可能会有所不同,取决于您的laravel应用程序实际安装的位置,但尾随 public 是替换需要发生的位置)必须应用'RewriteRule' .

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

    没有意义的部分:只是 stoppingrestarting Apache服务不会接受WAMP使用RewriteRule做正确的事情所需的更改 - 我知道,我尝试了很多次!

    做什么工作:制作muvera建议的更改(请参阅主题顶部)以加载正确的模块 . 然后,重置整个Windows会话,从而完全将Apache转储出内存 . 重启(重新加载)WAMP和VOILA!修复工作,应用正确的RewriteRule,yada,yada;我从此过上幸福的生活 .

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

  • 6

    尝试在php.ini中启用短php标签 . WAMP经常将它们关闭,并且laravel需要它们 .

  • 63
    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');
    });
    

    您必须在路线的末尾使用'/'(home / default)

  • 0

    你必须使用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命令更改,请参阅我之前遇到相同错误的文档

  • 76

    只需在您的终端中运行 .

    composer dump-autoload
    
  • 0

    路由无法正常工作的主要问题是macos中有mod_rewrite.so模块,在apache配置的httpd.conf文件中没有启用linux,因此.htaccess可以工作 . 我通过取消注释来解决这个问题:

    LoadModule rewrite_module libexec / apache2 / mod_rewrite.so

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

  • 0

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

  • 21

    简单的命令自动加载依赖项

    composer dump-autoload
    

    仍然得到你的一些重要文件丢失所以去这里看整个程序

    https://codingexpertise.blogspot.com/2018/11/laravel-new.html

相关问题