zend framework2 - Apigility:在引导程序中发送错误代码 404 模块.php


zend framework2 - Apigility: Send Error code 404 module.php in bootstrap

我使用 APIGILITY 创建一个 Web 服务,我想解决一个问题。我没有链接到我的应用程序的特定数据库,数据库由 URL 中的参数指定。例如 http://sk.localhost/users-service/1,其中 1 是数据库的参数"projet",您可以在下面看到。

这是我的数据库.conf:

return array(
    'db' => array(
    	'1' => array(
			'driver' => 'Pdo',
        	'dsn' => 'mysql:dbname=projet;host=localhost',
        	'driver_option' => array(
            1002 => 'SET NAMES ''UTF8'''
            ),	
         
        	),
        
        '2' => array(
        	'driver' => 'Pdo',
        	'dsn' => 'mysql:dbname=cgm;host=localhost',
        	'driver_option' => array(
            1002 => 'SET NAMES ''UTF8'''
            ),
        	));

我的代码是使用以下参数指定数据库:

class Module
{
   private $id_base;
   private $conf_Users_tables;
   private $conf_Droits_tables; 
   private $data;
  
    public function init(ModuleManager $moduleManager)
    {  
        $events = $moduleManager->getEventManager();
        $events->attach(ModuleEvent::EVENT_MERGE_CONFIG, array($this, 'initDBSConfig'));
    }
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        $eventManager->attach('route', array($this, 'checkRoute'));
    }
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
    public function getAutoloaderConfig()
    {
        return array(
            'Zend'Loader'StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
    public function checkRoute(MvcEvent $e) 
    {      
        $route = $e->getRouteMatch()->getParams();
        $this->id_base = $route['id_base'];
    }
    
     
public function initDBSConfig(ModuleEvent $e)
    { 
        $conf = include __DIR__ . '/../../config/Dbs.conf.php';
        $this ->data = $conf['db'];
        $this->initConfTables(); 
    }
    function initConfTables()
    {   
        $confUsers = include __DIR__ . '/../../config/Users.conf.php';
        $confDroits = include __DIR__ . '/../../config/Droits.conf.php';
        $this->conf_Users_tables = $confUsers;
        $this->conf_Droits_tables = $confDroits;
    }
    public function getServiceConfig()
     {
        return array(
             'factories' => array(  
                 'Application'Model'UsersTable' =>  function($sm) {     
                    if(isset($this->data[$this->id_base]))
                    {                                    
                        $tableGateway = $sm->get('UsersTableGateway');
                        $table = new UsersTable($tableGateway);
                        return $table;
                    }
                    else
                    {
                        return;
                    }
                 },
                 'UsersTableGateway' => function ($sm) {
                         
                     $dbAdapter = $sm->get('SwitchDbAdapter');
                     $resultSetPrototype = new ResultSet();                    
                     $resultSetPrototype->setArrayObjectPrototype($sm->get('UsersModel'));
                     return new TableGateway($this ->data['t'.$this->id_base]['users'], $dbAdapter, null, $resultSetPrototype);
                 },
                 'UsersModel' => function($sm) {
                     $usersModel = new UsersModel();
                     $usersModel->setConfig($this->conf_Users_tables[$this->id_base]);
                     return $usersModel;
                 },
        
                 'Application'Model'DroitsTable' =>  function($sm) {                    
                     $tableGateway = $sm->get('DroitsTableGateway');
                     $table = new DroitsTable($tableGateway);
                     return $table;
                 },
                 'DroitsTableGateway' => function ($sm) {                     
                    $dbAdapter = $sm->get('SwitchDbAdapter');
                        $resultSetPrototype = new ResultSet();
                        $resultSetPrototype->setArrayObjectPrototype($sm->get('DroitsModel'));
                        return new TableGateway($this ->data['t'.$this->id_base]['droits'], $dbAdapter, null, $resultSetPrototype);
                 },
                 'DroitsModel' => function($sm) {
                     $droitsModel = new DroitsModel();
                     $droitsModel->setConfig($this->$conf_Droits_tables[$this->id_base]);
                     return $droitsModel;
                 },
                 
                 'SwitchDbAdapter' => function ($sm) {
                   
                        $dbAdapter = new 'Zend'Db'Adapter'Adapter($this->data[$this->id_base]);
                        return $dbAdapter;
                 },
                ),          
            );
     }
}

因此,如果 URL 中的参数与我的配置中的任何数据库不匹配,我希望应用程序发送 HTTP 错误代码 404。

public function getServiceConfig() 
{
    if (empty($this->data[$this->id_base])){
        header('HTTP/1.0 404 Not Found');
        die('<h1>404 Not Found</h1>Incorrect Database request.');
    }
    // Rest of you code in method getServiceConfig() ...
}