@ user1986560の回答は完全ではありません。ルーターで「url」などの基本的なヘルパーが必要であり、場合によっては「basePath」も必要になるためです。
ここに完全なコードがあります -
$myApp=Zend\Mvc\Application::init(require 'config/application.config.php');
$sm=$myApp->getServiceManager();
$view=new \Zend\View\Renderer\PhpRenderer();
$view->getHelperPluginManager()->setServiceLocator($sm);
$basePathX='/your-folder-from-where-you-want-to-run';
$request=$myApp->getRequest();
if($request instanceof \Zend\Http\PhpEnvironment\Request){
#die($request->getBasePath());
$basePathX=$request->getBasePath();
}
$basePath1=explode('/', $basePathX);
/* please fix this path as your requirement */
array_pop($basePath1);
$bsPath=implode('/', $basePath1).'/';
/* following line will fix your router path, it is important */
$myApp->getMvcEvent()->getRouter()->setBaseUrl($bsPath);
$basePath=new \Zend\View\Helper\BasePath();
$basePath->setBasePath($bsPath);
$view->getHelperPluginManager()->setService('basePath', $basePath);
$urlHlpr=new \Zend\View\Helper\Url();
$urlHlpr->setRouter($myApp->getMvcEvent()->getRouter());
$view->getHelperPluginManager()->setService('url', $urlHlpr);
$resolver=new \Zend\View\Resolver\TemplateMapResolver();
$resolver->setMap(array(
'wmsLayout' => APP_ROOT_PATH . 'module'.DS.'YourModule'.DS.'view'.DS.'layout'.DS.'layout1.phtml',
'layout/left-menu' => APP_ROOT_PATH . 'module'.DS.'YourModule'.DS.'view'.DS.'layout'.DS.'left-menu.phtml',
));
$view->setResolver($resolver);
$viewModel = new \Zend\View\Model\ViewModel();
$tmpltFileToLoad=__DIR__ . DS.'your-file-to-render.phtml';
$tmpltIdx='_'.md5($tmpltFileToLoad);
$resolver->add($tmpltIdx, $tmpltFileToLoad);
$viewModel->setTemplate($tmpltIdx)->setVariables(array(
'test' => 'shahadat hossain khan'
));
$content=$view->render($viewModel);
$viewLayout = new \Zend\View\Model\ViewModel();
$viewLayout->setTemplate('wmsLayout')->setVariables(array(
'content' => $content,
'anyOtherVariableForLayout'=>'layout variable value',
));
echo $view->render($viewLayout);
それが誰かの助けになることを願っています。