草庐IT

php - Laravel - call_user_func_array() 期望参数 1 是一个有效的回调

coder 2024-04-05 原文

我收到这个错误:

call_user_func_array() expects parameter 1 to be a valid callback, class 'Symfony\Component\HttpFoundation\LaravelRequest' does not have a method 'url'

我使用的代码是:

路由.php:

<?php

Route::post('', 'app@check');
Route::get('', 'app@check');
Route::post('game', 'app@game');
Route::get('game', 'app@game');
Route::get('terminos', 'app@terminos');
Route::post('save', 'scores@savescore');
Route::get('scores', 'scores@scorelist');
Route::get('scores2468', 'scores@showscores');



Event::listen('404', function()
{
    //return Response::error('404');
    echo "Error 404: \n";
    echo Request::url();
});

Event::listen('500', function()
{
    return Response::error('500');
});



Route::filter('before', function()
{
    // Do stuff before every request to your application...
});

Route::filter('after', function($response)
{
    // Do stuff after every request to your application...
});

Route::filter('csrf', function()
{
    if (Request::forged()) return Response::error('500');
});

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::to('login');
});

分数.php:

class Scores_Controller extends Base_Controller {

    public $restful = true;    

    public function get_showscores()
    {
        // Imprimo pantalla con tabla de resultados
        $regs = array('regs' => Score::order_by('score','desc')->get());
        //dd($regs);
        return View::make('score.show', $regs);
    }    

    public function post_savescore()
    {
        // Tomo FacebookSDK instance
        $facebook = IoC::resolve('facebook-sdk');
        $accessToken = $facebook->getAccessToken();
        $user = $facebook->getUser();
        $user_profile = $facebook->api('/me');

        if ($user) {
            // Logueado
            $data = Input::all();
            // Guardo un nuevo score
            $score = Score::create(array(
                'nombre' => $user_profile['name'],
                'score' => $data['score'],
                'fbid' => $user_profile['id']
                ));

            return json_encode($score->attributes);
        }else{
            // No hay sesion
            return false;
        }

    }    

    public function get_scorelist(){

        $facebook = IoC::resolve('facebook-sdk');

        $scores = Score::order_by('score','desc')->take(10)->get();

        $response = array();
        foreach($scores as $sc){
            $ua = $facebook->api('http://www.facebook.com/'.$sc->attributes['fbid']);
            $dat = array(
                //'name' => $user->name,
                'nombre' => $sc->attributes['nombre'],
                'uid' => $sc->attributes['fbid'],
                'score' => $sc->attributes['score']
                );
            array_push($response, $dat);
        }

        return json_encode($response);
    }
}

正如您可能已经注意到的那样,这是 Facebook 应用程序的一部分。问题是,当我尝试调用“保存”路由时,我收到了之前发布的错误。

完整的错误描述,包括。回溯:

Unhandled Exception

Message:

call_user_func_array() expects parameter 1 to be a valid callback, class 'Symfony\Component\HttpFoundation\LaravelRequest' does not have a method 'url'

Location:

/www/conamor/htdocs/apps/aventuracenter/pacman/laravel/request.php on line 287

Stack Trace:

#0 /www/conamor/htdocs/apps/aventuracenter/pacman/laravel/laravel.php(42): Laravel\Error::native(2, 'call_user_func_...', '/www/conamor/ht...', 287)
#1 [internal function]: Laravel\{closure}(2, 'call_user_func_...', '/www/conamor/ht...', 287, Array)
#2 /www/conamor/htdocs/apps/aventuracenter/pacman/laravel/request.php(287): call_user_func_array(Array, Array)
#3 /www/conamor/htdocs/apps/aventuracenter/pacman/application/routes.php(63): Laravel\Request::__callStatic('url', Array)
#4 /www/conamor/htdocs/apps/aventuracenter/pacman/application/routes.php(63): Laravel\Request::url()
#5 [internal function]: {closure}()
#6 /www/conamor/htdocs/apps/aventuracenter/pacman/laravel/event.php(199): call_user_func_array(Object(Closure), Array)
#7 /www/conamor/htdocs/apps/aventuracenter/pacman/laravel/event.php(124): Laravel\Event::fire('404', Array)
#8 /www/conamor/htdocs/apps/aventuracenter/pacman/laravel/laravel.php(109): Laravel\Event::first('404')
#9 [internal function]: Laravel\{closure}('save')
#10 /www/conamor/htdocs/apps/aventuracenter/pacman/laravel/routing/route.php(163): call_user_func_array(Object(Closure), Array)
#11 /www/conamor/htdocs/apps/aventuracenter/pacman/laravel/routing/route.php(124): Laravel\Routing\Route->response()
#12 /www/conamor/htdocs/apps/aventuracenter/pacman/laravel/laravel.php(167): Laravel\Routing\Route->call()
#13 /www/conamor/htdocs/apps/aventuracenter/pacman/public/index.php(34): require('/www/conamor/ht...')
#14 {main}

有什么想法吗? 提前致谢!

最佳答案

根据您的堆栈跟踪,Laravel 触发了 404 事件。这意味着,您的事件处理程序会抛出错误。 此外,call_user_func' 提示您的处理程序中缺少函数url()`。所以在我看来,这个电话

echo Request::url();

是万恶之源。将其删除并检查是否仍然出现错误。

关于php - Laravel - call_user_func_array() 期望参数 1 是一个有效的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17981075/

有关php - Laravel - call_user_func_array() 期望参数 1 是一个有效的回调的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby - 在 Ruby 中实现 `call_user_func_array` - 2

    我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)

  3. Ruby Koans about_array_assignment - 非平行与平行分配歧视 - 2

    通过ruby​​koans.com,我在about_array_assignment.rb中遇到了这两段代码你怎么知道第一个是非并行赋值,第二个是一个变量的并行赋值?在我看来,除了命名差异之外,代码几乎完全相同。4deftest_non_parallel_assignment5names=["John","Smith"]6assert_equal["John","Smith"],names7end45deftest_parallel_assignment_with_one_variable46first_name,=["John","Smith"]47assert_equal'John

  4. ruby - 如何进行排列以有效地定制输出 - 2

    这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][

  5. arrays - 这是 Ruby 中 Array.fill 方法的错误吗? - 2

    这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]

  6. ruby - 怎么来的(a_method || :other) returns :other only when assigning to a var called a_method? - 2

    给定以下方法:defsome_method:valueend以下语句按我的预期工作:some_method||:other#=>:valuex=some_method||:other#=>:value但是下面语句的行为让我感到困惑:some_method=some_method||:other#=>:other它按预期创建了一个名为some_method的局部变量,随后对some_method的调用返回该局部变量的值。但为什么它分配:other而不是:value呢?我知道这可能不是一件明智的事情,并且可以看出它可能有多么模棱两可,但我认为应该在考虑作业之前评估作业的右侧...我已经在R

  7. python - 是否可以使用 Ruby 或 Python 禁用 anchor /引用来发出有效的 YAML? - 2

    是否可以在PyYAML或Ruby的Psych引擎中禁用创建anchor和引用(并有效地显式列出冗余数据)?也许我在网上搜索时遗漏了一些东西,但在Psych中似乎没有太多可用的选项,而且我也无法确定PyYAML是否允许这样做.基本原理是我必须序列化一些数据并将其以可读的形式传递给一个不是真正的技术同事进行手动验证。有些数据是多余的,但我需要以最明确的方式列出它们以提高可读性(anchor和引用是提高效率的好概念,但不是人类可读性)。Ruby和Python是我选择的工具,但如果有其他一些相当简单的方法来“展开”YAML文档,它可能就可以了。 最佳答案

  8. ruby - Arrays Sets 和 SortedSets 在 Ruby 中是如何实现的 - 2

    通常,数组被实现为内存块,集合被实现为HashMap,有序集合被实现为跳跃列表。在Ruby中也是如此吗?我正在尝试从性能和内存占用方面评估Ruby中不同容器的使用情况 最佳答案 数组是Ruby核心库的一部分。每个Ruby实现都有自己的数组实现。Ruby语言规范只规定了Ruby数组的行为,并没有规定任何特定的实现策略。它甚至没有指定任何会强制或至少建议特定实现策略的性能约束。然而,大多数Rubyist对数组的性能特征有一些期望,这会迫使不符合它们的实现变得默默无闻,因为实际上没有人会使用它:插入、前置或追加以及删除元素的最坏情况步骤复

  9. ruby-on-rails - 在 RSpec 中,如何以任意顺序期望具有不同参数的多条消息? - 2

    RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)

  10. ruby-on-rails - 禁用设备的 :confirmable on-the-fly to batch-generate users - 2

    Devise是一个Ruby库,它为我提供了这个User类:classUser当写入:confirmable时,注册时会发送一封确认邮件。上周我不得不批量创建300个用户,所以我在恢复之前注释掉了:confirmable几分钟。现在我正在为用户批量创建创建一个UI,因此我需要即时添加/删除:confirmable。(我也可以直接修改Devise的源码,但我宁愿不去调和它)问题:如何即时添加/删除:confirmable? 最佳答案 WayneConrad的解决方案:user=User.newuser.skip_confirmation

随机推荐