thinkphpÔõÑùʵÏÖ
thinkphpÊÇÒ»¸ö¿ªÔ´µÄphp¿ª·¢¿ò¼Ü£¬ËüÊܵ½ÁËÐí¶àphp³ÌÐòÔ±µÄ×·ÅõºÍʹÓá£Ëü×÷Ϊһ¸ö³ÉÊì¶øÓÖÎȹ̵Ŀò¼Ü£¬ÎªÎÒÃÇÌṩÁ˺ÜÊÇÇ¿Ê¢µÄ¹¤¾ßºÍ¿ª·¢×ÊÔ´¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphp¿ò¼ÜÖÐʵÏÖһЩ³£¼ûµÄ¹¦Ð§¡£
Ò»¡¢ÔõÑù½¨Éè¿ØÖÆÆ÷
ÔÚThinkPHPÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÈçÏÂÏÂÁîÀ´½¨ÉèÒ»¸ö¿ØÖÆÆ÷£º
php think make:controller Index
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁÔÚÓ¦ÓÃĿ¼ÏµÄcontrollerĿ¼Öн¨ÉèÒ»¸öÃûΪIndexµÄ¿ØÖÆÆ÷¡£ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷Öнç˵ÏêϸµÄÓªÒµÂ߼ʵÏÖ¡£
¶þ¡¢ÔõÑù½¨ÉèÄ£×Ó
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚThinkPHPÖУ¬Ä£×ÓÊÇÓÃÓÚ´¦ÀíÊý¾Ý¿â²Ù×÷µÄ¡£ÎÒÃÇ¿ÉÒÔʹÓÃÈçÏÂÏÂÁîÀ´½¨ÉèÒ»¸öÄ£×Ó£º
php think make:model User
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁÔÚÓ¦ÓÃĿ¼ÏµÄmodelĿ¼Öн¨ÉèÒ»¸öÃûΪUserµÄÄ£×Ó¡£ÎÒÃÇ¿ÉÒÔÔÚÄ£×ÓÖнç˵ÏêϸÊý¾Ý¿â²Ù×÷ʵÏÖ¡£
Èý¡¢ÔõÑù½¨ÉèÊÓͼ
ÔÚThinkPHPÖУ¬ÊÓͼÓÃÓÚչʾÊý¾ÝºÍÍê³ÉÓû§½»»¥¡£ÎÒÃÇ¿ÉÒÔʹÓÃÈçÏÂÏÂÁîÀ´½¨ÉèÒ»¸öÊÓͼ£º
php think make:view Index/index
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁÔÚÓ¦ÓÃĿ¼ÏµÄviewĿ¼Öн¨ÉèÒ»¸öÃûΪindex.htmlµÄÊÓͼ¡£ÔÚÕâ¸öÊÓͼÖУ¬ÎÒÃÇ¿ÉÒÔ½ç˵ÏêϸµÄÒ³ÃæչʾЧ¹ûºÍ½»»¥Ê½ÔªËØ¡£
ËÄ¡¢ÔõÑù½ç˵·ÓÉ
ÔÚThinkPHPÖУ¬ÎÒÃÇ¿ÉÒÔʹÓ÷ÓÉÀ´½ç˵URL»á¿´·¨Ö·Óë¿ØÖÆÆ÷ÒªÁìµÄÓ³Éä¹Øϵ¡£ÎÒÃÇ¿ÉÒÔÔÚÓ¦ÓÃĿ¼ÏµÄrouteĿ¼Öн¨ÉèÒ»¸öroute.phpÎļþ£¬²¢Ìí¼ÓÈçÏÂÄÚÈÝ£º
<?php use thinkacadeRoute; Route::get('user/:id', 'index/user');
µÇ¼ºó¸´ÖÆ
¸Ã´úÂë½ç˵ÁËÒ»¸öURLÓ³Éä¹Øϵ£¬½«URL/user/10Ó³Éäµ½index¿ØÖÆÆ÷ÖеÄuserÒªÁ죬ÆäÖÐ10ÊÇÓû§ID²ÎÊý¡£
Îå¡¢ÔõÑùʹÓÃÖÐÑë¼þ
ÔÚThinkPHPÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÖÐÑë¼þÀ´¾ÙÐÐ×èµ²¡¢Ð£ÑéºÍÉèÖá£ÎÒÃÇ¿ÉÒÔÔÚÓ¦ÓÃĿ¼ÏµÄmiddlewareĿ¼Öн¨ÉèÒ»¸öÃûΪTestMiddlewareµÄÖÐÑë¼þ£¬²¢Ìí¼ÓÈçÏ´úÂ룺
<?php namespace appmiddleware; class TestMiddleware { public function handle($request, Closure $next) { // ÖÐÑë¼þÂß¼´¦Àí return $next($request); } }
µÇ¼ºó¸´ÖÆ
¸Ã´úÂë½ç˵ÁËÒ»¸öÃûΪTestMiddlewareµÄÖÐÑë¼þ£¬ÔÚÖ´ÐзÓɲÙ×÷ʱ£¬½«»áÏÈÖ´ÐиÃÖÐÑë¼þµÄhandleÒªÁì¡£
Áù¡¢ÔõÑù¾ÙÐÐÊý¾Ý¿â²Ù×÷
ÔÚThinkPHPÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃDbÀàÀ´¾ÙÐÐMySQLÊý¾Ý¿âµÄ²Ù×÷¡£ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷»òÄ£×ÓÖÐÌí¼ÓÈçÏ´úÂ룺
<?php namespace appcontroller; use thinkacadeDb; class User { public function getUser($id) { return Db::table('user') ->where('id', $id) ->find(); } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂë½ç˵ÁËÒ»¸ö¿ØÖÆÆ÷ÒªÁ죬ÓÃÓÚ´ÓÊý¾Ý¿âÖлñÈ¡Óû§ÐÅÏ¢¡£
Æß¡¢ÔõÑùʹÓûº´æ
ÔÚThinkPHPÖУ¬ÎÒÃÇ¿ÉÒÔʹÓûº´æÀ´Ìá¸ß³ÌÐòµÄÐÔÄܺÍÓÅ»¯¡£ÎÒÃÇ¿ÉÒÔʹÓÃcacheº¯ÊýÀ´¾ÙÐлº´æ²Ù×÷£º
cache('user_'.$id, $user);
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÌåÏÖ½«$user¹¤¾ß»º´æµ½Ãû³ÆΪuser_10µÄ»º´æÖС£
°Ë¡¢ÔõÑùʹÓÃÈÕÖ¾
ÔÚThinkPHPÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÈÕÖ¾¼Í¼³ÌÐòÔËÐÐÀú³ÌÖеÄÐÅÏ¢ºÍÒì³£¡£ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷»òÄ£×ÓÖÐÌí¼ÓÈçÏ´úÂ룺
<?php namespace appcontroller; use thinkacadeLog; class User { public function getUser($id) { Log::info('ÅÌÎÊÓû§ÐÅÏ¢ÀÖ³É'); return Db::table('user') ->where('id', $id) ->find(); } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÌåÏÖÔÚ¿ØÖÆÆ÷ÖÐÅÌÎÊÓû§ÐÅÏ¢µÄͬʱ£¬¼Í¼һÌõÃûΪinfoµÄÈÕÖ¾ÄÚÈÝ¡£
×ÛÉÏËùÊö£¬ÒÔÉÏÊÇһЩ³£ÓõÄThinkPHP¿ò¼Ü²Ù×÷¡£ÔÚʵ¼ùÖУ¬¿ÉÒÔƾ֤ÏêϸÐèÇóÏìÓ¦µ÷½âºÍÐÞÕý¡£Ð»Ð»Äú¶ÔThinkPHP¿ò¼ÜµÄÖ§³ÖºÍʹÓá£
ÒÔÉϾÍÊÇthinkphpÔõÑùʵÏÖµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡