thinkphpÔõÑù¾ÙÐÐ×赲·ÓɵIJÙ×÷
Ò»¡¢Ê²Ã´ÊÇ·ÓÉ×èµ²
·ÓÉ×èµ²ÓÃÓÚ¶Ô»á¼û·¾¶¾ÙÐÐÖÎÀíºÍ¿ØÖÆ£¬ÒÔά»¤ÍøÕ¾µÄÇå¾²ÐÔºÍÎȹÌÐÔ¡£ÎªÁ˱ÜÃâÇå¾²ÎÊÌâºÍ¹¥»÷£¬ÎÒÃÇͨ³£»á¶Ô»á¼û·¾¶¾ÙÐйýÂ˺Íեȡ¡£Í¨¹ý·ÓÉ×èµ²£¬¿ÉÒÔ¶Ô»á¼ûµÄ URL ¾ÙÐпØÖÆ£¬¹ýÂËһЩÎÞЧµÄ URL£¬´Ó¶øÌá¸ßÍøÕ¾µÄÇå¾²ÐԺͿɿ¿ÐÔ¡£
¶þ¡¢ThinkPHP·ÓÉ×赲ʵÏÖÒªÁì
ÒÔÏÂÊÇThinkPHPʵÏÖ·ÓÉ×èµ²µÄÒªÁ죺
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
1.ͨ¹ýÔÚÓ¦ÓÃÉèÖÃÎļþconfig.phpÖÐÉèÖ÷ÓɹæÔò£»
2.ͨ¹ýÔÚ¿ØÖÆÆ÷ŲÓÃ֮ǰ¾ÙÐÐ×èµ²Åжϣ»
3.ͨ¹ýÔÚ·ÓÉ֮ǰ¾ÙÐÐ×èµ²Åжϡ£
ÏÂÃæÏêϸÏÈÈÝÕâÈýÖÖÒªÁìµÄʹÓ÷½·¨£º
1.ͨ¹ýÔÚÓ¦ÓÃÉèÖÃÎļþconfig.phpÖÐÉèÖ÷ÓɹæÔò
Ó¦ÓÃÉèÖÃÎļþconfig.phpÖÐÉèÖ÷ÓɹæÔòÊÇThinkPHPÖеÄÒ»ÏЧ¡£ÔÚconfig.phpÎļþÖУ¬¿ÉÒÔÕÒµ½ÃûΪ¡®URL_ROUTE_RULES¡¯µÄÉèÖÃÏ¿ÉÒÔÔÚ¸ÃÉèÖÃÏîÖнç˵·ÓɹæÔò¡£Ïêϸ²Ù×÷ÈçÏ£º
return [ 'URL_ROUTE_RULES' => [ 'login' => 'Index/login', 'register' => 'Index/register', 'user/:id' => 'User/index', 'user/add' => 'User/add', 'user/edit/:id' => 'User/edit', 'user/delete/:id' => 'User/delete', ], ];
µÇ¼ºó¸´ÖÆ
2.ͨ¹ýÔÚ¿ØÖÆÆ÷ŲÓÃ֮ǰ¾ÙÐÐ×èµ²ÅжÏ
ʹÓÿØÖÆÆ÷À©Õ¹¹¦Ð§¿ÉÒÔÔÚThinkPHPÖÐʵÏÖ·ÓÉ×èµ²¡£ÎÒÃÇ¿ÉÒÔʹÓÃbeforeÒªÁìÔÚ¿ØÖÆÆ÷À©Õ¹¹¦Ð§µÄÀú³ÌÖÐ×èµ²»á¼ûÇëÇó²¢¾ÙÐд¦Àí¡£Ê¹ÓÃbeforeÒªÁìÐèÒªÖª×ãÒÔÏÂÌõ¼þ£º
1.¿ØÖÆÆ÷ÐèÒª¼ÌÐø\think\ControllerÀࣻ
2.beforeÒªÁìÐèÒª·µ»ØÒ»¸öboolÀàÐ͵ÄÖµ£¬trueÌåÏÖ×èµ²Àֳɣ¬falseÌåÏÖ×赲ʧ°Ü¡£
Ïêϸ²Ù×÷ÈçÏ£º
namespace app\index\controller; use think\Controller; class Index extends Controller { protected function before() { if(request()->action() == "index"){ if(!session('loginTime')){ return false; } } return true; } public function index() { return "hello world"; } public function login() { return $this->fetch(); } }
µÇ¼ºó¸´ÖÆ
3.ͨ¹ýÔÚ·ÓÉ֮ǰ¾ÙÐÐ×èµ²ÅжÏ
ÎÒÃÇ¿ÉÒÔʹÓ÷ÓÉÀ©Õ¹¹¦Ð§ÔÚThinkPHPÖÐʵÏÖ·ÓÉ×èµ²¡£Í¨¹ýʹÓÃbeforeÒªÁ죬ÎÒÃÇ¿ÉÒÔ¶Ô»á¼ûÇëÇó¾ÙÐÐ×èµ²ºÍ´¦Àí£¬´Ó¶øÀ©Õ¹Â·ÓɵĹ¦Ð§¡£Ê¹ÓÃbeforeÒªÁìÐèÒªÖª×ãÒÔÏÂÌõ¼þ£º
1.·ÓÉÐèÒª½ç˵һ¸ö±Õ°üº¯Êý£»
2.beforeÒªÁìÐèÒª·µ»ØÒ»¸öboolÀàÐ͵ÄÖµ£¬trueÌåÏÖ×èµ²Àֳɣ¬falseÌåÏÖ×赲ʧ°Ü¡£
Ïêϸ²Ù×÷ÈçÏ£º
use think\Route; Route::rule('/', function () { return 'hello world!'; }, 'GET')->before(function () { if(!session('loginTime')){ return false; } return true; });
µÇ¼ºó¸´ÖÆ
Èý¡¢³£¼û·Óɹýʧ¼°½â¾ö¼Æ»®
1.ÇëÇóµÄÒªÁì²»±»ÔÊÐí
ÍÉ»¯Ôµ¹ÊÔÓÉ£ºÇëÇóµÄÒªÁ첻׼ȷ£¬ÀýÈçʹÓÃgetÇëÇó»á¼ûpost·ÓÉ¡£
½â¾ö¼Æ»®£º¼ì²é·Óɽç˵ºÍÇëÇóÒªÁìÊÇ·ñÒ»Ö¡£
2.δÕÒµ½ÒªÁì½ç˵
ÍÉ»¯Ôµ¹ÊÔÓÉ£ºÇëÇ󷾶ÓëÒªÁ첻ƥÅ䣬»òÕß·ÓɹæÔò½ç˵¹ýʧ¡£
½â¾ö¼Æ»®£º¼ì²é·ÓɹæÔòºÍ½ç˵µÄÒªÁìÃûÊÇ·ñÒ»Ö¡£
3.¿ØÖÆÆ÷²»±£´æ
ÍÉ»¯Ôµ¹ÊÔÓÉ£ºÕÒ²»µ½¶ÔÓ¦µÄ¿ØÖÆÆ÷¡£
½â¾ö¼Æ»®£º¼ì²é¿ØÖÆÆ÷ÀàÃûºÍÎļþÃûÊÇ·ñÒ»Ö£¬ÒÔ¼°ÊÇ·ñÔÚ׼ȷµÄλÖá£
4.ȱÉÙ²ÎÊý
ÍÉ»¯Ôµ¹ÊÔÓÉ£ºÇëÇ󷾶ÖÐȱÉÙÐëÒªµÄ²ÎÊý¡£
½â¾ö¼Æ»®£º¼ì²é·ÓɹæÔòÊÇ·ñ½çËÃ÷È׼ȷ£¬²¢×ª´ï׼ȷµÄ²ÎÊý¡£
5.¡°Access Denied¡±¹ýʧ
ÍÉ»¯Ôµ¹ÊÔÓÉ£ºÈ¨ÏÞȱ·¦£¬»òÕßÒѾÍ˳öµÇ¼¡£
½â¾ö¼Æ»®£º¼ì²éȨÏÞÉèÖú͵Ǽ״̬µÈÐÅÏ¢¡£
×¢£ºÒÔÉϹýʧ½öÊdz£¼û¹ýʧ£¬Ïêϸ¹ýʧ»¹ÒªÆ¾Ö¤ÏêϸÇéÐξÙÐÐÅŲ顣
ÒÔÉϾÍÊÇthinkphpÔõÑù¾ÙÐÐ×赲·ÓɵIJÙ×÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡