UndefinedFunctionException尝试从命名空间xxx调用函数xxx

时间:2016-09-19 13:16:59

标签: php symfony silex

我有这个错误(在silex 2.0下):

  

app.php第88行中的UndefinedFunctionException:尝试从命名空间“SocialWall \ Controller”调用函数“postIndexArticle”。

in app.php line 88
at {closure}(object(Application)) in Container.php line 113
at Container->offsetGet('home.controller') in CallbackResolver.php line 55
at CallbackResolver->convertCallback('home.controller') in ServiceControllerResolver.php line 50
at ServiceControllerResolver->getController(object(Request)) in HttpKernel.php line 136
at HttpKernel->handleRaw(object(Request), '1') in HttpKernel.php line 68
at HttpKernel->handle(object(Request), '1', true) in Application.php line 496
at Application->handle(object(Request)) in Application.php line 477
at Application->run() in index.php line 17

我的app.php

<?php

use SocialWall\Controller;

$app['home.controller'] = function($app) {
    return SocialWall\Controller\postIndexArticle($app);
};

我的route.php

<?php

// Home page
$app->get('/', 'home.controller')->bind('home');

HomeController.php

<?php

namespace SocialWall\Controller;

use Silex\Application;
use SocialWall\DAO\ArticleDAO;

function postIndexArticle(Application $app) {
    return function() use ($app) {
        return new $app['twig']->render('index.html.twig', array('articles' => $app['dao.article']->findAll()));
    };
}

我需要帮助!

2 个答案:

答案 0 :(得分:0)

你应该使用类而不是函数,我会使用

我的app.php

<?php

use SocialWall\Controller;

$app['home.controller'] = function($app) {
    return SocialWall\Controller\HomeController($app);
};

我的route.php

<?php

// Home page
$app->get('/', 'home.controller:postIndexArticle')->bind('home');

Homecontroller.php

<?php

namespace SocialWall\Controller;

use Silex\Application;
use SocialWall\DAO\ArticleDAO;

class HomeController
{
  protected $app;

  public function __construct(Application $app)
  {
    $this->app= $app;
  }

  function postIndexArticle() {
        return $this->app['twig']->render('index.html.twig', array('articles' => $this->app['dao.article']->findAll()));
  }
}

不建议直接在任何地方使用$ app。你应该读一下DI。

答案 1 :(得分:0)

我尝试重新创建你的演示,它对我有用。我看到你按照这里显示的最后一个例子http://silex.sensiolabs.org/doc/master/providers/service_controller.html提到使用callables作为控制器:

// app.php
<?php

require_once __DIR__.'/vendor/autoload.php';
require_once 'HomeController.php';

$app = new Silex\Application();
$app['debug'] = true;

$app->register(new Silex\Provider\ServiceControllerServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider());

$app['home.controller'] = function($app) {
    return \SocialWall\Controller\postIndexArticle($app);
};

// Home page
$app->get('/', 'home.controller')->bind('home');

$app->run();

然后控制器位于一个单独的文件中:

// HomeController.php
namespace SocialWall\Controller;

use Silex\Application;

function postIndexArticle(Application $app) {
    return function() use ($app) {
        return $app['twig']->createTemplate('test template')->render([]);
    };
}

仅打印test template

所以我认为问题出在其中一个方面(或者两个方面):

  1. 您的postIndexArticle内部可调用返回return new $app['twig']->render(...这是不正确的。您不想在此处使用关键字new,因为render()方法只返回字符串模板。

  2. 我认为您要调用的app.phpHomeController.php中的命名空间有问题

    $app['home.controller'] = function($app) {
        return SocialWall\Controller\postIndexArticle($app);
    };
    

    因此,尝试使用绝对命名空间路径,就像我使用\SocialWall\Controller\postInd...

  3. 一样