ThinkPHP 5.0 ÖÐÔõÑù¾ÙÐÐÐÞ¸Ä
thinkphp 5.0 ÊÇÒ»¿î»ùÓÚ php µÄÇáÁ¿¼¶ mvc ¿ò¼Ü¡£ËüµÄÒ×ÓÃÐÔ¡¢ÎÞаÐÔ¡¢¸ßЧÂÊʹµÃËü³ÉΪÁË¿ª·¢ÕßµÄÊ×Ñ¡¡£È»¶øËæ×ÅÏîÄ¿µÄÉîÈ뿪·¢£¬ÎÒÃÇ¿ÉÄÜ»áÐèÒª¶Ô¿ò¼Ü¾ÙÐÐÐÞ¸ÄÒÔ˳Ӧ×Ô¼ºµÄÓªÒµÐèÇó¡£±¾ÎĽ«ÏÈÈÝÔÚ thinkphp 5.0 ÖÐÔõÑù¾ÙÐÐÐ޸ġ£
Ò»¡¢Ã÷È·´úÂë½á¹¹
ÔÚ¾ÙÐÐÐÞ¸ÄÇ°£¬ÎÒÃÇÐèÒªÏàʶ¿ò¼ÜµÄ´úÂë½á¹¹¡£Ö÷ÒªµÄ´úÂë½á¹¹ÊÇ£º
application£ºÓ¦ÓÃĿ¼£¬Ó¦ÓõÄËùÓдúÂ붼·ÅÔÚ´ËĿ¼Ï¡£
thinkphp£ºThinkPHP ¿ò¼Ü½¹µã´úÂëĿ¼¡£
public£º¹«¹²¸ùĿ¼£¬Í¨³£°²ÅÅ index.php ºÍ¾²Ì¬×ÊÔ´Îļþ¡£
vendor£ºComposer ÒÀÀµ¿âĿ¼¡£
¶þ¡¢Ð޸Ľ¹µãÀà¿â
Ð޸ķÓÉÀà
ThinkPHP 5.0 ÖзÓÉÀàλÓÚ thinkphp/library/think/Route.php¡£
ÓÐʱ¼ä£¬ÎÒÃÇÐèҪƾ֤Óû§µÄÇëÇó URL ×öһЩÌØÊâ´¦Àí£¬ÀýÈçÔöÌíһЩ²ÎÊý¡¢¸Ä±ä·µ»ØÖµÀàÐ͵ȡ£Õâʱ¼äÎÒÃǾÍÐèÒªÐ޸ķÓÉÀà¡£Ê×ÏÈÐèÒªÔÚ application Ŀ¼Ï½¨ÉèÒ»¸ö route.php Îļþ£¬È»ºóÖØд Route ÀàµÄÒªÁì¡£ÀýÈ磬ÎÒÃÇÏëҪƾ֤Óû§ÇëÇó URL ÔÚ·µ»ØÖµÖÐÔöÌíÒ»¸ö data ²ÎÊý£¬¿ÉÒÔ½« Route ÀàÖÐµÄ return $result; ¸ÄΪ return [‘data’ => $result];¡£Í¬Ê±ÔÚ route.php ÖÐÒýÈëÔÀ´µÄ·ÓÉÎļþ²¢ÐÞ¸Ä Route Àà¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
Ð޸ĿØÖÆÆ÷Àà
ThinkPHP 5.0 ÖÐĬÈϵĿØÖÆÆ÷ÀàλÓÚ thinkphp/library/think/Controller.php¡£
ÓÐʱ¼ä£¬ÎÒÃÇ×Ô½ç˵µÄ¿ØÖÆÆ÷ÐèҪһЩȫ¾ÖµÄ²ÎÊý»òÕßÒªÁ죬ÀýÈçȨÏÞÑéÖ¤¡¢È«¾Ö±äÁ¿µÈ¡£´Ëʱ£¬ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷ÀàÖнç˵һ¸ö $options ÊôÐÔ£¬À´´æ´¢ÕâЩ²ÎÊý»òÕßÒªÁì¡£½Ó×Å£¬ÔÚ×Ô½ç˵µÄ¿ØÖÆÆ÷ÖÐÖØд¿ØÖÆÆ÷ÀàµÄ __construct ÒªÁ죬²¢¼ÌÐø $options ÊôÐÔ¡£ÀýÈ磬ÎÒÃÇÏëÒªÔÚËùÓпØÖÆÆ÷Öж¼×¢ÈëÒ»¸ö $user ±äÁ¿£¬¿ÉÒÔ½«ÒÔÏ´úÂë¼ÓÈëµ½¿ØÖÆÆ÷ÀàÖУº
public $options; public function __construct() { $this->options = ['user' => 'test']; parent::__construct(); }
µÇ¼ºó¸´ÖÆ
È»ºóÔÚÏìÓ¦µÄ¿ØÖÆÆ÷ÖÐʹÓà $this->options[‘user’] ¾Í¿ÉÒÔ»ñµÃ×¢ÈëµÄ $user ±äÁ¿¡£
ÐÞ¸ÄÄ£×ÓÀà
ThinkPHP 5.0 ÖÐĬÈϵÄÄ£×ÓÀàλÓÚ thinkphp/library/think/Model.php¡£
ÓÐʱ¼ä£¬ÎÒÃÇÐèÒª¸ü¸ÄÄ£×ÓÀàµÄĬÈÏÒªÁìÒÔ˳Ӧ×Ô¼ºµÄÓªÒµÐèÇó¡£ÀýÈ磬ÎÒÃÇÏ£ÍûËùÓеÄÅÌÎʲÙ×÷¶¼°üÀ¨Èíɾ³ý×ֶΣ¬¿ÉÒÔÔÚÄ£×ÓÀàÖнç˵³õʼ»¯ÒªÁ죬ȻºóŲÓÃĬÈϵÄÅÌÎÊÒªÁìʱ¾ÙÐиÄд¡£ÀýÈ磺
class MyModel extends Model { protected function initialize() { parent::initialize(); $this->where(['is_deleted' => 0])->scope('soft_delete', function($query){ $query->where(['is_deleted' => 0]); }); } public function find($dataOrWhere = null) { return $this->softDelete()->where($dataOrWhere)->find(); } public function select($dataOrWhere = null) { return $this->softDelete()->where($dataOrWhere)->select(); } }
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬ÎÒÃÇͨ¹ýÏòÅÌÎÊÌõ¼þÌí¼Ó soft_delete À´ÊµÏÖÈíɾ³ý¹ýÂË¡£
Èý¡¢ÐÞ¸ÄÉèÖÃÎļþ
ThinkPHP 5.0 µÄÉèÖÃÎļþλÓÚ application/config.php¡£
ÎÒÃÇ¿ÉÒÔÔÚÕâ¸öÉèÖÃÎļþÖÐÐ޸Ŀò¼ÜµÄĬÈÏÉèÖã¬ÀýÈç·ÓɺÍÊý¾Ý¿â¡£ÀýÈ磬ÈôÊÇÎÒÃÇÏ£Íû¸ü¸Ä ThinkPHP 5.0 ·ÓɵÄĬÈϲÎÊý£¬¿ÉÒÔ½«ÉèÖÃÎļþÖеÄĬÈϲÎÊýÐÞ¸ÄΪ£º
'route' => [ 'default_route_pattern' => '[\w\-\_]+', // ĬÈϵÄ·ÓɲÎÊýÍÑÀë·û 'default_route_depr' => '/', // ÊÇ·ñ¿ªÆô·ÓÉÑÓ³ÙÆÊÎö 'url_route_lazy' => true, // ÊÇ·ñÇ¿ÖÆʹÓ÷ÓÉ 'url_route_must' => true, // ÊÇ·ñÆôÓ÷ÓÉ»º´æ 'route_check_cache' => true, ],
µÇ¼ºó¸´ÖÆ
ͬÑùµÄ£¬ÎÒÃÇÒ²¿ÉÒÔÔÚÉèÖÃÎļþÖÐÌí¼Ó×Ô½ç˵µÄÉèÖÃÏÒÔ˳Ӧ²î±ðµÄÓªÒµÐèÇó¡£ÀýÈ磬ÎÒÃÇÐèÒªÔÚÕû¸öÓ¦ÓÃÖнç˵һ¸ö³£Á¿£¬¿ÉÒÔ½«³£Á¿½ç˵ÔÚ config.php ÎļþÖС£
define('APP_VERSION', '1.0.0'); return [ 'test' => 'hello world', 'constant' => APP_VERSION, // ÆäËûÉèÖÃÏî ];
µÇ¼ºó¸´ÖÆ
È»ºóÔÚÓ¦ÓÃÖпÉÒÔͨ¹ý Config::get(‘constant’) À´»ñÈ¡½ç˵µÄ³£Á¿Öµ¡£
×ܽá
ÔÚÐÞ¸Ä ThinkPHP 5.0 ¿ò¼Üʱ£¬ÐèÒª¶Ô´úÂë½á¹¹ÓÐÒ»¶¨µÄÏàʶ£¬²¢ÇÒÔÚ¾ÙÐÐÐÞ¸ÄʱÐèҪעÖؼæÈÝÐԺͿÉÀ©Õ¹ÐÔ¡£ÈôÊDz»Ð¡ÐÄ·ºÆðÁ˼æÈÝÐÔÎÊÌ⣬¿ÉÒÔͨ¹ýÉèÖÃÎļþÀ´½â¾ö¡£ÈôÊÇ·ºÆð¿ÉÀ©Õ¹ÐÔÎÊÌ⣬¿ÉÒÔͨ¹ýÖØд¿ò¼ÜµÄ½¹µãÀà¿âÀ´½â¾ö¡£
ÒÔÉϾÍÊÇThinkPHP 5.0 ÖÐÔõÑù¾ÙÐÐÐ޸ĵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡