Zend Framework 2 - 可在控制器/模型中访问的全局变量,在local.php或global.php中初始化

时间:2014-01-10 12:40:34

标签: zend-framework frameworks

大家好,请有人帮我如何在zend框架2中创建一个全局变量,用于可在控制器和模型中访问的表前缀。

感谢所有人。

1 个答案:

答案 0 :(得分:0)

在config / database.local.php中,您可以定义全局

<? 
return array(
    'service_manager' => array(
        'factories' => array(
            //'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
            'Zend\Db\Adapter\Adapter' => function ($serviceManager) {
                $adapterFactory = new Zend\Db\Adapter\AdapterServiceFactory();
                $adapter = $adapterFactory->createService($serviceManager);
                \Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter($adapter);
                return $adapter;
         }
        ),
    ),

    'db' => array(
        'driver'    => 'pdo',
        'dsn'       => 'mysql:dbname=testdb;host=localhost',
        'username'  => 'root',
        'password'  => '',
    ),
    'msg' => array(
        'add'    => 'Data Inserted Successfully',
        'edit'   => 'Data Updated Successfully',
        'delete' => 'Data Deleted Successfully',
    ),
);
?>

控制器文件: DemoController.php

<?php
  namespace Demo\Controller;
  use Zend\Mvc\Controller\AbstractActionController;
    class DemoController extends AbstractActionController
    { 
       public function indexAction($cms_page_name='whyus')
        { 

            /*Call config file to fetch current cms page id-- fetch config file from database.local.php*/
            $config = $this->getServiceLocator()->get('Config');
            $all_msg = $config['msg'];
       }
    }
?>