ÔÎÄ£ºhttp://www.phpddt.com/php/ci-service.html
´ó¼ÒÖªµÀCodeIgniter¿ò¼ÜʽMVC·Ö²ãµÄ£¬Í¨³£´ó¼Ò°ÑÒµÎñÂ߼дµ½ControllerÖУ¬¶øModelÖ»¸ºÔðºÍÊý¾Ý¿â´ò½»µÀ¡£
µ«ÊÇËæ×ÅÒµÎñÔ½À´Ô½¸´ÔÓ£¬controllerÔ½À´Ô½Ó·Ö×£¬¾ÙÒ»¸ö¼òµ¥µÄÀý×Ó£¬±ÈÈç˵Óû§Ï¶©µ¥£¬Õâ±ØÈ»»áÓÐһϵÁеIJÙ×÷£º¸üйºÎï³µ¡¢Ìí¼Ó¶©µ¥¼Ç¼¡¢»áÔ±Ìí¼Ó»ý·ÖµÈµÈ£¬ÇÒ϶©µ¥µÄ¹ý³Ì¿ÉÄÜÔÚ¶àÖÖ³¡¾°³öÏÖ£¬Èç¹ûÕâÑùµÄ´úÂë·ÅcontrollerÖÐÔòºÜÓ·Ö×ÄÑÒÔ¸´Óã¬Èç¹û·Åmodel»áÈó־òãºÍÒµÎñ²ãñîºÏ¡£ÏÖÔÚ¹«Ë¾µÄÏîÄ¿¾ÍÊÇ£¬ºÜ¶àÈ˽«Ò»Ð©ÒµÎñÂ߼дµ½modelÖÐÈ¥ÁË£¬modelÖÐÓÖµ÷ÆäËümodel£¬Ò²¾ÍÊÇÒµÎñ²ãºÍ³Ö¾Ã²ãÏ໥ñîºÏ¡£ÕâÊǼ«Æä²»ºÏÀíµÄ£¬»áÈÃmodelÄÑÒÔά»¤£¬ÇÒ·½·¨ÄÑÒÔ¸´Óá£
ÊDz»ÊÇ¿ÉÒÔ¿¼ÂÇÔÚcontrollerºÍmodelÖмÓÒ»¸öÒµÎñ²ãservice£¬ÓÉËüÀ´¸ºÔðÒµÎñÂß¼£¬·â×°ºÃµÄµ÷ÓýӿڿÉÒÔ±»controller¸´Óá£
ÕâÑù¸÷²ãµÄÈÎÎñ¾ÍÃ÷È·ÁË£º
Model(DAO):Êý¾Ý³Ö¾Ã²ãµÄ¹¤×÷£¬¶ÔÊý¾Ý¿âµÄ²Ù×÷¶¼·â×°ÔÚÕâ¡£
Service £º ÒµÎñÂß¼²ã£¬¸ºÔðÒµÎñÄ£¿éµÄÂß¼Ó¦ÓÃÉè¼Æ£¬controllerÖоͿÉÒÔµ÷ÓÃserviceµÄ½Ó¿ÚʵÏÖÒµÎñÂß¼´¦Àí£¬Ìá¸ßÁËͨÓõÄÒµÎñÂß¼µÄ¸´ÓÃÐÔ£¬Éè¼Æµ½¾ßÌåÒµÎñʵÏÖ»áµ÷ÓÃModelµÄ½Ó¿Ú¡£
Controller £º¿ØÖƲ㣬¸ºÔð¾ßÌåÒµÎñÁ÷³Ì¿ØÖÆ£¬ÕâÀïµ÷ÓÃservice²ã£¬½«Êý¾Ý·µ»Øµ½ÊÓͼ
View £º ¸ºÔðÇ°¶ËÒ³Ãæչʾ£¬ÓëController½ôÃÜÁªÏµ¡£
»ùÓÚÉÏÃæÃèÊö£¬ÊµÏÖ¹ý³Ì£º
£¨1£©ÈÃCIÄܹ»¼ÓÔØservice,serviceĿ¼·ÅÔÚapplicationÏ£¬ÒòΪCIϵͳûÓÐservice£¬ÔòÔÚapplication/coreÏÂн¨À©Õ¹MY_Service.php
<?php class MY_Service { public function __construct() { log_message('debug', "Service Class Initialized"); } function __get($key) { $CI = & get_instance(); return $CI->$key; } }£¨2£©À©Õ¹CI_LoaderʵÏÖ£¬¼ÓÔØservice£¬ÔÚapplication/coreÏÂн¨MY_Loader.phpÎļþ£º
<?php class MY_Loader extends CI_Loader { /** * List of loaded sercices * * @var array * @access protected */ protected $_ci_services = array(); /** * List of paths to load sercices from * * @var array * @access protected */ protected $_ci_service_paths = array(); /** * Constructor * * Set the path to the Service files */ public function __construct() { parent::__construct(); $this->_ci_service_paths = array(APPPATH); } /** * Service Loader * * This function lets users load and instantiate classes. * It is designed to be called from a user's app controllers. * * @param string the name of the class * @param mixed the optional parameters * @param string an optional object name * @return void */ public function service($service = '', $params = NULL, $object_name = NULL) { if(is_array($service)) { foreach($service as $class) { $this->service($class, $params); } return; } if($service == '' or isset($this->_ci_services[$service])) { return FALSE; } if(! is_null($params) && ! is_array($params)) { $params = NULL; } $subdir = ''; // Is the service in a sub-folder? If so, parse out the filename and path. if (($last_slash = strrpos($service, '/')) !== FALSE) { // The path is in front of the last slash $subdir = substr($service, 0, $last_slash + 1); // And the service name behind it $service = substr($service, $last_slash + 1); } foreach($this->_ci_service_paths as $path) { $filepath = $path .'service/'.$subdir.$service.'.php'; if ( ! file_exists($filepath)) { continue; } include_once($filepath); $service = strtolower($service); if (empty($object_name)) { $object_name = $service; } $service = ucfirst($service); $CI = &get_instance(); if($params !== NULL) { $CI->$object_name = new $service($params); } else { $CI->$object_name = new $service(); } $this->_ci_services[] = $object_name; return; } } }£¨3£©¼òµ¥Àý×ÓʵÏÖ£º
¿ØÖÆÆ÷Öе÷ÓÃservice £º
<?php class User extends CI_Controller { public function __construct() { parent::__construct(); $this->load->service('user_service'); } public function login() { $name = 'phpddt.com'; $psw = 'password'; print_r($this->user_service->login($name, $psw)); } }serviceÖе÷ÓÃmodel £º
<?php class User_service extends MY_Service { public function __construct() { parent::__construct(); $this->load->model('user_model'); } public function login($name, $password) { $user = $this->user_model->get_user_by_where($name, $password); //..... //..... //..... return $user; } }modelÖÐÄãÖ»¸údb´ò½»µÀ£º
<?php class User_model extends CI_Model { public function __construct() { parent::__construct(); } public function get_user_by_where($name, $password) { //$this->db //...... //...... return array('id' => 1, 'name' => 'mckee'); } }
»ù±¾ÊµÏÖ˼·¾ÍÊÇÕâÑùµÄ¡£
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ