2017-07-09 6 views
0

私はVoltチュートリアルhereの例をphalconの基本例hereを使って複製しようとしています。このような例外:依存関係注入コンテナに 'voltService'サービスが見つかりませんでした

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

と、それに対応するアプリ/ビュー/記事/ 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> 

私も中にボルトを登録

だから私はこのようなこのアプリ/コントローラ/ PostsControllersを作成しました私のブートストラップファイル(/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)私は次のエラーを取得するにESS:

ボルトサービスは、それが同様のポスト hereに言われるよう、既に Services.phpで宣言されていないが、それはないされた場合、私がチェック

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

ありがとうございました

答えて

2

このコードブロックに問題があります。あなたは、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

関連する問題