laravelÔõôÒþ²Øid
Ëæ×ÅwebÓ¦ÓóÌÐòµÄÉú³¤£¬Ô½À´Ô½¶àµÄÈË×îÏÈʹÓÿò¼ÜÀ´¹¹½¨×Ô¼ºµÄÓ¦ÓóÌÐò£¬ÆäÖÐÒ»¸öºÜÊ¢ÐеĿò¼ÜÊÇlaravel¡£laravelÌṩÁËÐí¶àÀû±ã¿ª·¢µÄ¹¦Ð§ºÍ¹¤¾ß£¬ÆäÖаüÀ¨Ò»¸ö¿ÉÒÔÒþ²ØidµÄ¹¦Ð§£¬Õâ¹ØÓÚһЩÇå¾²ÐÔÒªÇó½Ï¸ßµÄÓ¦ÓóÌÐòÀ´ËµºÜÊÇÓÐÓá£
ÔÚÐí¶àÓ¦ÓóÌÐòÖУ¬Êý¾Ý¼Í¼µÄÖ÷¼üͨ³£¶¼ÊÇÒ»¸öÊý×Öid£¬ÕâÑùÒ×ÓÚÖÎÀíºÍ¼ìË÷Êý¾Ý¡£È»¶ø£¬ÓÐʱ¼äÎÒÃÇÐèÒª±£»¤ÕâЩÊý¾Ý¼Í¼ÒÔ±ÜÃâËüÃDZ»ÈÝÒ׵ر»Ì»Â¶³öÀ´¡£ÀýÈ磬ÈôÊÇÎÒÃǵÄÊý¾Ý¼Í¼°üÀ¨Ãô¸ÐÐÅÏ¢£¬ÎÒÃÇÏ£Íû½öÔÊÐíÒÑÈÏÖ¤Óû§»á¼û¡£
ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÒ»¸ö³ÆΪ·ÓÉÄ£×Ӱ󶨵Ĺ¦Ð§À´Òþʽת´ïidÖµ¡£ÕâÒâζ×ÅÎÒÃÇ¿ÉÒÔÔÚURLÖÐÒþ²Øid£¬¶øÈÔÈ»¿ÉÒÔʹÓÃËüÀ´ÅÌÎÊÊý¾Ý¼Í¼¡£ÈÃÎÒÃÇÀ´¿´¿´ÕâÊÇÔõÑùÊÂÇéµÄ¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÎÒÃǵÄÄ£×ÓÖнç˵һ¸ö·Óɼü£¬Õ⽫ÊÇÎÒÃÇÒþʽʹÓõÄ×ֶΡ£ÔÚÎÒÃǵÄʾÀýÖУ¬ÎÒÃǽ« ʹÓÃslug×Ö¶ÎÀ´Ê¶±ðÎÒÃǵÄÊý¾Ý¼Í¼¡£
class Post extends Model { public function getRouteKeyName() { return 'slug'; } }
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª¸üÐÂÎÒÃǵÄ·Óɽç˵£¬ÒÔʹÓÃÎÒÃǵÄÄ£×ӺͷÓɼü¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ·ÓÉÀ´ÏÔʾһ¸öÌû×Ó£º
Route::get('/posts/{post}', function (Post $post) { return view('post', compact('post')); });
µÇ¼ºó¸´ÖÆ
Õ⽫¸øÎÒÃǵÄÓ¦ÓóÌÐòÌṩÁËÒ»¸öURL£¬Ëü½«ÏÔʾidΪ1µÄÌû×Ó£º
http://example.com/posts/1
µÇ¼ºó¸´ÖÆ
È»¶ø£¬ÎªÁËÒþ²Øid£¬ÎÒÃÇ¿ÉÒÔ¸üÐÂÎÒÃǵÄ·Óɽç˵£¬Ê¹ÓÃÒ»¸öslugÀ´Ìæ»»id¡£ÀýÈ磺
Route::get('/posts/{post:slug}', function (Post $post) { return view('post', compact('post')); });
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔʹÓÃslug×Ö¶ÎÀ´È¡´úidÔÚÎÒÃǵÄURLÖС£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂURLÀ´ÏÔʾÏàͬµÄÌû×Ó£º
http://example.com/posts/my-first-post
µÇ¼ºó¸´ÖÆ
µ±ÎÒÃÇ·¿ª¸ÃURLʱ£¬Laravel½«Ê¹ÓÃÎÒÃǵÄÄ£×ÓÀ´²éÕÒ¶ÔÓ¦ÓÚslugΪ¡°my-first-post¡±µÄÌû×Ó£¬²¢½«Æä×÷Ϊ²ÎÊýת´ï¸øÎÒÃǵĿØÖÆÆ÷¡£ÎÒÃÇ¿ÉÒÔʹÓÃÓëÒÔÇ°Ò»ÑùµÄ·½·¨À´»á¼û¸ÃÌû×ÓµÄÊôÐÔºÍÒªÁ죬ÀýÈ磺
<h1>{{ $post->title }}</h1> <p>{{ $post->content }}</p>
µÇ¼ºó¸´ÖÆ
ʹÓÃÕâÖÖ·½·¨£¬ÎÒÃÇ¿ÉÒÔÔÚÎÒÃǵÄÓ¦ÓóÌÐòÖÐÒþ²Øid²¢Ê¹ÓÃÈ¡´ú×ֶΡ£ÕâÔÚһЩӦÓóÌÐòÖкÜÊÇÓÐÓã¬ÀýÈ磺
±ÜÃâÓû§´ÓURLÖÐÖ±½Ó»á¼ûÃô¸ÐÊý¾Ý
ÔöÇ¿Ó¦ÓóÌÐòµÄÇå¾²ÐÔ£¬È·±£Ö»ÓÐÊÚȨ»á¼û²Å»ª»ñµÃÊý¾Ý
Ìá¸ßÓû§ÓѺÃÐÔ£¬Ê¹ÓøüºÃ£¬¸ü¿É¶ÁµÄURL
×ÜÖ®£¬Laravel¿ò¼ÜÌṩÁËÐí¶àÓÐÓõŤ¾ßºÍ¹¦Ð§£¬Ê¹Ó÷ÓÉÄ£×Ӱ󶨿ÉÒÔ×ÊÖúÎÒÃÇÒþ²ØID²¢ÔöÇ¿ÎÒÃǵÄÓ¦ÓóÌÐòµÄÇå¾²ÐÔºÍÓû§ÓѺÃÐÔ¡£ÈôÊÇÄúÊÇÒ»ÃûLaravel¿ª·¢Ö°Ô±£¬½¨ÒéÄúʵÑéʹÓÃÕâÒ»ÊÖÒÕ£¬ÒÔÌá¸ßÄúµÄÓ¦ÓóÌÐò×ÜÌåÖÊÁ¿¡£
ÒÔÉϾÍÊÇlaravelÔõôÒþ²ØidµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡