laravel5.4×öÉÌÆ·ÐÞ¸Ä
Ëæ×ŵçÉ̵ÄÉú³¤£¬ÉÌƷչʾ¹ØÓÚµçÉÌÍøÕ¾µÄÖ÷ÒªÐÔÔ½À´Ô½Í»³ö¡£ÎªÁËÖª×ãÉ̼ҵÄÐèÇó£¬ÍøÕ¾ÐèÒª´òÔìÒ»¸öºÃµÄÉÌƷչʾҳÃ棬ҲÐèҪʵÏÖÀû±ãµÄÉÌÆ·ÖÎÀíϵͳ¡£ÔÚ±¾ÎÄÖУ¬½«Ì½ÌÖÔõÑùʹÓà laravel 5.4 À´ÊµÏÖÒ»¸öÉÌÆ·ÖÎÀíϵͳµÄÐ޸Ĺ¦Ð§¡£
Ç°ÖÃ֪ʶ
ÔÚ×îÏÈ֮ǰ£¬ÎÒÃÇÐèÒªÕÆÎÕ Laravel ¿ò¼ÜºÍ Blade Ä£°åµÄ»ù´¡ÖªÊ¶¡£
Laravel ÏÈÈÝ
Laravel ÊÇÒ»¸öÃâ·ÑµÄ¿ªÔ´ PHP Web ¿ò¼Ü£¬Ê¹Óà PHP ÓïÑÔ±àд£¬»ùÓÚ MVC ģʽ£¨Model – View – Controller£©Éè¼Æ¡£Laravel ¿ò¼ÜÌṩÁËÐí¶à¹¤¾ßºÍº¯Êý£¬ÒÔʵÏÖ¾ßÓиßЧÐԺͿÉÀ©Õ¹Ð﵀ Web Ó¦ÓóÌÐò¡£
Blade Ä£°åÏÈÈÝ
Blade ÊÇ Laravel µÄÄ£°åÒýÇ棬ÊÇ»ùÓÚ PHP Blade ¹¹½¨µÄ¡£ Blade Ä£°åÖ§³ÖÀ©Õ¹£¬ÔÊÐíÄúʹÓÃĬÈϽṹ¡¢½ÚÔ¼Öظ´µÄ´úÂ룬²¢¿ÉÒÔÇáËɵØÓëºó¶ËÊý¾Ý½»»¥¡£
×îÏÈʵÏÖÉÌÆ·ÐÞ¸Ä
ÔÚʵÏÖÉÌÆ·ÐÞ¸ÄÇ°£¬ÎÒÃÇÐèҪȷ±£ÒѾÉèÖúà Laravel ÇéÐκÍÊý¾Ý¿âÅþÁ¬¡£½ÓÏÂÀ´£¬ÎÒÃǽ«Ê¹Óà Laravel µÄÄ£×ÓÀ´½«ÉÌÆ·Êý¾Ý×÷Ϊ¹¤¾ß´¦Öóͷ£¡£
½¨ÉèÉÌÆ·Ä£×Ó
ÔÚ Laravel ÖУ¬Ä£×Ó±»ÓÃÓÚÓëÊý¾Ý¿âµÄ½»»¥¡£Í¨¹ý creating ÊÂÎñºÍ saved ÊÂÎñ´¦Öóͷ£º¯Êý£¬ÎÒÃÇ¿ÉÒÔÔÚÉúÑÄÇ°ºÍÉúÑĺóÖ´ÐÐijЩ²Ù×÷¡£
Ê×ÏÈ£¬ÔÚ Laravel µÄÏÂÁîÐÐÖÐÖ´ÐÐÒÔÏÂÏÂÁÉèÒ»¸öеÄÄ£×Ó£º
php artisan make:model Product
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÔÚÄ£×ÓÖнç˵±íÃûºÍ¿ÉÌî³äÊôÐÔ¡£ÐÞ¸Ä app/Product.php ÎļþÈçÏ£º
<?php namespace App; use IlluminateDatabaseEloquentModel; class Product extends Model { protected $table = 'products'; protected $fillable = ['name', 'description', 'price', 'image']; }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÄ£×ÓÖУ¬ÎÒÃǽç˵Á˱íÃûΪ products£¬¿ÉÌî³ä£¨mass-assignable£©ÊôÐÔΪ name¡¢description¡¢price ºÍ image¡£
½¨ÉèÉÌÆ·¿ØÖÆÆ÷
ÔÚ Laravel ÖУ¬¿ØÖÆÆ÷ÊÇÓÃÓÚ´¦Öóͷ£ HTTP ÇëÇóµÄÀ࣬¿ÉÒÔ½«Óû§ÇëÇóÖ¸¶¨¸øÊʵ±µÄÒªÁì²¢·µ»Ø HTTP ÏìÓ¦¡£ÎÒÃÇÐèÒª½¨ÉèÒ»¸ö¿ØÖÆÆ÷²¢Ìí¼ÓÒªÁìÀ´´¦Öóͷ£ÉÌÆ·µÄÐ޸IJÙ×÷¡£
ÔÚÏÂÁîÐÐÖÐÖ´ÐÐÒÔÏÂÏÂÁîÌìÉúÒ»¸öеĿØÖÆÆ÷£º
php artisan make:controller ProductController --resource
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁî»áÔÚ app/Http/Controllers Ŀ¼Ï½¨ÉèÒ»¸ö ProductController.php Îļþ£¬²¢ÎªÎÒÃÇÌìÉú»ù±¾µÄ RESTful ×ÊÔ´ÒªÁ죨index¡¢create¡¢store¡¢show¡¢edit¡¢update¡¢destroy£©¡£
ÎÒÃǽ«ÔÚ¿ØÖÆÆ÷Ìí¼ÓÒ»¸ö edit ÒªÁìÀ´Õ¹Ê¾ÉÌÆ·ÐÞ¸Ä±íµ¥£¬Ò»¸ö update ÒªÁìÀ´´¦Öóͷ£±íµ¥Êý¾ÝºÍÉúÑĸüкóµÄÉÌÆ·ÐÅÏ¢¡£
ÐÞ¸Ä app/Http/Controllers/ProductController µÄ´úÂëÈçÏ£º
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppProduct; class ProductController extends Controller { public function edit($id) { $product = Product::findOrFail($id); return view('products.edit', compact('product')); } public function update(Request $request, $id) { $product = Product::findOrFail($id); $product->update($request->all()); return redirect('/products'); } }
µÇ¼ºó¸´ÖÆ
ÔÚ edit ÒªÁìÖУ¬ÎÒÃÇÅÌÎÊÖ¸¶¨ ID µÄÉÌÆ·²¢½«Æäת´ï¸øÊÓͼ£¬ÊÓͼ½«äÖȾÉÌÆ·ÐÅÏ¢ºÍÌṩһ¸öÐÞ¸Ä±íµ¥¡£ÔÚ update ÒªÁìÖУ¬ÎÒÃǸüÐÂÉÌÆ·ÐÅÏ¢²¢Öض¨Ïòµ½ÉÌÆ·ÁбíÒ³Ãæ¡£
½¨ÉèÉÌÆ·ÐÞ¸Ä±íµ¥
ÔÚ Laravel ÖУ¬¿ÉÒÔʹÓà Blade Ä£°æÒýÇæÀ´½¨Éè Web Ò³Ãæ¡£ ÏÂÃæÎÒÃǽ«½¨ÉèÒ»¸öÊÓͼÀ´ÈÃÓû§ÐÞ¸ÄÉÌÆ·ÐÅÏ¢¡£
½¨Éè resources/views/products/edit.blade.php Îļþ£¬Ìí¼ÓÒÔÏ´úÂ룺
{!! Form::model($product, ['method'=>'PATCH', 'action'=>['ProductController@update', $product->id], 'files'=>true]) !!} <div class="form-group"> {!! Form::label('name', 'ÉÌÆ·Ãû³Æ') !!} {!! Form::text('name', null, ['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('description', 'ÉÌÆ·ÐÎò') !!} {!! Form::text('description', null, ['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('price', 'ÉÌÆ·¼ÛÇ®') !!} {!! Form::text('price', null, ['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('image', 'ÉÌƷͼƬ') !!} {!! Form::file('image') !!} </div> <div class="form-group"> {!! Form::submit('ÉúÑÄ', ['class'=>'btn btn-primary']) !!} </div> {!! Form::close() !!}
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÄ£°åÖУ¬ÎÒÃÇʹÓÃÁË Laravel µÄ Form Builder ÌìÉú±íµ¥Î»ËØ¡£ÔÚÄ£°åÖУ¬ÎÒÃÇʹÓÃÁË $product ¹¤¾ßÀ´äÖȾÉÌÆ·ÐÅÏ¢£¬Í¨¹ý Form::model() ÒªÁ콫±íµ¥ÓëÄ£×Ó¹¤¾ß°ó¶¨ÆðÀ´£¬²¢½«Ìá½»Êý¾ÝµÄÇëÇóÒªÁìÉèÖÃΪ PATCH¡£
ÔÚ±íµ¥ÖУ¬ÎÒÃǽç˵ÁËÉÌÆ·µÄÃû³Æ¡¢ÐÎò¡¢¼ÛÇ®ºÍͼƬ×ֶΣ¬Í¬Ê±»¹ÉèÖÃÁËÉúÑÄ°´Å¥¡£µ±Óû§Ìá½»±íµ¥ºó£¬Êý¾Ý½«Ìá½»µ½ÉÌÆ·µÄ update ÒªÁìÖС£
·ÓÉÉèÖÃ
ÔÚ Laravel ÖУ¬Â·ÓÉÊÇÓÃÓÚÆ¥Åä URL ºÍ¿ØÖÆÆ÷ÒªÁìµÄ»úÖÆ¡£ÎÒÃÇÐèҪΪ edit ºÍ update ÒªÁìÌí¼Ó·ÓɹæÔò¡£
·¿ª routes/web.php Îļþ£¬Ìí¼ÓÒÔÏ´úÂ룺
Route::resource('products', 'ProductController'); Route::get('products/{id}/edit', 'ProductController@edit')->name('products.edit'); Route::patch('products/{product}', 'ProductController@update')->name('products.update');
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸ö´úÂëƬ¶ÏÖУ¬ÎÒÃÇʹÓÃ×ÊÔ´¿ØÖÆÆ÷µÄ Route::resource ÒªÁ콨ÉèÁËÉÌÆ·µÄ RESTful ×ÊԴ·ÓÉ¡£½Ó×Å£¬ÎÒÃÇΪ edit ÒªÁìºÍ update ÒªÁì»®·ÖÌí¼ÓÁË·ÓɹæÔò£¬ÔÊÐíÎÒÃÇÔÚä¯ÀÀÆ÷ÖÐͨ¹ýÏìÓ¦µÄ URL À´»á¼ûÕâЩҪÁì¡£
Íê³ÉÒÔÉϲÙ×÷ºó£¬ÎÒÃǾͿÉÒÔÔÚä¯ÀÀÆ÷Öлá¼û /products/{id}/edit µÄÒ³Ã棬¶ÔÉÌÆ·¾ÙÐÐÐÞ¸ÄÁË¡£
С½á
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏêϸÏÈÈÝÁËÔõÑùʹÓà Laravel 5.4 ¿ò¼ÜÀ´ÊµÏÖÉÌÆ·µÄÐ޸Ĺ¦Ð§¡£
ͨ¹ý½¨ÉèÉÌÆ·Ä£×ӺͿØÖÆÆ÷¡¢½¨ÉèÉÌÆ·ÐÞ¸Ä±íµ¥ÒÔ¼°Ìí¼Ó·ÓÉÉèÖã¬ÎÒÃÇÀֳɵؽ¨ÉèÁËÒ»¸ö»ùÓÚ Laravel µÄÉÌÆ·ÖÎÀíϵͳ£¬²¢ÇÒ¿ÉÒÔÀû±ãµØ¾ÙÐÐÉÌÆ·µÄÐ޸ĺÍÖÎÀí¡£
Laravel ¿ò¼ÜµÄ¿ìËÙ¿ª·¢ÄÜÁ¦ºÍ Blade Ä£°åÒýÇæµÄÎÞаÐÔ£¬ÉÁ¿ª·¢Õß¿ÉÒÔÇáËɹ¹½¨³öÒ»¸öÀû±ãÒ×Óá¢È«¹¦Ð§µÄ Web Ó¦ÓóÌÐò¡£Ç¿ÁÒÍƼöʹÓà Laravel 5.4 ¿ª·¢µçÉÌ Web Ó¦ÓóÌÐò£¬ÒÔ±ãΪ¿í´óÓû§Ìṩ¸üºÃµÄÌåÑé¡£
ÒÔÉϾÍÊÇlaravel5.4×öÉÌÆ·Ð޸ĵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡