thinkphp5.0ÔõôÔöɾ²é¸Ä
Ò»¡¢Ð¡Ðò
ThinkPHPÊÇÒ»¿î¿ªÔ´µÄPHP¿ª·¢¿ò¼Ü£¬ÖÂÁ¦ÓÚΪ¿ª·¢Õß¹¹½¨¸ßЧ¡¢Îȹ̡¢Çå¾²ÇÒÒ×ÓÚÀ©Õ¹µÄWEBÓ¦ÓÃÌṩ×ÊÖú¡£thinkPHP ¿ò¼Ü°æ±¾Éý¼¶ºÍÐÂÔö¹¦Ð§ÊDz»¿É×èÖ¹µÄ£¬ÏÂÃæÎÒ½«Í¨¹ýÏÈÈÝÔõÑùʹÓÃTP5.0ʵÏÖÔöɾ²é¸Ä¹¦Ð§£¬Ï£ÍûÄܹ»¶Ô¸÷ÈËÓÐËù×ÊÖú¡£
¶þ¡¢ÊµÏÖÔöɾ²é¸ÄµÄ°ì·¨
×¼±¸Ê¾ÀýÊý¾Ý¿âºÍ±í
Ê×ÏÈ£¬ÐèҪ׼±¸Ò»¸öʾÀýÊý¾Ý¿âºÍ±í£¬ÒÔÏÂÊÇÒ»¸öʾÀý±í£º
CREATE TABLE think_user (
id int(11) NOT NULL AUTO_INCREMENT COMMENT ‘Óû§id’,
name varchar(255) DEFAULT NULL COMMENT ‘Óû§êdzƒ,
email varchar(255) DEFAULT NULL COMMENT ‘Óû§ÓÊÏä’,
status tinyint(4) DEFAULT ‘1’ COMMENT ‘Óû§×´Ì¬’,
create_time datetime DEFAULT NULL COMMENT ‘½¨Éèʱ¼ä’,
update_time datetime DEFAULT NULL COMMENT ‘¸üÐÂʱ¼ä’,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=’Óû§ÐÅÏ¢±í’;
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
½¨ÉèÓû§Ä£×ӺͿØÖÆÆ÷
Ê×ÏÈÐèҪͨ¹ýÌìÉúÏÂÁîÀ´½¨ÉèÓû§Ä£×ӺͿØÖÆÆ÷£º
php think make:model User
php think make:controller User
¿É²Î¿¼ÏÂÁîÎĵµ£ºhttps://www.kancloud.cn/manual/thinkphp5_1/353947
ʵÏÖÓû§Áбí
ÔÚÓû§¿ØÖÆÆ÷ÖУ¬ÎÒÃÇÐèҪʵÏÖÓû§ÁбíµÄչʾ£¬ÊµÏÖ´úÂëÈçÏ£º
public function index()
{
$users = UserModel::all(); $this->assign('users', $users); return $this->fetch();
µÇ¼ºó¸´ÖÆ
}
ÆäÖУ¬UserModelΪÔÚµÚ2²½Öн¨ÉèµÄÓÃÓÚ²Ù×÷Óû§±íµÄÄ£×Ó£¬$usersÌåÏÖ´Óuser±íÖлñÈ¡µÄËùÓÐÓû§ÐÅÏ¢£¬$this->assign()ÓÃÓÚÏòÄ£°åÖÐת´ï±äÁ¿£¬×îºóͨ¹ý$this->fetch()ÒªÁìäÖȾģ°å¡£
ʵÏÖÓû§Ìí¼Ó
Óû§Ìí¼ÓµÄʵÏÖÏà¶Ô½Ï¼òÆÓ£¬ÈçÏÂÒ»¸öʵÏֵIJο¼´úÂ룺
public function add()
{
if (Request::instance()->isPost()) { $user = new UserModel; $user->name = input('name'); $user->email = input('email'); if ($user->save()) { return $this->success('Óû§Ìí¼ÓÀֳɣ¡', url('index')); } else { return $this->error('Óû§Ìí¼Óʧ°Ü£¡'); } } return $this->fetch();
µÇ¼ºó¸´ÖÆ
}
ʵÏÖÓû§±à¼
Óû§±à¼µÄʵÏÖÐèÒª¸ü¶àµÄ×¢ÖØ£¬ÏêϸʵÏÖ´úÂëÈçÏ£º
public function edit($id)
{
$user = UserModel::get($id); if (Request::instance()->isPost()) { $user->name = input('name'); $user->email = input('email'); if ($user->save()) { return $this->success('Óû§¸üÐÂÀֳɣ¡', url('index')); } else { return $this->error('Óû§¸üÐÂʧ°Ü£¡'); } } $this->assign('user', $user); return $this->fetch();
µÇ¼ºó¸´ÖÆ
}
ÆäÖУ¬$id´ú±í±à¼µÄÓû§Id£¬UserModel::get($id)ÓÃÓÚ»ñÈ¡¶ÔÓ¦µÄÓû§ÐÅÏ¢£¬$this->assign()ÓÃÓÚ´«Öµ¡£
ʵÏÖÓû§É¾³ý
Óû§É¾³ýÐèÒªÉóÉ÷²Ù×÷£¬ÒÔÏÂÊÇÒ»¸ö»ù±¾µÄɾ³ýʵÏÖ£º
public function delete($id)
{
$user = UserModel::get($id); if ($user) { $user->delete(); return $this->success('Óû§É¾³ýÀֳɣ¡', url('index')); } else { return $this->error('Óû§²»±£´æ£¡'); }
µÇ¼ºó¸´ÖÆ
}
´úÂë²âÊÔ
ÒÔÉÏ´úÂëÒѾʵÏÖÁËÓû§ÖÎÀíµÄ»ù±¾¹¦Ð§£¬¿ÉÒÔͨ¹ý»á¼ûÒÔÏÂURL²âÊÔ´úÂ룺
»á¼û /user/index ¿ÉÒÔÉó²éµ½Óû§Áбí
»á¼û /user/add ¿ÉÒÔÌí¼ÓеÄÓû§
»á¼û /user/edit/id ±à¼Ö¸¶¨IDµÄÓû§
»á¼û /user/delete/id ɾ³ýÖ¸¶¨IDµÄÓû§
Èý¡¢×ܽá
ÔÚTP5.0ÖÐʵÏÖÔöɾ²é¸ÄµÄ¹¦Ð§£¬¿ÉÒÔ¼ÓËÙ¿ª·¢Ö°Ô±µÄ¿ª·¢Ð§ÂÊ¡£Í¨¹ýÒÔÉϵÄʵÏÖ°ì·¨£¬¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±ÊµÏÖ¼òÆÓµÄÔöɾ²é¸Ä¹¦Ð§£¬Í¬Ê±Ò²¿ÉÒԲο¼TP5.0¹Ù·½ÎĵµÀ´¸üÉîÈëµØÏàʶTP5.0¿ò¼Ü¡£
ÒÔÉϾÍÊÇthinkphp5.0ÔõôÔöɾ²é¸ÄµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡