thinkphp¶àѡɾ³ý
thinkphp6ÊÇÒ»¿îºÜÊÇÓÅÒìµÄphp¿ª·¢¿ò¼Ü£¬Ëü»ùÓÚmvc¼Ü¹¹£¬¿ª·¢Ð§Âʸߡ¢Ò×ÉÏÊÖ¡¢Çå¾²¿É¿¿£¬Í¬Ê±¼«´ó¼ò»¯ÁË´úÂë±àдµÄÁ÷³Ì¡£½ñÌ죬ÎÒÃÇÀ´Ì¸Ò»ÏÂʹÓÃthinkphp6ʵÏÖ¶àѡɾ³ýµÄÒªÁì¡£
Ò»¡¢Ç°ÑÔ
¶àѡɾ³ýÊÇÏÖ´úWebÓ¦ÓóÌÐòµÄ³£¼û¹¦Ð§Ö®Ò»¡£ËüÔÊÐíÓû§ÔÚÊý¾Ý±í¸ñ»òÁбíÖÐÑ¡Ôñ¶à¸öÏîÄ¿£¬È»ºó¿ÉÒÔɾ³ýËüÃÇ¡£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃthinkphp6µÄ¿ØÖÆÆ÷ºÍÄ£×ÓÀ´ÊµÏÖÕâ¸ö¹¦Ð§¡£
¶þ¡¢½¨ÉèÊý¾Ý¿â
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚMySQLÊý¾Ý¿âÖн¨ÉèÒ»¸öʾÀý±í¡£ÎÒÃǽ«Ê¹ÓÃÏÂÁîÐй¤¾ß»òphpMyAdminÀ´Íê³ÉÕâ¸ö°ì·¨¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
CREATE TABLE example_table (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
email varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Õ⽫½¨ÉèÒ»¸öÃûΪ¡° example_table¡±µÄÐÂ±í£¬ÆäÖаüÀ¨id£¬nameºÍemail×ֶΡ£
Èý¡¢ÌìÉúÄ£×ӺͿØÖÆÆ÷
½ÓÏÂÀ´£¬ÎÒÃÇÐèҪƾ֤ÉÏÃ潨ÉèµÄ±í½á¹¹À´ÌìÉúÒ»¸öÄ£×ÓºÍÒ»¸ö¿ØÖÆÆ÷¡£
ʹÓÃthinkphp6µÄArtisanÏÂÁîÐй¤¾ß¿ÉÒÔ¿ìËÙÌìÉúÕâЩÎļþ£º
php think make:model ExampleModel –migration
ÔËÐÐÉÏÊöÏÂÁÌìÉúÒ»¸öеÄÄ£×ÓÎļþ£¨ app /Model/ExampleModel.php £©ºÍÒ»¸öеÄÊý¾Ý¿âǨáãÎļþ£¨ database /migrations /yyyymmddhhmmss_create_example_model.php £©¡£
php think make:controller ExampleController
ÔËÐÐÉÏÊöÏÂÁÌìÉúÒ»¸öÃûΪExampleController.phpµÄÐÂÎļþ£¨ app /Controller/ExampleController.php £©¡£
ËÄ¡¢±àд´úÂë
ÏÖÔÚÎÒÃÇÒѾ׼±¸ºÃ±àд¿ØÖÆÆ÷´úÂëÀ´´¦Àí¶àѡɾ³ýÁË¡£ÔÚ¿ØÖÆÆ÷ÖУ¬ÎÒÃÇÐèҪʵÏÖÁ½¸öÖ÷ÒªµÄactionº¯Êý£ºindex£¨ÓÃÓÚÏÔʾËùÓÐÊý¾Ý£©ºÍdelete£¨ÓÃÓÚ´¦Àíɾ³ýÇëÇ󣩡£
ÔÚapp /Controller /ExampleController.phpÎļþÖУ¬Ìí¼ÓÒÔÏ´úÂ룺
<?php namespace appcontroller; use thinkacadeView; use thinkRequest; use appmodelExampleModel; class ExampleController { public function index() { $list = ExampleModel::all(); return View::fetch('index', [ 'list' => $list, ]); } public function delete(Request $request) { $ids = $request->param('ids'); foreach ($ids as $id) { ExampleModel::destroy($id); } return ['status' => 'success', 'message' => 'ɾ³ýÀÖ³É']; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÊ×Ïȵ¼ÈëÁËÀ࣬²¢½ç˵ÁËÁ½¸öº¯ÊýindexºÍdelete¡£º¯ÊýindexÓÃÓÚ´ÓExampleModelÖлñÈ¡ËùÓÐÊý¾Ý£¬²¢½«Æäת´ïµ½Ä£°åÖС£º¯Êýdelete´ÓHTTPÇëÇóÖлñȡҪɾ³ýµÄIDÁÐ±í£¬²¢Ê¹ÓÃExampleModel::destroyÒªÁìɾ³ýÕâЩÏîÄ¿¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèҪΪģ°åÌí¼ÓÒ»¸ö¶àÑ¡¿òÒÔ¼°É¾³ý°´Å¥¡£·¿ªapp /View /example /index.htmlÎļþ£¬Ìí¼ÓÒÔÏ´úÂ룺
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Example</title> </head> <body> <h1>Example</h1> <table> <thead> <tr> <th><input type="checkbox" id="check-all"></th> <th>ID</th> <th>Name</th> <th>Email</th> </tr> </thead> <tbody> <?php foreach ($list as $item): ?> <tr> <td><input type="checkbox" name="ids[]" value="<?php echo $item->id ?>"></td> <td><?php echo $item->id ?></td> <td><?php echo $item->name ?></td> <td><?php echo $item->email ?></td> </tr> <?php endforeach ?> </tbody> </table> <button id="btn-delete">ɾ³ý</button> <script> var btnDelete = document.querySelector('#btn-delete'); btnDelete.addEventListener('click', function () { var ids = []; var checkboxes = document.querySelectorAll('input[type=checkbox][name^=ids]:checked'); for (var i = 0; i < checkboxes.length; i++) { ids.push(checkboxes[i].value); } if (ids.length > 0) { if (confirm('È·¶¨É¾³ý£¿')) { var xhr = new XMLHttpRequest(); xhr.open('POST', '/example/delete'); xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8'); xhr.onload = function () { var result = JSON.parse(xhr.responseText); if (result.status == 'success') { alert(result.message); location.reload(); } else { console.error(result); alert('ɾ³ýʧ°Ü'); } }; xhr.onerror = function () { console.error(xhr); alert('ÍøÂçÒì³££¬ÇëÖØÊÔ'); }; xhr.send(JSON.stringify({ids: ids})); } } else { alert('ÇëѡժҪɾ³ýµÄÏîÄ¿'); } }); var checkAll = document.querySelector('#check-all'); checkAll.addEventListener('click', function () { var checkboxes = document.querySelectorAll('input[type=checkbox][name^=ids]'); for (var i = 0; i < checkboxes.length; i++) { checkboxes[i].checked = checkAll.checked; } }); </script> </body> </html>
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁËJavaScriptÀ´»ñÈ¡ËùÓÐÑ¡ÖеĶàÑ¡¿ò£¬²¢½«ÕâЩIDת´ï¸øЧÀÍÆ÷µÄ/delete·ÓÉ¡£
Îå¡¢²âÊÔ
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔ·¿ª¿ØÖÆ̨£¬Ê¹ÓÃÒÔÏÂÏÂÁWebЧÀÍÆ÷Æô¶¯ÆðÀ´¡£
php think run
È»ºóÔÚä¯ÀÀÆ÷ÖÐÊäÈëlocalhost:8000/example£¬¼´¿É·¿ªexampleµÄÊ×Ò³£¬µã»÷¶àÑ¡¿òÒÔѡժҪɾ³ýµÄÏî¡£×îºóµã»÷ɾ³ý°´Å¥¼´¿Éɾ³ýÑ¡ÔñµÄÏî¡£
Áù¡¢½áÂÛ
ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃÇʹÓÃthinkphp6µÄ¿ØÖÆÆ÷£¬Ä£×ÓºÍÊÓͼÀ´ÊµÏÖÁ˶àѡɾ³ýµÄ¹¦Ð§¡£Õâ¸ö¹¦Ð§¿ÉÒÔÔÚWebÓ¦ÓóÌÐòÖÐÊ©Õ¹Ö÷Òª×÷Óã¬ÓÅ»¯Ó¦ÓóÌÐòµÄÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇthinkphp¶àѡɾ³ýµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡