2017-01-26 43 views
16

我有一個TWIG問題。此代碼在學校工作,但絕對不是我的筆記本電腦。我試圖用一個簡單的代碼,但我有錯誤:傳遞給Twig_Filter :: __ construct()的參數1必須是字符串的一個實例,字符串給出

Catchable fatal error: Argument 1 passed to Twig_Filter::__construct() must be an instance of string, string given, called in /opt/lampp/htdocs/webalizer/projetSilex/vendor/twig/twig/lib/Twig/Extension/Core.php on line 139 and defined in /opt/lampp/htdocs/webalizer/projetSilex/vendor/twig/twig/lib/Twig/Filter.php on line 35

我使用PHP 5.6/2.0 SILEX /枝條2.0

感謝您的幫助。

我的代碼是非常簡單的,但是不工作:

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

$app = new Silex\Application(); 
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views', 
)); 

    $app->get('/', function(){ 
return "hello"; 
    }); 

$app->get('/hello/{name}', function($name)use($app){ 

return $app['twig']->render('hello.twig', 
          array("name"=>$name 
           )); 

}); 

$app->run();> 
+2

不能鍵入聲明標量類型在PHP5中。 https://secure.php.net/manual/en/functions.arguments.php#functions.arguments.type-claclaration –

+1

你必須傳遞一個字符串,而不是一個字符串,這很清楚;-)(作曲家應該警告你安裝Twig 2.0與Php 5.6沒有?) –

+0

可能重複[錯誤時將字符串傳入類型提示方法](https://stackoverflow.com/questions/3112791/error-when-passing-string-into-method-與型提示) – Axel

回答

26

@CharlotteDunois指出了這一點,但枝杈2.0要求> = PHP7.0,所以在您的環境(PHP 5.6),你可以不使用Twig 2.0。從Twig official documentation

Prerequisites

Twig needs at least PHP 7.0.0 to run.

注意,對於PHP5.x分支你仍然有枝杈的1.x aviable

0

只是我的2美分。我無法升級PHP版本(共享主機),所以我不得不在我的package.json中更改Twig版本以使用1.x版本。

替換此行: "symfony/twig-bridge": "~2.8|^3.0", - >"symfony/twig-bridge": "~2.8|3.0.*",

然後,我加入這一行:

"twig/twig" : "~1.0" 

和evething工作正常的PHP v服務器5.6

相關問題