2016-09-19 14 views
3

我有這樣的錯誤(在硅石2.0​​):UndefinedFunctionException試圖調用從名稱空間函數XXX XXX

UndefinedFunctionException在app.php線88:嘗試從名稱空間 「SocialWall \控制器」 調用函數 「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())); 
    }; 
} 

我需要幫助!

回答

0

我試圖重新創建你的演示,它適用於我。我看到你在這裏如下所示http://silex.sensiolabs.org/doc/master/providers/service_controller.html是提及使用可以調用的控制器的最後一個例子:

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

+0

現在好了:我剛剛添加了「require_once __DIR __。」/ ../src/Controller/HomeController.php';「在我的index.php!並刪除」新「作爲你寫的控制器的回報 –

+0

這就是爲什麼使用這種方法感到腥我:(:(需要require_once意味着返回以前自動加載很好請使用它 – seblucas

+0

你說可以調用控制器是一個偉大的方法,如果使用惠特Autoload? –

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())); 
    } 
} 

作爲sid e注意不建議直接在任何地方使用$ app。你應該閱讀關於DI。

+0

好,不過如果沒有你看文檔頁面的末尾(http://silex.sensiolabs.org/doc/master/providers/service_controller.html),你會看到「除了使用服務控制器的類...」「而且就在下面,他們的控制器是一個簡單的功能,爲什麼?我想我不明白這部分文檔,你能解釋我嗎? –

+0

我剛讀過它,但我從未想過使用一個簡單的函數,對我來說服務是一個類,就是這樣,我甚至不想知道如何通過一個簡單的函數來獲取請求或路由參數。對我而言太複雜了。 – seblucas

相關問題