thinkphpÔõô×Ô½ç˵ÃüÃû
thinkphpÊÇÒ»¿îºÜÊÇÊ¢ÐеÄphp¿ò¼Ü£¬ËüµÄÃüÃû¹æÔòÊÇ×ñÕÕÔ¼¶¨Ë׳ɵÄpsr-4×Ô¶¯¼ÓÔع淶µÄ¡£Óû§¿ÉÒÔͨ¹ýÃüÃû¿Õ¼äµÄ·½·¨À´×Ô½ç˵ÃüÃû¡£
Ò»¡¢ÃüÃû¿Õ¼ä
1.1 ʲôÊÇÃüÃû¿Õ¼ä
ÃüÃû¿Õ¼äÊÇÒ»ÖÖ½«´úÂë°üÀ¨ÔÚÒ»¸öÌض¨µÄ¹æÄ£ÄÚµÄÊÖÒÕ£¬¸ÃÊÖÒÕ¿ÉÒÔ¶Ô´úÂë¾ÙÐаü×°ºÍ¸ôÀ룬ÒÔ±ãÓÚʹÓúÍά»¤¡£
ÔÚPHPÖнç˵ÃüÃû¿Õ¼äºÜÊǼòÆÓ£¬Ö»ÐèÒªÔÚÀà½ç˵֮ǰÉùÃ÷Ò»¸öÃüÃû¿Õ¼ä¼´¿É¡£ÀýÈ磺
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
<?php namespace appcontrollers; class IndexController{ // ... }
µÇ¼ºó¸´ÖÆ
1.2 ÃüÃû¿Õ¼äµÄ×÷ÓÃ
ÃüÃû¿Õ¼äµÄÖ÷Òª×÷ÓÃÊÇ×èÖ¹ÃüÃû³åÍ»£¬Ëü¿ÉÒÔÈÃÎÒÃÇÔÚÒ»¸öPHPÓ¦ÓóÌÐòÖÐʹÓòî±ðµÄ´úÂë¿â¶ø²»»á±¬·¢ÃüÃû³åÍ»¡£
±ðµÄ£¬ÃüÃû¿Õ¼ä»¹¿ÉÒÔÈÃÎÒÃǸüºÃµØ×éÖ¯´úÂ룬Ìá¸ß´úÂëµÄ¿É¶ÁÐԺͿÉά»¤ÐÔ¡£
¶þ¡¢×Ô½ç˵ÃüÃû
ÔÚThinkPHPÖУ¬Ä¬ÈϵÄÃüÃû¿Õ¼äÊÇ¡°app¡±£¬Ò²¾ÍÊÇÎÒÃǵÄÓ¦ÓóÌÐò¸ùÃüÃû¿Õ¼ä¡£¿ÉÊÇ£¬ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇͨ³£ÐèÒª×Ô½ç˵ÃüÃûÒÔ¸üºÃµØ×éÖ¯ÎÒÃǵĴúÂë¡£
2.1 Ŀ¼½á¹¹
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½ç˵һ¸öеÄĿ¼½á¹¹¡£ÀýÈ磬ÎÒÃÇÔÚÓ¦ÓóÌÐòµÄ¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪ¡°common¡±µÄĿ¼£¬¸ÃĿ¼ÏÂÓÐÁ½¸ö×ÓĿ¼¡°controller¡±ºÍ¡°model¡±£¬»®·ÖÓÃÓÚ´æ·Å¿ØÖÆÆ÷ºÍÄ£×ÓÎļþ¡£
|-- application | |-- common | | |-- controller | | |-- model | |-- config | |-- ...
µÇ¼ºó¸´ÖÆ
2.2 ÃüÃû¿Õ¼ä½ç˵
ÎÒÃÇÐèÒªÔÚ¿ØÖÆÆ÷ºÍÄ£×ÓÎļþÖнç˵еÄÃüÃû¿Õ¼ä£¬ÀýÈ磺
<?php namespace appcommoncontroller; class BaseController{ // ... }
µÇ¼ºó¸´ÖÆ
<?php namespace appcommonmodel; use thinkModel; class UserModel extends Model{ // ... }
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬ÎÒÃǾͽç˵ÁËÒ»¸öÃûΪ¡°appcommon¡±µÄÐÂÃüÃû¿Õ¼ä£¬ÒÔ¼°ÔÚ¸ÃÃüÃû¿Õ¼äϵġ°controller¡±ºÍ¡°model¡±×ÓÃüÃû¿Õ¼ä¡£
2.3 ×Ô¶¯¼ÓÔØ
×îºó£¬ÎÒÃÇÐèÒª¸æËßThinkPHPÔõÑù×Ô¶¯¼ÓÔØÎÒÃǽç˵µÄÃüÃû¿Õ¼ä¡£ÔÚÓ¦ÓóÌÐò¸ùĿ¼Ïµġ°config¡±Ä¿Â¼ÖÐÓÐÒ»¸öÃûΪ¡°autoload.php¡±µÄÎļþ£¬ÎÒÃÇÖ»ÐèÒªÔÚ¸ÃÎļþÖÐÌí¼ÓÒÔÏ´úÂë¼´¿É£º
<?php // ×Ô½ç˵ÃüÃû¿Õ¼äµÄ×Ô¶¯¼ÓÔØ // µ±»á¼ûµÄÀàÔÚappcommonÃüÃû¿Õ¼äÏÂʱ£¬×Ô¶¯È¥commonĿ¼Ï²éÕÒÏìÓ¦µÄÎļþ hinkLoader::addNamespace('common', APP_PATH.'common/');
µÇ¼ºó¸´ÖÆ
ÕâÑùÒ»À´£¬µ±ÎÒÃÇÔÚ¿ØÖÆÆ÷»òÄ£×ÓÖÐʹÓÃ×Ô½ç˵ÃüÃûʱ£¬¾Í¿ÉÒÔ×Ô¶¯¼ÓÔØÏìÓ¦µÄÎļþÁË¡£ÀýÈ磺
<?php namespace appindexcontroller; use appcommoncontrollerBaseController; class IndexController extends BaseController{ // ... }
µÇ¼ºó¸´ÖÆ
<?php namespace appindexcontroller; use appcommonmodelUserModel; class UserController{ public function index(){ $user = UserModel::get(1); // ... } }
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇ×Ô½ç˵ÃüÃûµÄÒªÁìÁË¡£×Ô½ç˵ÃüÃû¿ÉÒÔÓÐÓõØ×éÖ¯ºÍÖÎÀíÎÒÃǵĴúÂ룬Ìá¸ß´úÂëµÄ¿Éά»¤ÐԺͿɶÁÐÔ¡£
ÒÔÉϾÍÊÇthinkphpÔõô×Ô½ç˵ÃüÃûµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡