异常:在依赖项注入容器中找不到服务 'voltService'

Exception: Service 'voltService' wasn't found in the dependency injection container

我正在尝试复制 Volt 教程中的示例 here, using the basic example of phalcon here,所以没什么复杂的。

所以我这样创建了 app/controllers/PostsControllers:

<?php

use Phalcon\Mvc\Controller;

class PostsController extends Controller
{
    public function indexAction()
    {
/*        $post = Post::findFirst();
        $menu = Menu::findFirst();*/

        $post = array("title"=>"The titre");
        $menu = "menu1";

        $this->view->show_navigation = true;
        $this->view->menu            = $menu;
        $this->view->title           = $post["title"];
        $this->view->post            = $post;

        // Or...

        $this->view->setVar('show_navigation', true);
        $this->view->setVar('menu',            $menu);
        $this->view->setVar('title',           $post["title"]);
        $this->view->setVar('post',            $post);
    }
}

及其对应的app/views/posts/index.phtml是这样的:

{# app/views/posts/show.phtml #}
<!DOCTYPE html>
<html>
    <head>
        <title>{{ title }} - An example blog</title>
    </head>
    <body>

        {% if show_navigation %}
            <ul id='navigation'>
                {% for item in menu %}
                    <li>
                        <a href='{{ item.href }}'>
                            {{ item.caption }}
                        </a>
                    </li>
                {% endfor %}
            </ul>
        {% endif %}

        <h1>{{ post.title }}</h1>

        <div class='content'>
            {{ post.content }}
        </div>

    </body>
</html>

我还在我的 bootstrap 文件 (/public/index.php) 中注册了伏特,它看起来像这样:

<?php

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt;


// Register an autoloader
$loader = new Loader();

$loader->registerDirs(
    [
        "../app/controllers/",
        "../app/models/",
    ]
);

$loader->register();



// Create a DI
$di = new FactoryDefault();

// Setup the view component
$di->set(
    "view",
    function () {
        $view = new View();

        $view->setViewsDir("../app/views/");

        $view->registerEngines(
            [
                '.volt' => 'voltService',
            ]
        );


        return $view;
    }
);

// Setup a base URI so that all generated URIs include the "tutorial" folder
$di->set(
    "url",
    function () {
        $url = new UrlProvider();

        $url->setBaseUri("/care/");

        return $url;
    }
);



$application = new Application($di);

try {
    // Handle the request
    $response = $application->handle();

    $response->send();
} catch (\Exception $e) {
    echo "Exception: ", $e->getMessage();
}

但是当我尝试访问 /posts 目录 (localhost/care/posts) 时,出现以下错误:

Exception: Service 'voltService' wasn't found in the dependency injection container

我检查了是否已在 Services.php 中声明了 Volt 服务,正如在类似的 post 中所说的那样,但事实并非如此。

谢谢

问题出在这段代码上。您告诉您的视图它应该使用 volt 扩展并使用名为 voltService 的服务处理它。

// Setup the view component
$di->set(
    "view",
    function () {
        $view = new View();
        $view->setViewsDir("../app/views/");
        $view->registerEngines(
            [
                '.volt' => 'voltService',
            ]
        );

        return $view;
    }
);

如果您查看您的代码段,没有定义名为 voltService 的服务。

但是,如果您将此添加到您的服务中,它应该会起作用:

// Register Volt as a service
$di->set(
    'voltService',
    function ($view, $di) {
        $volt = new Volt($view, $di);

        $volt->setOptions(
            [
                'compiledPath'      => '../app/compiled-templates/',
                'compiledExtension' => '.compiled',
            ]
        );

        return $volt;
    }
);

参考:https://docs.phalconphp.com/en/3.2/volt#setup