Ôõô¿´¶®thinkphp5ÏîÄ¿
thinkphp 5ÊÇÒ»¸ö¼òÆÓÒ×ѧ£¬¹¦Ð§Ç¿Ê¢µÄphp¿ª·¢¿ò¼Ü£¬ÒѾ³ÉΪÁËÖÚ¶àphp¿ª·¢ÕßµÄÊ×Ñ¡¿ò¼ÜÖ®Ò»¡£¿ÉÊÇÔõÑù¿´¶®thinkphp 5µÄÏîÄ¿ÄØ£¿ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ì½ÌÖÔõÑù¸üºÃµØÃ÷È·thinkphp 5µÄÏîÄ¿ÒÔ¼°ÔõÑù¸ü¿ìµØÕÆÎÕËü¡£
Ò»¡¢ÏàʶThinkPHP 5µÄĿ¼½á¹¹
ÔÚ×îÏÈѧϰÈκÎÒ»¸ö¿ò¼Ü֮ǰ£¬ÏàʶĿ¼½á¹¹ÊÇÊ®·ÖÐëÒªµÄ¡£ThinkPHP 5µÄĿ¼½á¹¹Ïà¶ÔÀ´ËÃ÷ÈÕվɽÏÁ¿ÇåÎúµÄ¡£¿ª·¢ÕßÖ»ÐèÒªÏàʶÆäÖеĽ¹µãĿ¼½á¹¹¼´¿É¡£ÏÂÃæÊÇThinkPHP 5µÄĿ¼½á¹¹£º
public չʾ¸øÓû§µÄ¾²Ì¬ÎļþĿ¼ application Ä£¿éÎļþ¼Ð ©À©¤index ǰ̨ģ¿é ©¦ ©À©¤controller ¿ØÖÆÆ÷ ©¦ ©À©¤model Ä£×Ó ©¦ ©¸©¤view Ä£°å ©À©¤admin ºǫ́ģ¿é ©¦ ©À©¤controller ¿ØÖÆÆ÷ ©¦ ©À©¤model Ä£×Ó ©¦ ©¸©¤view Ä£°å ©À©¤common ¹«¹²Ä£¿é ©¦ ©À©¤controller ¿ØÖÆÆ÷ ©¦ ©À©¤model Ä£×Ó ©¦ ©¸©¤view Ä£°å runtime ÔËÐÐʱĿ¼£¬´æ·ÅÈÕÖ¾¡¢»º´æµÈÔËÐÐʱÎļþ thinkphp ThinkPHP¿ò¼Ü½¹µã´úÂëĿ¼
µÇ¼ºó¸´ÖÆ
¶þ¡¢ÕÆÎÕThinkPHP 5µÄ·ÓÉ
ÔÚThinkPHP 5ÖУ¬Â·Óɹ¦Ð§ºÜÊÇÇ¿Ê¢¡£ÕÆÎÕ·Óɹ¦Ð§¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±Ô½·¢ÎÞаµØÉèÖÃÍøÕ¾»á¼û·¾¶¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÏñÏÂÃæÕâÑùÔÚconfigÎļþ¼ÐϵÄroute.phpÖÐÉèÖ÷ÓÉ£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
return [ 'user/:id' => 'user/detail', //»á¼û/user/5µÄʱ¼ä»áÌøתµ½user¿ØÖÆÆ÷µÄdetailÒªÁì 'blog/:year/:month' => 'blog/archive', //»á¼û/blog/2019/10µÄʱ¼ä»áÌøתµ½blog¿ØÖÆÆ÷µÄarchiveÒªÁì 'list-<id>-<page>' => 'article/index', //»á¼û/list-10-2µÄʱ¼ä»áÌøתµ½article¿ØÖÆÆ÷µÄindexÒªÁì ];
µÇ¼ºó¸´ÖÆ
Èý¡¢ÏàʶThinkPHP 5µÄ¿ØÖÆÆ÷
ÔÚThinkPHP 5ÖУ¬¿ØÖÆÆ÷ÊÇMVC¿ò¼ÜÖеÄC£¨Controller£©²¿·Ö¡£¿ØÖÆÆ÷ÊÇ´¦ÀíÇëÇóÂß¼µÄµØ·½£¬Í¨³£ÔÚ¿ØÖÆÆ÷ÖÐŲÓÃÄ£×ÓºÍÊÓͼµÈÆäËû¹¦Ð§ÊµÏÖÌض¨ÓªÒµÂß¼¡£
ÔÚ¿ØÖÆÆ÷ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃ$requestÀ´»ñÈ¡GET¡¢POST¡¢PUTµÈת´ïµÄ²ÎÊý£¬Ê¹ÓÃ$responseÀ´ÉèÖÃHTTPÏìӦͷºÍÄÚÈÝ¡£ÀýÈ磺
namespace appindexcontroller; use thinkController; use thinkRequest; class Index extends Controller { public function index(Request $request) { $name = $request->param('name'); $this->assign('name', $name); return $this->fetch(); } }
µÇ¼ºó¸´ÖÆ
ËÄ¡¢Ñ§Ï°ThinkPHP 5µÄÄ£×Ó
ÔÚThinkPHP 5ÖУ¬Ä£×ÓÊÇMVC¿ò¼ÜÖеÄM£¨Model£©²¿·Ö¡£Ä£×ÓÊÇÓÃÀ´²Ù×÷Êý¾Ý¿âµÄ¡£Í¨¹ýÄ£×Ó£¬ÎÒÃÇ¿ÉÒÔºÜÀû±ãµØ¶ÔÊý¾Ý¿â¾ÙÐÐÔöɾ¸Ä²é²Ù×÷¡£
ThinkPHP 5µÄÄ£×Ó¿ÉÒÔͨ¹ýORM·½·¨¾ÙÐвÙ×÷¡£ORMÊÇ”¹¤¾ß-¹ØϵӳÉ䔵ÄËõд£¬ÓÃÓÚʵÏÖ¹ØϵÐÍÊý¾Ý¿âµÄ¹¤¾ß»¯²Ù×÷¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÄ£×ÓʾÀý£º
namespace appindexmodel; use thinkModel; class User extends Model { public function getUserList() { return $this->field('id,name,email')->select(); } }
µÇ¼ºó¸´ÖÆ
Îå¡¢Ã÷È·ThinkPHP 5µÄÊÓͼ
ÔÚThinkPHP 5ÖУ¬ÊÓͼÊÇMVC¿ò¼ÜÖеÄV£¨View£©²¿·Ö¡£ÊÓͼÈÏÕæÏÔʾģ°åºÍÊý¾Ý¡£
ThinkPHP 5µÄÊÓͼͨ³£ÓÃÓÚ¹¹½¨HTML´úÂëºÍÏÔʾÊý¾Ý¿âÖлñÈ¡µÄÐÅÏ¢¡£ÊÓͼͨ³£´æ´¢ÔÚÄ£¿éµÄviewĿ¼Ï¡£ÊÓͼ¿ÉÒÔʹÓÃ{}±ê¼ÇÊä³ö±äÁ¿£¬Ê¹ÓÃforeach£¬ifµÈ±ê¼ÇʵÏÖÂß¼¿ØÖÆ¡£
Áù¡¢Ê¹ÓÃThinkPHP 5µÄÖúÊÖº¯Êý
ThinkPHP 5ÄÚÖÃÁËÐí¶àÓÐÓõÄÖúÊÖº¯Êý£¬¿ÉÒÔÓÃÀ´Àû±ãµØÍê³ÉÐí¶àÊÂÇé¡£ÀýÈ磺
input() ÖúÊÖº¯ÊýÓÃÀ´»ñÈ¡Óû§ÊäÈëÊý¾Ý£¬°üÀ¨ GET¡¢POST ºÍ PUT µÈÇëÇóÒªÁìµÄÊý¾Ý¡£
$name = input('post.name');
µÇ¼ºó¸´ÖÆ
config() ÖúÊÖº¯ÊýÓÃÀ´»ñȡϵͳÉèÖã¬ÀýÈçÊý¾Ý¿â¡¢»º´æµÈÉèÖá£
$database = config('database');
µÇ¼ºó¸´ÖÆ
session() ÖúÊÖº¯ÊýÓÃÀ´ÉèÖûò»ñÈ¡ Session Öµ¡£
session('name', 'thinkphp'); $name = session('name');
µÇ¼ºó¸´ÖÆ
Æß¡¢ÕÆÎÕThinkPHP 5µÄ»º´æ»úÖÆ
»º´æÊÇÇ°¶Ë»òЧÀÍÆ÷½«Êý¾ÝÔÝʱ´æ´¢ÆðÀ´£¬ÒÔ±ãÏ´λá¼ûʱÄܹ»¸ü¿ìµØ»ñÈ¡Êý¾Ý¡£ÔÚThinkPHP 5ÖУ¬»º´æÌṩÁ˶àÖÖ´æ´¢·½·¨£¬°üÀ¨Îļþ¡¢Memcache¡¢RedisµÈ¡£
»º´æ»úÖÆ¿ÉÒÔ×ÊÖúÎÒÃÇÓÅ»¯³ÌÐòµÄÐÔÄÜ£¬Ìá¸ß»á¼ûËÙÂÊ¡£ÏÂÃæÊÇÒ»¸ö¼òÆӵĻº´æʾÀý£º
use thinkCache; $cache = Cache::get('user_1'); if (!$cache) { $user_info = User::where('id', 1)->find(); $cache = Cache::set('user_1', $user_info, 3600); }
µÇ¼ºó¸´ÖÆ
ÉÏÃæ´úÂëÖУ¬ÎÒÃÇÊ×ÏÈʵÑé´Ó»º´æÖлñÈ¡Óû§ÐÅÏ¢£¬ÈôÊÇ»º´æ²»±£´æ£¬Ôò´ÓÊý¾Ý¿âÖлñÊØÐÅÏ¢£¬²¢½«Æä´æÈ뻺´æÖС£
°Ë¡¢Ñ§Ï°ThinkPHP 5µÄ¹ýʧµ÷ÊÔ
ÔÚ¿ª·¢Àú³ÌÖУ¬¾³£»áÓöµ½³ÌÐòÍÉ»¯µÄÇéÐΡ£ÔÚÕâÖÖÇéÐÎÏ£¬ÎÒÃÇÐèҪʵʱµÄÕÒµ½¹ýʧ²¢½â¾ö£¬Õâ¾ÍÐèÒªÔËÓÃThinkPHP 5µÄ¹ýʧµ÷ÊÔ¹¦Ð§¡£
ÔÚThinkPHP 5ÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖÃÎļþÀ´¿ªÆô/¹Ø±Õ¹ýʧµ÷ÊÔ¡£µ±ÎÒÃÇ¿ªÆôÁ˵÷ÊÔ¹¦Ð§Ê±£¬ÏµÍ³»á×Ô¶¯ÔÚÒ³ÃæÉÏÊäÍÉ»¯ÎóÐÅÏ¢ºÍŲÓÃÕ»£¬×ÊÖúÎÒÃÇÆÊÎöÏ¢Õù¾öÎÊÌâ¡£ÀýÈ磺
//ÔÚconfigĿ¼ÏµÄapp.phpÎļþÖÐÉèÖà 'debug' => true,
µÇ¼ºó¸´ÖÆ
×ܽá
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÎÒÃÇÏàʶÁËÔõÑù¸üºÃµØÃ÷È·ThinkPHP 5µÄÏîÄ¿ÒÔ¼°ÔõÑù¸ü¿ìµØÕÆÎÕËü¡£ËäÈ»ÕâÖ»ÊÇÒ»¸ö×îÏÈ£¬ÏëÒª³¹µ×ÕÆÎÕThinkPHP 5£¬¾ÍÐèÒªÎÒÃÇһֱѧϰ¡¢Ñо¿¡¢Êµ¼ù¡£Í¬Ê±£¬ÎÒÃÇÒ²Òª¹Ø×¢¹Ù·½ÎĵµºÍÉçÇø£¬Ïàʶ×îеÄÊÖÒպͿª·¢¶¯Ì¬£¬Ò»Ö±Ìá¸ß×Ô¼ºµÄ±à³Ìˮƽ¡£
ÒÔÉϾÍÊÇÔõô¿´¶®thinkphp5ÏîÄ¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡