【问题标题】:1/1 NotFoundHttpException in Controller.php line 268: Controller method not found. in laravel 5Controller.php 第 268 行中的 1/1 NotFoundHttpException:找不到控制器方法。在 laravel 5
【发布时间】:2016-04-02 01:28:26
【问题描述】:

我正在尝试使用 jquery ajax 将一些数据发布到 laravel 控制器,但它显示错误

"Controller.php 第 268 行中的 1/1 NotFoundHttpException:控制器 找不到方法。”

在控制台中抛出

“网络错误:404 未找到”

下面是我的代码:

在 routes.php 中

Route::controller('contact', 'ContactController');
Route::post('searchDependency','ContactController@postSearchDependency');

ContactController.php

public function postSearchDependency(){

    //dd('hello');
}

ajax 到 index.blade.php

$('#deleteRecord').click(function(){

    var total = $('input[class="ids"]:checkbox:checked').length;
    var ContactId=[];
    $('input[class="ids"]:checked').each(function() {
       ContactId.push($(this).val());
    });
    //alert(ContactId);
    $.ajax({
        type: 'POST',
        data: 'contactId: ContactId',
        url: '<?php echo URL::to('contact/searchDependency')?>',         
            success: function(contacts){                
        }          
    }); 

});

我已经通过这种方式成功地使用 ajax 将数据发布到另一个控制器的不同方法,我已经尝试了可能的解决方案,但不知道在这种情况下会发生什么,请任何人帮助我。

【问题讨论】:

  • 没有一个解决方案适合我!这个问题看起来很奇怪!!我刚刚对我的代码进行了编辑。
  • 你必须使用contact/search-dependency而不是contact/searchDependency
  • @user-4653387 检查更新
  • @shock_gone_wild 你的解决方案对我有用!!真漂亮!谢谢!

标签: php ajax laravel laravel-5 laravel-routing


【解决方案1】:

假设 routes.php 文件包含以下行:

Route::controller('contact', 'ContactController');

并且您的 ContactController 定义了以下函数:

public function postSearchDependency() {
    dd('hello');
}

然后这个函数会被一个post请求调用到contact/search-dependency而不是contact/searchDependency

这是由于在使用 Route::controller 时将 CamelCase 函数名称转换为更友好和可读的 URL。

如果您另外定义了到您的控制器方法的直接路由,例如:

Route::post('searchDependency','ContactController@postSearchDependency');

这个路由被 URL 中的 searchDependency 直接调用(之前没有联系)。

您可以随时通过键入来检查您现有的路线和网址

php artisan route:list

在您的终端中。

【讨论】:

    【解决方案2】:

    你不应该使用静态方法,改变:

    public static function postSearchDependency(){
    
        //dd('hello');
    }
    

    进入

    public function postSearchDependency()
    {
        //dd('hello');
    }
    

    另外,在你的 AJAX 文件中你使用了奇怪的:

    '{{"contact/searchDependency"}}'
    

    应该是这样的:

    'http://domain.com/contact/searchDependency'
    

    你也应该改变:

    Route::post('searchDependency','ContactController@postSearchDependency');
    

    进入

    Route::post('contact/searchDependency','ContactController@postSearchDependency');
    

    在这种情况下。

    【讨论】:

      【解决方案3】:

      发生此类错误的原因可能有很多。 首先 - 您的路由是否在路由器中命名?如果没有,则可以使用 url('searchDependency'),而不是 url('contact/searchDependency') - 检查路线。

      在您的 ajax 请求中排名第二

       url: '{{"contact/searchDependency"}}',         
      

      这将返回 url: 'contact/searchDependency' ,这是一个相对路径,而不是绝对路径。对于绝对,你需要使用像

      这样的 url 生成器
      url: '{{url("contact/searchDependency")}}',         
      

      【讨论】:

        【解决方案4】:

        您正在使用static function 而不是实例函数:

        因此,您的调度程序没有找到 postSearchDependency 方法,因为您将其定义为静态函数。

        解决方法:

        public function postSearchDependency(){
        
            //dd('hello');
        }
        

        除了在您的 index.blade.php 中,您还使用刀片标签:{{"contact/searchDependency"}}

        将它们修改为:

        url : 'contact/searchDependency' 并修改您的路线如下:

        Route::post('contact/searchDependency','ContactController@postSearchDependency');

        更新 从控制器中删除 Route::controller('contact', 'ContactController');。我能够重现您的错误:

        Controller.php 第 259 行中的 NotFoundHttpException:控制器方法 没找到。

        in Controller.php line 259
        at Controller->missingMethod('searchDependency')
        at call_user_func_array(array(object(ContactController), 'missingMethod'), array('_missing' => 'searchDependency')) in
        

        Controller.php 第 246 行

        在注释掉Route::controller('contact', 'ContactController'); 并修改您的ajax 中的routes.phpurl 部分后,我指定它对我有用。

        参考: Routing , Controller Dispatcher, Router

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-01-09
          • 1970-01-01
          • 2017-12-26
          • 2015-12-17
          • 2015-10-17
          • 1970-01-01
          • 1970-01-01
          • 2016-02-16
          相关资源
          最近更新 更多