ÔõÑùʹÓÃ×Ô½ç˵º¯Êý¿âÀ©Õ¹ThinkPHP6£¿
thinkphp6ÊÇÒ»¸öºÜÊÇÊ¢ÐеÄphp¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÐí¶àÏÖ´ú»¯µÄÌØÕ÷ºÍ¹¤¾ß£¬Ê¹µÃ¿ª·¢Õß¿ÉÒÔ¸ü¸ßЧµØ¹¹½¨webÓ¦ÓóÌÐò¡£ÆäÖÐÒ»¸öºÜÊÇÇ¿Ê¢µÄ¹¦Ð§ÊÇ×Ô½ç˵º¯Êý¿â£¬ËüÔÊÐí½«Öظ´Ê¹ÓõĴúÂë·â×°ÔÚÒ»¸öº¯Êý¿âÖУ¬Ê¹¿ª·¢ºÍά»¤ÊÂÇé±äµÃÔ½·¢¼òÆӺͿìËÙ¡£±¾ÎĽ«ÏÈÈÝÄúÔõÑùʹÓÃ×Ô½ç˵º¯Êý¿âÀ©Õ¹thinkphp6¡£
½¨Éè×Ô½ç˵º¯Êý¿â
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸ö×Ô½ç˵º¯Êý¿â¡£ÔÚThinkPHP6ÖУ¬Äú¿ÉÒÔʹÓÃcomposerÀ´½¨Éè×Ô¼ºµÄº¯Êý¿â¡£ÔÚÏÂÁîÐÐÖУ¬ÊäÈëÒÔÏÂÏÂÁ
composer init
µÇ¼ºó¸´ÖÆ
Õ⽫ָµ¼ÄúÉèÖÃ×Ô½ç˵º¯Êý¿âµÄÃû³Æ¡¢°æ±¾¡¢×÷ÕßµÈÐÅÏ¢¡£Íê³Éºó£¬Äú½«»á»ñµÃÒ»¸öcomposer.jsonÎļþ£¬ÕâÊÇÒ»¸öÐÎòÄúº¯Êý¿âµÄJSONÎļþ¡£ÔÚ´ËÎļþÖУ¬Äú¿ÉÒÔ½ç˵ÒÀÀµ¹Øϵ¡¢×Ô¶¯¼ÓÔعæÔòµÈµÈ¡£
×îÖ÷ÒªµÄÊÇ£¬ÔÚcomposer.jsonÎļþÖеÄautoload²¿·Ö£¬ÄúÐèÒª½ç˵×Ô½ç˵º¯Êý¿âµÄÃüÃû¿Õ¼ä¡£ÀýÈ磺
"autoload": { "psr-4": { "MyLib\": "src/" } }
µÇ¼ºó¸´ÖÆ
Õ⽫ÉèÖÃMyLibÃüÃû¿Õ¼äµÄËùÓÐÀ඼ÔÚsrcĿ¼ÖС£ÇëÈ·±£ÔÚsrcĿ¼Öн¨ÉèÒ»¸öÃüÃûΪFunctions.phpµÄÎļþ£¬²¢ÔÚ´ËÎļþÖнç˵Äú×Ô¼ºµÄº¯Êý¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
×°ÖÃ×Ô½ç˵º¯Êý¿â
ÒªÔÚÄúµÄThinkPHP6Ó¦ÓóÌÐòÖÐʹÓÃ×Ô½ç˵º¯Êý¿â£¬ÄúÐèÒªÏȽ«Æä×°Öõ½ÄúµÄÏîÄ¿ÖС£ÔÚÄúµÄÓ¦ÓóÌÐòµÄ¸ùĿ¼Ï£¬ÊäÈëÒÔÏÂÏÂÁ
composer require yourcomposername/mylib
µÇ¼ºó¸´ÖÆ
Õ⽫´ÓPackagistÉÏ×°ÖÃÄú½¨ÉèµÄº¯Êý¿â¡£
½ÓÏÂÀ´£¬ÄúÐèÒªÔÚÓ¦ÓóÌÐòµÄconfig/autoloadĿ¼Öн¨ÉèÒ»¸öyourcomposername.phpÎļþ£¨×¢ÖØÌæ»»yourcomposernameΪÄúµÄº¯Êý¿âµÄÃû×Ö£©£¬²¢ÔÚ´ËÎļþÖÐÌí¼ÓÒÔÏÂÐУº
<?php // yourcomposername.php // ¼ÓÔØ×Ô½ç˵º¯Êý¿â require_once 'vendor/autoload.php'; // ×¢²áMyLibÃüÃû¿Õ¼ä MyLibFunctions::register();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫ÔÚÓ¦ÓóÌÐòÆô¶¯Ê±¼ÓÔØ×Ô½ç˵º¯Êý¿â£¬²¢ÇÒÔÚÓ¦ÓóÌÐòÖÐ×¢²áMyLibÃüÃû¿Õ¼ä¡£
ʹÓÃ×Ô½ç˵º¯Êý¿â
ÏÖÔÚÄúÒѾÀֳɽ¨Éè²¢×°ÖÃÁË×Ô½ç˵º¯Êý¿â£¬²¢ÔÚThinkPHP6ÖÐ×¢²áÁËËü£¬½ÓÏÂÀ´Äú¿ÉÒÔÔÚÓ¦ÓóÌÐòÖÐʹÓÃËüÁË¡£ÀýÈ磬ÈôÊÇÄúÏëÔÚ¿ØÖÆÆ÷ÖÐʹÓÃÄúµÄ×Ô½ç˵º¯Êý£¬Ö»Ðèµ¼ÈëÄú½ç˵µÄÃüÃû¿Õ¼ä²¢Å²ÓÃÆ亯Êý¡£ÀýÈ磺
<?php namespace appcontroller; use MyLibFunctions; class TestController { public function index() { $result = Functions::myCustomFunction($arg1, $arg2); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǵ¼ÈëÁËMyLibÃüÃû¿Õ¼ä£¬²¢Ê¹ÓÃmyCustomFunctionº¯ÊýÀ´Ö´ÐÐ×Ô½ç˵º¯Êý¿âÖеĴúÂë¡£Äú¿ÉÒÔƾ֤Õâ¸öģʽÀ´Ê¹ÓÃÄúµÄ×Ô½ç˵º¯Êý¿â¡£
×ܽá
ʹÓÃ×Ô½ç˵º¯Êý¿â¿ÉÒÔ´ó´ó¼ò»¯´úÂëÊÂÇ飬²¢ÇÒʹµÃ¿ª·¢ºÍά»¤Àú³Ì±äµÃÔ½·¢¼òÆÓ¡£ThinkPHP6µÄ×Ô½ç˵º¯Êý¿â¹¦Ð§ºÜÊÇÇ¿Ê¢£¬ÈÃÄú¿ÉÒÔ·â×°´úÂë²¢ÔÚÓ¦ÓóÌÐòÖÐÖظ´Ê¹Óã¬ïÔÌÖظ´ÐÔÊÂÇé¡£ÈôÊÇÄú×ñÕÕÒÔÉÏ°ì·¨£¬¾Í¿ÉÒÔÔÚThinkPHP6ÖÐÀֳɽ¨É貢ʹÓÃ×Ô½ç˵º¯Êý¿âÁË¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃ×Ô½ç˵º¯Êý¿âÀ©Õ¹ThinkPHP6£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡