laravelÈÝÆ÷ÔõôÓÃ
laravelÊÇÒ»¸öºÜÊÇÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁËÐí¶à±ãµ±µÄ¹¦Ð§£¬ÆäÖÐÒ»¸ö¾ÍÊÇÈÝÆ÷¡£laravelÈÝÆ÷ÊÇÒ»ÖÖÒÀÀµ×¢Èëϵͳ£¬ËüÔÊÐíÎÒÃÇÔÚÓ¦ÓóÌÐòÖÐÇáËÉÖÎÀí¹¤¾ßʵÀý¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ñ§Ï°laravelÈÝÆ÷ÒÔ¼°ÔõÑùʹÓÃËü¡£
ʲôÊÇLaravelÈÝÆ÷
LaravelÈÝÆ÷ÊÇÒ»¸öIoCÈÝÆ÷£¬Ò²¾ÍÊÇÒÀÀµ×¢ÈëÈÝÆ÷¡£ËüÊÇÒ»¸ö×¢²á±í£¬¿ÉÒÔÔÚÓ¦ÓóÌÐòÖÐÇáËÉÖÎÀí¹¤¾ß¡£ÕâЩ¹¤¾ß¿ÉÒÔÊÇÈκι¤¾ß£¬°üÀ¨Ð§ÀÍ¡¢ÖÐÐļþ¡¢¿ØÖÆÆ÷¡¢Ä£×ӵȵȡ£
ʹÓÃLaravelÈÝÆ÷Ö÷ÒªÓÐÁ½¸ö·½ÃæµÄÀûÒæ:
±ãÓÚÖÎÀí´úÂë
±ãÓÚ²âÊÔ
ÔõÑùʹÓÃLaravelÈÝÆ÷
LaravelÈÝÆ÷¿ÉÒÔºÜÈÝÒ×µØÔÚÓ¦ÓóÌÐòÖÐʹÓá£Ê×ÏÈ£¬ÎÒÃÇÐèÒªÏàʶÈÝÆ÷µÄ½¹µã¿´·¨Ö®Ò»¡ª¡ª°ó¶¨¡£
°ó¶¨
°ó¶¨Êǽ«Ò»¸öÀà»ò½Ó¿Ú°ó¶¨µ½ÈÝÆ÷ÖеÄÀú³Ì¡£µ±ÎÒÃÇÐèÒªÒ»¸ö°ó¶¨µÄʵÀýʱ£¬ÈÝÆ÷»áÌṩËü¡£ÔÚLaravelÖУ¬Ê¹ÓÃbind/bindSharedÒªÁìÍê³É°ó¶¨¡£bindÒªÁì°ó¶¨µÄÊÇÒ»¸öʵÀý£¬¶øbindSharedÒªÁìÊǰ󶨵¥Àý¡£
ÀýÈ磬ÎÒÃÇÏë°ó¶¨Ò»¸öÊý¾Ý¿â²Ù×÷Àà¡£ÎÒÃÇ¿ÉÒÔÔÚLaravelÖÐʹÓÃÒÔÏ´úÂ뽫Æä°ó¶¨µ½ÈÝÆ÷ÖУº
App::bind('db', function() { return new Database; });
µÇ¼ºó¸´ÖÆ
Õ⽫°ó¶¨Ò»¸öÃûΪ¡°db¡±µÄÀ࣬ÿµ±ÎÒÃÇŲÓá°db¡±Ê±£¬ÈÝÆ÷½«·µ»ØÒ»¸öеÄÅþÁ¬¡£
ÒÀÀµ×¢Èë
ÒÀÀµ×¢ÈëÊÇLaravelÈÝÆ÷µÄÖ÷Òª¹¦Ð§Ö®Ò»¡£ËüÊÇÖ¸½«ÒÀÀµÏîת´ï¸øÒ»¸ö¹¤¾ß£¬¶ø²»ÊÇÔÚ¹¤¾ßÄÚ²¿È¥ÊµÀý»¯ËüÃÇ¡£
ÀýÈ磬ÎÒÃÇÓÐÒ»¸ö¿ØÖÆÆ÷£¬ËüÐèÒªÒ»¸öÊý¾Ý¿â¹¤¾ß×÷Ϊ²ÎÊý¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÀÀµ×¢È뽫Ëüת´ï¸ø¿ØÖÆÆ÷£º
class UserController extends Controller { protected $db; public function __construct(Database $db) { $this->db = $db; } public function index() { $users = $this->db->table('users')->get(); return view('users.index', compact('users')); } }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇÓÃÒÀÀµ×¢ÈëµÄ·½·¨½«Ò»¸öʵÀý»¯µÄÊý¾Ý¿â¹¤¾ßת´ï¸ø¿ØÖÆÆ÷¡£Õâ¸ö²Ù×÷ÓÉLaravelÈÝÆ÷×Ô¶¯Íê³É¡£
ÔÚLaravelÖУ¬¿ÉÒÔʹÓÃÁ½¸ö·½·¨¾ÙÐÐÒÀÀµ×¢Èë¡£Ò»¸öÊǽṹº¯Êý×¢È룬ÁíÒ»¸öÊÇÒªÁì×¢Èë¡£ÉÏÃæµÄ´úÂëʾÀýʹÓõÄÊǽṹº¯Êý×¢Èë¡£
ÒªÁì×¢Èë
ÒªÁì×¢ÈëÊÇÁíÒ»ÖÖÒÀÀµ×¢Èë·½·¨¡£Ëü¿ÉÒÔƾ֤ÐèÒªÔÚÒªÁìÖÐ×¢ÈëÀàµÄʵÀý¡£
ÀýÈ磬ÎÒÃÇÓÐÒ»¸ö²Ù×÷Óû§µÄÀ࣬ÆäÖÐÓÐÒ»¸öÒªÁìgetUser£¬ËüÐèҪʹÓÃÒ»¸öAuthÀàµÄʵÀý¡£ÎÒÃÇ¿ÉÒÔÔÚÒªÁìµÄ²ÎÊýÖÐ×¢ÈëÕâ¸öʵÀý£º
class UserService { public function getUser(Auth $auth) { return $auth->user(); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬µ±ÎÒÃÇŲÓÃgetUserÒªÁìʱ£¬LaravelÈÝÆ÷»á×Ô¶¯×¢ÈëÒ»¸öAuthÀàµÄʵÀý¡£
LaravelÈÝÆ÷Ó¦Óó¡¾°
LaravelÈÝÆ÷¿ÉÒÔÔÚÐí¶à³¡¾°ÖÐʹÓá£ÏÂÃæÊǼ¸¸öµä·¶µÄ³¡¾°£º
ЧÀÍÌṩÕß
ЧÀÍÌṩÕßÊÇLaravelÈÝÆ÷Öг£ÓõĿ´·¨Ö®Ò»¡£ËüÊÇÒ»¸öÀ࣬ËüΪӦÓóÌÐòÌṩЧÀÍ¡£ÀýÈ磬ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂë×¢²áÒ»¸öЧÀÍÌṩÕߣº
class AppServiceProvider extends ServiceProvider { public function boot() { // } public function register() { $this->app->bind('db', function() { return new Database; }); } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬ÎÒÃÇ°ó¶¨ÁËÒ»¸öÃûΪ¡°db¡±µÄЧÀÍ£¬Ëü½«·µ»ØÒ»¸öеÄÊý¾Ý¿âÅþÁ¬¡£
ÖÐÐļþ
ÖÐÐļþÊÇÔÚLaravelÇëÇó´¦ÀíÁ÷³ÌÖб»Å²ÓõÄÀà¡£ÔÚÖÐÐļþÖУ¬ÎÒÃÇ¿ÉÒÔ¶ÔHTTPÇëÇóºÍÏìÓ¦¾ÙÐÐÐ޸ĻòÔöÇ¿¡£ÎÒÃÇ¿ÉÒÔʹÓÃLaravelÈÝÆ÷½«ÖÐÐļþ×¢Èëµ½Ó¦ÓóÌÐòÖУº
class ExampleMiddleware { public function handle($request, Closure $next) { // ´¦ÀíÇëÇó $response = $next($request); // ´¦ÀíÏìÓ¦ return $response; } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪExampleMiddlewareµÄÖÐÐļþ£¬Ëü½«´¦ÀíHTTPÇëÇóºÍÏìÓ¦¡£
¿ØÖÆÆ÷
Laravel¿ØÖÆÆ÷ÊÇÒ»ÖÖÓÃÓÚ´¦ÀíHTTPÇëÇóµÄÀà¡£ÎÒÃÇ¿ÉÒÔʹÓÃLaravelÈÝÆ÷½«¿ØÖÆÆ÷×¢Èëµ½Ó¦ÓóÌÐòÖУº
class UserController extends Controller { protected $db; public function __construct(Database $db) { $this->db = $db; } public function index() { $users = $this->db->table('users')->get(); return view('users.index', compact('users')); } }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃǽ«Ò»¸öÊý¾Ý¿âÀàʵÀý»¯²¢×¢Èëµ½UserController¿ØÖÆÆ÷ÖС£
×ܽá
LaravelÈÝÆ÷ÊÇÒ»¸öÇ¿Ê¢µÄÒÀÀµ×¢ÈëÈÝÆ÷¡£Ëü¿ÉÒÔÈÃÎÒÃÇÇáËɵØÖÎÀí¹¤¾ßʵÀý£¬²¢ÇÒÈÃÎÒÃǵĴúÂëÔ½·¢Ò×ÓÚ²âÊÔºÍÀ©Õ¹¡£ÔÚLaravelÓ¦ÓóÌÐòÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÈÝÆ÷À´Íê³ÉÐí¶àʹÃü£¬ÀýÈç×¢²áЧÀÍÌṩÕß¡¢ÖÐÐļþºÍ¿ØÖÆÆ÷¡£ÈôÊÇÄúÏëÏàʶ¸ü¶à¹ØÓÚLaravelÈÝÆ÷µÄÄÚÈÝ£¬ÇëÉó²éLaravel¹Ù·½Îĵµ¡£
ÒÔÉϾÍÊÇlaravelÈÝÆ÷ÔõôÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡