2

我正在尝试让 Smarty 3 作为 Symfony 2 的模板引擎。我正在尝试安装此捆绑包以使 smarty 3 工作:

https://github.com/noiselabs/SmartyBundle

它安装得很好,但是当我按照安装说明中的说明将它添加到 AppKernal 时,出现以下错误:

致命错误:在第 20 行的 >/home/kevin/workspace/Symfony/app/AppKernel.php 中找不到类“NoiseLabs\Bundle\SmartyBundle”

其中第 20 行在 registerBundles() 内:new NoiseLabs\Bundle\SmartyBundle(),

我遇到的第二个可能相关的问题是,在 app/config/config.yml 中,如果我将“smarty”添加到模板引擎数组中:

templating:      { engines: ['twig'] }

它抛出这个错误:

ServiceNotFoundException:服务“模板”依赖于不存在的>服务“模板.engine.smarty”。

我意识到 twig 带有 symfony,但对于这个项目,我需要使用 smarty。我错过了什么还是有其他解决方案?

这是内核代码:

<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {   
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
//new NoiseLabs\Bundle\SmartyBundle(),
            new Blog\EntryBundle\BlogEntryBundle(),
        );  

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }   

        return $bundles;
    }   

    public function registerContainerConfiguration(LoaderInterface $loader)
    {   
        $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
    }   
}

这是自动加载程序代码:

<?php

use Symfony\Component\ClassLoader\UniversalClassLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;

$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
    'Symfony'          => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
    'Sensio'           => __DIR__.'/../vendor/bundles',
    'JMS'              => __DIR__.'/../vendor/bundles',
    'NoiseLabs'        => __DIR__.'/../vendor/bundles',
    'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
    'Doctrine\\DBAL'   => __DIR__.'/../vendor/doctrine-dbal/lib',
    'Doctrine'         => __DIR__.'/../vendor/doctrine/lib',
    'Monolog'          => __DIR__.'/../vendor/monolog/src',
    'Assetic'          => __DIR__.'/../vendor/assetic/src',
    'Metadata'         => __DIR__.'/../vendor/metadata/src',
));
$loader->registerPrefixes(array(
    'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
    'Twig_'            => __DIR__.'/../vendor/twig/lib',
));

// intl
if (!function_exists('intl_get_error_code')) {
    require_once __DIR__.'/../vendor/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';

    $loader->registerPrefixFallbacks(array(__DIR__.'/../vendor/symfony/src/Symfony/Component/Locale/Resources/stubs'));
}

$loader->registerNamespaceFallbacks(array(
    __DIR__.'/../src',
));
$loader->register();

AnnotationRegistry::registerLoader(function($class) use ($loader) {
    $loader->loadClass($class);

// Swiftmailer needs a special autoloader to allow
// the lazy loading of the init file (which is expensive)
require_once __DIR__.'/../vendor/swiftmailer/lib/classes/Swift.php';
Swift::registerAutoload(__DIR__.'/../vendor/swiftmailer/lib/swift_init.php');
4

2 回答 2

6

感谢您使用 SmartyBundle(我是创建者)。

该死的,文档是错误的。请换行:

new \NoiseLabs\Bundle\SmartyBundle(),

经过

new \NoiseLabs\Bundle\SmartyBundle\SmartyBundle(),
于 2012-02-04T19:44:57.743 回答
1

我在使用手册时遇到了问题,但我解决了。使用以下步骤:

  1. 下载 Smarty并将其解压到您的 htdocs 目录中。结果你得到一个像这样的文件树:htdocs/Smarty/libs/Smarty.class.php

  2. 通过 ZIP 按钮在Github上下载Symfony 2 的 Smarty Bundle 。创建以下文件夹层次结构:htdocs/yourSymfonyFolder/src/NoiseLabs/Bundle/SmartyBundle 并使用 Smarty.php、SmartyBundle.php、SmartyEngine.php 和所有其他文件将下载的 zip 解压缩到 htdocs/Symfony/src/NoiseLabs/Bundle/智能捆绑。

  3. 转到您的 Web 服务器 php.ini,例如 c:/xampp/php/php.ini,搜索以下行:

    include_path = ".;c:\php\includes;"
    

    并添加 c:\yourHtdocsFolder\Smarty\libs\ 与全部结果:

    include_path = ".;c:\php\includes;c:\yourHtdocsFolder\Smarty\libs\"
    
  4. 打开文件 htdocs/yourSymfonyFolder/app/AppKernel.php 并添加:

    $bundles = array(
        [...]
        new NoiseLabs\Bundle\SmartyBundle\SmartyBundle(),
        [...]
    );
    
  5. 就是这样!在控制器 src/Acme/HelloBundle/Controller/DefaultController.php 中使用以下示例:

    public function indexAction($name)
    {
        return $this->render('AcmeHelloBundle:Default:index.html.tpl', array('name' => $name);
    }
    
  6. 在路径 src/Acme/HelloBundle/Resources/views/Default/index.html.tpl 中创建一个模板:

    Hello {$name}
    
  7. 调用 URL www.yourWebsite/app_dev.php/hello/yourName。请注意,您必须在路由文件中注册 URL。

于 2012-05-20T07:18:02.753 回答