【问题标题】:Exception: Service 'voltService' wasn't found in the dependency injection container例外:在依赖注入容器中找不到服务“voltService”
【发布时间】:2017-07-09 10:18:27
【问题描述】:

我正在尝试复制 Volt 教程 here 中的示例,使用 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>

我还在我的引导文件 (/public/index.php) 中注册了 volt,如下所示:

<?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) 时,我收到以下错误:

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

我检查了 Volt 服务是否尚未在 Services.php 中声明,正如在类似的帖子 here 中所说,但事实并非如此。

谢谢

【问题讨论】:

    标签: phalcon


    【解决方案1】:

    问题在于这段代码。您告诉您的视图应该使用volt 扩展并使用名为voltService 的服务对其进行处理。

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

    如果您查看您的 sn-p,则没有定义名为 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 2011-11-13
      相关资源
      最近更新 更多