2017-02-22 47 views
0

我使用迷你php MVC。 bootstrap的变量不包含在生成的view中。我可以包括entityManagerindex.php, 但我不能包括entityManager来查看文件。为什么?如何将教条实体管理器包含在View生成的文件中?如何将教条实体管理器包含到View生成的文件中?

如何在以下文件中包含实体管理器到_register1Db.php文件? 正确显示视图,它不显示没有找到bootstrap.php的错误(如果我chaneg路径,这样的错误显示),但视图不包括从bootstrap.php实体管理器

// 1 。 \ src \ bootstrap.php

use Doctrine\ORM\Tools\Setup; 
use Doctrine\ORM\EntityManager; 

$isDevMode = true; 
$entitiesPaths = array(__DIR__.'/CrmBundle/Entity'); 

$dbParams = array(
    'dbname' => 'dbname', 
    'user' => 'user', 
    'password' => 'pswd', 
    'host' => 'localhost', 
    'driver' => 'pdo_mysql', 
); // 'pdo_mysql', 


$config = Setup::createAnnotationMetadataConfiguration($entitiesPaths, $isDevMode); 
$em = \Doctrine\ORM\EntityManager::create($dbParams, $config); 

$some='some'; 

// 2。 \ public \ index.php

require_once '../src/bootstrap.php'; //which requires autoload.php 
print_r('<br><br> index some='.$some); // works 
var_dump($em); //works 
use core\App; 
$app = new App(); //App from urk identifines controller and action and pass url paramteters to them, controller action analize parameters and displays views, in this case security\register 

// 3。 src \ CrmBundle \ Resources \ views \ security \ register.php

include ('_register1Db.php'); 

// 4。 SRC \ CrmBundle \资源\视图\ security_register1Db.php

require_once '/../../../../bootstrap.php'; // 
var_dump($em); // Undefined variable: em 
print_r('<br><br> register some='.$some); //Undefined variable: some 

//我生成视图的方法:SRC \芯\ View.php

ob_start(); 
include_once(__DIR__.'/../bootstrap.php'); 
include_once($this->viewTemplatePath.$this->file); 
ob_end_flush(); 

//视图产生与从自举调试变量.php:src \ core \ View.php

include_once(__DIR__.'/../bootstrap.php'); 
    ob_start(); 
    // think how to render extract variable in order to pass to the file 
    include_once(__DIR__.'/../bootstrap.php'); 
    var_dump($em); //undefined 
    print_r('<br><br> parseViewPhpinside some=' . $some); //undefined 
    print_r('<br><br> parseViewPhpinside __DIR__=' . __DIR__); // ....apache2\htdocs\own\log\src\core 
    include_once($this->viewTemplPath.$this->file); 
    if($store) return ob_get_clean(); 
    else ob_end_flush(); 
    var_dump($em); //undefined 
    print_r('<br><br> parseViewPhp some='.$some); //undefined 

回答

0

这是一个解决方案,使事情工作,但不是答案。

解决方法是创建一个文件dbcon.php并将其包含在register.phpregister1Db.php中。的dbcon.php

use Doctrine\ORM\Tools\Setup; 
use Doctrine\ORM\EntityManager; 

$isDevMode = true; 
$entitiesPaths = array(__DIR__.'/CrmBundle/Entity'); 
$dbParams = array(
    'dbname' => 'dbname', 
    'user' => 'user', 
    'password' => 'pswd', 
    'host' => 'localhost', 
    'driver' => 'pdo_mysql', 
); // 'pdo_mysql', 
$config = Setup::createAnnotationMetadataConfiguration($entitiesPaths, $isDevMode); 
$em = \Doctrine\ORM\EntityManager::create($dbParams, $config); 

print_r('<br><br> end of dbcon.php '); 

//内容我不明白为什么,如果我有bootsrap.php不起作用? 但是,如果我包含来自另一个文件的类似内容,它的工作原理。

也许因为我已经在index.php中创建了一个对象App.php,它显示了视图文件,而所有的视图文件第二次不包含bootsrap,因为它看起来已经包含了bootstrap,尽管变量不可用?

可能是什么原因?