zend framework2 - Apigility: Send Error code 404 module.php in bootstrap -
i use apigility create web-service, i’d resolve problem. don’t have specific database linked app ,the database specified parameter in url. example http://sk.localhost/users-service/1, 1 database’s paramaters “projet” can see below.
it’s database .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\'' ), ));
and it’s code specify database parameter:
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; }, ), ); } }
so i’d application send http error code 404 if parameter in url doesn’t match databases in config.
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 code in method getserviceconfig() ... }
Comments
Post a Comment