×ðÁú¿­Ê±¹ÙÍøµÇ¼

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Ôõô×Ô½ç˵ÃüÃûµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ