2014-06-16 60 views
2

我渲染嵌套視圖中的問題,這裏是我想要做的 我改變你的HMVC的「請求」(HMVC-GitHub和/或HMVC-Pattern)函數爲元素模塊爾康:HMVC視圖不工作

namespace Modules\Main\Libraries; 

/** 
* Elements 
* 
* Helps to build UI elements for the application 
*/ 
class Elements extends \Phalcon\Mvc\User\Component 
{ 

    public function loadModule($path = '', $data = array()) { 
     $di = clone $this->getDI(); 
     $dispatcher = $di->get('dispatcher'); 

     $paths = explode('/', $path); 
     $data = is_array($data) ? $data : array($data); 

     // get controller name 
     if (isset($paths[0])) { 
      $controller = $paths[0]; 
     } 

     // get action name 
     if (isset($paths[1])) { 
      $action = $paths[1]; 
     } 

     // get params 
     if (isset($paths[2])) { 
      array_splice($paths, 0, 2); 
      $params = array_merge($paths, $data); 
     } else { 
      $params = $data; 
     } 

     if (!empty($controller)) { 
      $dispatcher->setControllerName($controller); 
     } else { 
      $dispatcher->setControllerName('index'); 
     } 

     if (!empty($action)) { 
      $dispatcher->setActionName($action); 
     } else { 
      $dispatcher->setActionName('index'); 
     } 

     if (!empty($params)) { 
      if(is_array($params)) { 
       $dispatcher->setParams($params); 
      } else { 
       $dispatcher->setParams((array) $params); 
      } 
     } else { 
      $dispatcher->setParams(array()); 
     } 

     $dispatcher->dispatch(); 

     $response = $dispatcher->getReturnedValue(); 
     if ($response instanceof ResponseInterface) { 
      return $response->getContent(); 
     } 

     return $response; 
    } 
} 

和我有2個控制器:

namespace Modules\Main\Controllers; 

class IndexController extends ControllerBase 
{ 

    public function indexAction() 
    { 
     $secondContent = $this->elements->loadModule('test/hello/json'); 

     $this->view->setVar('secondContent', $secondContent); 
    } 

} 

namespace Modules\Main\Controllers; 

use \Phalcon\Http\Response; 

class TestController extends ControllerBase 
{ 

    public function indexAction() 
    { 

    } 

    public function helloAction($format='html', $param = 'empty') 
    { 
     $this->view->setVar('content', 'Hello this is test value "'.$param.'"'); 

     $content = $this->view->getContent(); 

     return (string)$content; 

     // return 'Hello this is test value "'.$param.'"'; 
    } 

} 

我的DI

 $di['elements'] = function() { 
      return new \Modules\Main\Libraries\Elements(); 
     }; 

查看文件 的IndexController ::指數

<h1>Congratulations!</h1> 

<p>You're now flying with Phalcon. Great things are about to happen!</p> 
<p>Second content: {{ secondContent}}</p> 
<p>HMVC: {{ elements.loadModule('test/hello/json', 'test') }}</p> 

,併爲HelloController ::測試

This is :: {{ content }} 

期望得到

Congratulations! 
You're now flying with Phalcon. Great things are about to happen! 
Second content: This is :: Hello this is test value "empty" 
HMVC: This is :: Hello this is test value "test" 

但只渲染HelloController中(從索引控制器首先調用::的indexAction):

This is :: Hello this is test value "empty" 

如果我改變的IndexController ::到的indexAction

public function indexAction() 
{ 
    $secondContent = ''; 

    $this->view->setVar('secondContent', $secondContent); 
} 

和::的TestController到helloAction

public function helloAction($format='html', $param = 'empty') 
{ 
    $this->view->setVar('content', 'Hello this is test value "'.$param.'"'); 

    $content = $this->view->getContent(); 

    //return (string) $content; 

    return 'Hello this is test value "'.$param.'"'; 
} 

我得到的結果是(第二內容爲空):

Congratulations! 
You're now flying with Phalcon. Great things are about to happen! 
Second content: 
HMVC: Hello this is test value "test" 

解決此問題的任何解決方案?

感謝, 赫爾曼

回答

2

爾康有內置模塊它的功能,你不必建立自己的模塊加載器,你只需要創建模塊引導擴展ModuleDefinitionInterface。

只要看看該樣品從多法爾康模塊

https://github.com/phalcon/mvc/tree/master/multiple

下面這個例子中,從鏈路採取以上,這包含模塊引導代碼。

<?php 

namespace Multiple\Frontend; 

class Module 
{ 

    public function registerAutoloaders() 
    { 

     $loader = new \Phalcon\Loader(); 

     $loader->registerNamespaces(array(
      'Multiple\Frontend\Controllers' => '../apps/frontend/controllers/', 
      'Multiple\Frontend\Models' => '../apps/frontend/models/', 
     )); 

     $loader->register(); 
    } 

    /** 
    * Register the services here to make them general or register in the ModuleDefinition to make them module-specific 
    */ 
    public function registerServices($di) 
    { 

     //Registering a dispatcher 
     $di->set('dispatcher', function() { 
      $dispatcher = new \Phalcon\Mvc\Dispatcher(); 

      //Attach a event listener to the dispatcher 
      $eventManager = new \Phalcon\Events\Manager(); 
      $eventManager->attach('dispatch', new \Acl('frontend')); 

      $dispatcher->setEventsManager($eventManager); 
      $dispatcher->setDefaultNamespace("Multiple\Frontend\Controllers\\"); 
      return $dispatcher; 
     }); 

     //Registering the view component 
     $di->set('view', function() { 
      $view = new \Phalcon\Mvc\View(); 
      $view->setViewsDir('../apps/frontend/views/'); 
      return $view; 
     }); 

     $di->set('db', function() { 
      return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
       "host" => "localhost", 
       "username" => "root", 
       "password" => "secret", 
       "dbname" => "invo" 
      )); 
     }); 

    } 

} 

您可以通過以下

此代碼加載模塊