laravelÔõÑùÌìÉú¿ØÖÆÆ÷
laravel ÊÇÒ»¸öºÜÊÇÊ¢ÐÐµÄ php ¿ª·¢¿ò¼Ü£¬¼«´óµØ¼ò»¯ÁË×ðÁú¿Ê±¿ª·¢ÊÂÇé¡£ÔÚ laravel ÖУ¬¿ØÖÆÆ÷£¨controller£©ÊÇÓÃÓÚ´¦Öóͷ£Óû§ÇëÇóµÄÖ÷Òª×é¼þ£¬³£ÓÃÓÚ¶àÖÖ web Ó¦ÓóÌÐòÖС£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓà laravel ¿ìËÙµØÌìÉú¿ØÖÆÆ÷¡£
Laravel ÓÐÒ»Ì×ÍêÕûµÄ¿ØÖÆÆ÷ÌìÉú¹¤¾ß£¬¿ÉÓÃÓÚÌìÉú±ê×¼µÄ RESTful ½Ó¿Ú¿ØÖÆÆ÷¡£ÎªÁËʹÓà Laravel µÄ¿ØÖÆÆ÷ÌìÉú¹¤¾ß£¬ÎÒÃÇÐèÒªÏÈÔÚÏÂÁîÐÐÖÐ×°Öà Laravel£¬×°ÖÃÀú³ÌÖÐÐèÒªÏÈ×°Öà Composer¡£ÈôÊÇÄ㻹²»Ïàʶ Laravel ºÍ Composer£¬ÇëÉó²é¹Ù·½ÍøÕ¾ºÍÎĵµÏàʶ¸ü¶àÐÅÏ¢¡£
ÔÚ×°Öà Laravel ºó£¬Äú¿ÉÒÔÔÚÏÂÁîÐÐÖÐÊäÈëÒÔÏÂÏÂÁîÒÔ½¨ÉèÒ»¸öеĿØÖÆÆ÷£º
php artisan make:controller UserController
µÇ¼ºó¸´ÖÆ
ÉÏÊöÏÂÁÌìÉúÒ»¸öÃûΪ UserController µÄ¿ØÖÆÆ÷Îļþ£¨UserController.php£©¡£ÔÚĬÈÏÇéÐÎÏ£¬¿ØÖÆÆ÷Îļþ½«±£´æÓÚÓ¦ÓóÌÐòµÄ¡°app/Http/Controllers¡±Îļþ¼ÐÖС£
Laravel ÌìÉúµÄ¿ØÖÆÆ÷ÀàÎļþ°üÀ¨ÁËһЩĬÈÏʵÏÖÒªÁì¡£ÆäÖÐ×îÖ÷ÒªµÄÒªÁìÊÇ index ÒªÁì¡£Õâ¸öÒªÁ콫ÔÚÓû§ÇëÇó¿ØÖÆÆ÷ʱŲÓã¬Í¨³£ÓÃÀ´·µ»ØÒ»¸ö°üÀ¨ËùÓÐ×ÊÔ´µÄÁÐ±í¡£
Laravel ¿ØÖÆÆ÷»¹¿ÉÒÔÔÚ´¦Öóͷ£Óû§ÇëÇóʱŲÓÃÄ£×ÓÀà»òÊý¾Ý¿â£¬ÒÔ·µ»ØÇкÏÇëÇóÌõ¼þµÄ×ÊÔ´¡£ÔÚÕâÖÖÇéÐÎÏ£¬ÎÒÃÇÐèÒªÔÚ¿ØÖÆÆ÷ÀàµÄ½á¹¹º¯ÊýÖÐ×¢ÈëËùÐèµÄÄ£×Ó£¬ÀýÈ磺
use AppModelsUser; class UserController extends Controller { protected $user; public function __construct(User $user) { $this->user = $user; } public function index() { $users = $this->user->all(); return view('user.index', compact('users')); } }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇͨ¹ýÀàÐÍÌáÐÑ×¢ÈëÁË User Ä£×Ó£¬È»ºóÔÚ¿ØÖÆÆ÷ÖÐŲÓÃÁË all() ÒªÁìÀ´»ñÈ¡ËùÓÐÓû§¼Í¼¡£±ðµÄ£¬ÎÒÃÇ»¹ÔÚ index ÒªÁìÖзµ»ØÁËÒ»¸öÓû§ÁбíÊÓͼ¡£
³ýÁËĬÈϵÄʵÏÖÒªÁìÖ®Í⣬ÎÒÃÇ»¹¿ÉÒÔÔÚ Laravel ¿ØÖÆÆ÷ÖÐ×Ô½ç˵²Ù×÷¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸ö show ÒªÁ죬ÓÃÓÚÏÔʾָ¶¨Óû§µÄÏêϸÐÅÏ¢£º
public function show($id) { $user = $this->user->find($id); return view('user.show', compact('user')); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇͨ¹ý×¢ÈëµÄ User Ä£×Ó²éÕÒÁËÖ¸¶¨ id µÄÓû§£¬È»ºó°ÑÕâ¸öÓû§×ª´ï¸ø user.show ÊÓͼ¡£
ÔÚ Laravel ÖУ¬¿ØÖÆÆ÷µÄ×÷ÓþÍÊÇÎüÊպʹ¦Öóͷ£Óû§ÇëÇó£¬È»ºóƾ֤ÇëÇó¸ø³öÏìÓ¦¡£Ò»Ñùƽ³£À´Ëµ£¬¿ØÖÆÆ÷Ö»ÊÇÒ»ÖÖÖÐÐIJ㣬ÓÃÀ´Ðµ÷Êý¾ÝÁ÷ºÍÇëÇóÏìÓ¦£¬ÏÖʵµÄÊý¾Ý´¦Öóͷ£ºÍ²Ù×÷¿ÉÄÜ»áÔÚÄ£×ÓÀà»òÊý¾Ý¿â²ãÃæ¾ÙÐС£
×ܽáһϣºÊ¹Óà Laravel ÌìÉú¿ØÖÆÆ÷ºÜÊǼòÆÓ£¬Ö»ÐèÒªÔÚÏÂÁîÐÐÖÐÊäÈëÏìÓ¦µÄÏÂÁ¾Í¿ÉÒÔ¿ìËÙµØÌìÉú±ê×¼µÄ RESTful ½Ó¿Ú¿ØÖÆÆ÷¡£ÔÚÓ¦ÓóÌÐòÖÐʹÓÿØÖÆÆ÷ʱ£¬Çë×¢ÖØÕÆÎÕËüËù×öµÄÊÂÇ飬²¢½«ÆäÓëÄ£×ÓÀàºÍÊý¾Ý¿â²Ù×÷ÍŽáʹÓã¬ÒÔ»ñµÃ¸üºÃµÄЧ¹û¡£
ÒÔÉϾÍÊÇlaravelÔõÑùÌìÉú¿ØÖÆÆ÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡