laravel ÔËÐÐÀú³Ì
laravelÊÇÏÖÔÚ×îÊܽӴýµÄphp ¿ò¼ÜÖ®Ò»£¬ËüµÄÉè¼ÆÀíÄîÊǾ«Á·¡¢ÓÅÑÅ£¬Í¬Ê±Ò²ÌṩÁËÐí¶àÀû±ã¿ª·¢µÄ¹¤¾ßºÍ¹¦Ð§¡£ÔÚlaravelÖУ¬Ò»¸öÇëÇó×îÖÕ±äΪһ¸öÏìÓ¦£¬Õâ¸öÀú³ÌÖб£´æ×ÅÐí¶à×é¼þ£¬ÒÔÖª×㿪·¢ÕßµÄÐèÇó¡£±¾ÎĽ«»áÏêϸÏÈÈÝlaravelµÄÔËÐÐÀú³Ì£¬´ÓÇëÇó×îÏÈÒ»Ö±µ½ÏìÓ¦¿¢Ê£¬ÈöÁÕßÏàʶÿһ¸ö°ì·¨¶¼ÊÇÔõÑùÔË×÷µÄ¡£
·ÓÉ
LaravelÖеĵÚÒ»¸ö×é¼þÊÇ·ÓÉ¡£ËüµÄÄ¿µÄÊÇƾ֤ÇëÇóµÄURLÀ´¾öÒéÏìÓ¦µÄ´¦ÀíÂß¼¡£ÔÚLaravelÖУ¬Â·ÓɵĽç˵ºÜÊǼòÆÓ£¬ÔÚroutes/web.phpÎļþÖпÉÒÔ½ç˵һϵÁзÓÉ£¬ÈçÏ£º
Route::get('/', function () { return view('welcome'); });
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö´¦Àí¸ù·¾¶µÄ·ÓÉ£¬Õâ¸ö·ÓɵĴ¦ÀíÂß¼ÊÇ·µ»ØÒ»¸öÃûΪwelcomeµÄÄ£°åÎļþ¡£
ÇëÇóºÍÖÐÐļþ
µ±Ò»¸öÇëÇóµÖ´ïÓ¦ÓóÌÐòʱ£¬Ó¦ÓóÌÐò»áÊ×ÏȽ«Õâ¸öÇëÇó·â×°³ÉÒ»¸öIlluminateHttpRequest ¹¤¾ß¡£¸Ã¹¤¾ß°üÀ¨Ò»ÏµÁÐÓÐÓõÄÒªÁìºÍÊôÐÔ£¬¿ÉÒÔÈÃÎÒÃÇÀû±ãµØ»ñÈ¡ÇëÇóµÄ¸÷¸ö²¿·Ö£¬ÀýÈçGET²ÎÊý¡¢POST²ÎÊýµÈµÈ¡£
ÔÚÇëÇóµÖ´ïÓ¦ÓóÌÐòÖ®ºó£¬½ÓÏÂÀ´µÄ°ì·¨¾ÍÊǾÓÉһϵÁеÄÖÐÐļþ¡£ÖÐÐļþ¿ÉÒÔ¿´×÷ÊÇÔÚÇëÇóºÍÏìÓ¦Ö®¼äµÄ´¦Àí²ã£¬¿ÉÒÔͨ¹ýÖÐÐļþÀ´ÊµÏÖһЩ¹²ÐԵĴ¦ÀíÂß¼£¬ÀýÈçУÑéÓû§È¨ÏÞ¡¢¿çÓò´¦Àí¡¢·¢ËÍÓʼþµÈµÈ¡£ÔÚLaravelÖУ¬ÖÐÐļþ½ç˵ÔÚapp/Http/Middleware Ŀ¼Ï£¬ÈçÏÂÊÇÒ»¸ö½ç˵ÖÐÐļþµÄʾÀý´úÂ룺
<?php namespace AppHttpMiddleware; use Closure; class MyMiddleware { public function handle($request, Closure $next) { // ÔÚÇëÇó´¦Àí֮ǰµÄ×Ô½ç˵Âß¼ return $next($request); } }
µÇ¼ºó¸´ÖÆ
¿ØÖÆÆ÷
ÔÚLaravelÖУ¬¿ØÖÆÆ÷ÊÇ´¦ÀíHTTPÇëÇóµÄÒ»¸öÖ÷Òª×é³É²¿·Ö¡£ËüÊǽ«Ä£×Ó¡¢ÊÓͼºÍÆäËûÀà×éºÏÆðÀ´µÄ½¹µã²¿·Ö£¬Ö¼ÔÚʹÇëÇóÂß¼Ô½·¢½á¹¹»¯ºÍ¿Éά»¤¡£µ±Ò»¸öÇëÇóͨ¹ý·ÓɺÍÖÐÐļþÖ®ºó£¬Laravel¿ò¼Üƾ֤·ÓÉÖÐÖ¸¶¨µÄ¿ØÖÆÆ÷À༰ÆäÒªÁìÀ´´¦ÀíÇëÇ󣬲¢·µ»ØÏìÓ¦¡£
ÏÂÃæÊÇÒ»¸ö¼òÆӵĿØÖÆÆ÷ʾÀý´úÂ룺
<?php namespace AppHttpControllers; use IlluminateHttpRequest; class MyController extends Controller { public function index(Request $request) { return view('my.view', ['key' => 'value']); } }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸öMyController¿ØÖÆÆ÷£¬²¢ÔÚÆäÖнç˵ÁËÒ»¸ö´¦ÀíindexÇëÇóµÄÒªÁì¡£Õâ¸öÒªÁì·µ»ØÁËmy.viewµÄÊÓͼ£¬²¢×ª´ïÁËÒ»¸öÃûΪkeyµÄ²ÎÊý¡£
ÊÓͼ
ÊÓͼÊÇLaravelÖÐÁíÒ»¸öÖ÷ÒªµÄ×é³É²¿·Ö¡£ËüÃÇÊÇÏÔʾÓû§½çÃæµÄ×é¼þ£¬·ºÆð¸øÓû§¿ÉÒÔ¿´µ½µÄHTML´úÂë¡£ÔÚLaravelÖУ¬ÊÓͼÎļþÉúÑÄÔÚresources/viewsĿ¼Ï¡£µ±¿ØÖÆÆ÷·µ»ØÒ»¸öÊÓͼµÄʱ¼ä£¬Laravel½«×Ô¶¯µØÔÚÕâ¸öĿ¼Ï²éÕÒÆ¥ÅäµÄÄ£°åÎļþ¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÊÓͼʾÀý´úÂ룺
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>My View</title> </head> <body> <p>The value of the key parameter is: {{ $key }}</p> </body> </html>
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö¼òÆÓµÄHTMLÎļþ£¬È»ºóͨ¹ý{{ $key }}µÄ·½·¨À´Êä³ö¿ØÖÆÆ÷ת´ï¹ýÀ´µÄ²ÎÊý¡£
ÏìÓ¦
ÇëÇóµÄ×îºóÒ»¸ö×é¼þÊÇÏìÓ¦¡£LaravelÖÐÏìÓ¦ÓÐÐí¶àÖÖÐÎʽ£¬¿ÉÒÔÊÇÒ»¸ö´¿Îı¾×Ö·û´®¡¢Ò»¸öHTMLÊÓͼ¡¢Ò»¸öJSONÏìÓ¦µÈµÈ¡£ÔÚLaravelÖУ¬ÏìÓ¦¹¤¾ßÊÇSymfonyComponentHttpFoundationResponseÀàµÄʵÀý¡£µ±ÄãÔÚ¿ØÖÆÆ÷Öзµ»ØÒ»¸öÏìӦʱ£¬Laravel»á½«Æäת»»ÎªÒ»¸öÍêÕûµÄÏìÓ¦¹¤¾ß£¬È»ºó·¢Ëͻظø¿Í»§¶Ë¡£
ÏÂÃæÊÇÒ»¸ö·µ»ØJSONÏìÓ¦µÄʾÀý´úÂ룺
<?php namespace AppHttpControllers; use IlluminateHttpRequest; class MyController extends Controller { public function index(Request $request) { return response()->json([ 'message' => 'Hello World!', ]); } }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇ·µ»ØÁËÒ»¸ö°üÀ¨message¼üºÍ¡°Hello World£¡¡±ÖµµÄJSONÏìÓ¦¡£
×ܽá
ÉÏÃæ¾ÍÊÇLaravelµÄÔËÐÐÀú³Ì£¬´Ó·ÓɵĽç˵µ½ÏìÓ¦µÄ·¢ËÍ£¬Ã¿¸ö×é¼þ¶¼Ê©Õ¹ÁË×Ô¼ºµÄÆæÒì×÷Óá£ÏàʶÕâЩ×é¼þµÄÔË×÷ÔÀíÓÐÖúÓÚÄã¸üºÃµØʹÓÃLaravel¿ª·¢×Ô¼ºµÄÓ¦ÓóÌÐò£¬Í¬Ê±Ò²ÓÐÖúÓÚÄã¸üºÃµØÏàʶPHPÓ¦ÓõÄÕû¸öÉúÃüÖÜÆÚ¡£
ÒÔÉϾÍÊÇlaravel ÔËÐÐÀú³ÌµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡