laravel²î±ðurl²î±ð¿ØÖÆÆ÷
×÷Ϊһ¿îÊ¢ÐеÄphp¿ò¼Ü£¬laravel±»ÆÕ±éµØÓ¦ÓÃÓÚwebÓ¦ÓóÌÐòµÄ¿ª·¢¡£laravelÌṩÁËһϵÁеÄÇ¿Ê¢¹¦Ð§£¬ÆäÖаüÀ¨Ê¹Óòî±ðµÄurl»á¼û²î±ð¿ØÖÆÆ÷µÄ¹¦Ð§¡£Õâ¸ö¹¦Ð§¹ØÓÚ´óÐÍwebÓ¦ÓóÌÐòºÜÊÇÓÐÓã¬ÓÉÓÚËüÄܹ»Ê¹Ó¦ÓóÌÐòÔ½·¢ÎÞаºÍÒ×ÓÚά»¤¡£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒ½«ÏêϸÏÈÈÝlaravelÔõÑùʵÏÖ²î±ðµÄurl»á¼û²î±ðµÄ¿ØÖÆÆ÷¡£
ÔÚLaravelÖУ¬Ã¿¸öURL¶¼ÓÉ·ÓÉ´¦Àí¡£Â·ÓÉÊǽ«URLÓë¿ØÖÆÆ÷»òÐж¯ÒªÁì¾ÙÐÐÓ³ÉäµÄ»úÖÆ¡£ÎªÁËʵÏÖ²î±ðURL»á¼û²î±ð¿ØÖÆÆ÷µÄ¹¦Ð§£¬ÎÒÃÇÐèÒªÔÚ·ÓÉÖÐÉèÖöà¸öURL£¬Ê¹ËüÃÇ»®·ÖÖ¸Ïò²î±ðµÄ¿ØÖÆÆ÷¡£
ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃRoute::get()º¯ÊýÀ´½ç˵·ÓÉ¡£ÏÂÃæÊÇËüµÄ»ù±¾Óï·¨£º
Route::get('/url', 'Controller@method');
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÓï·¨ÖУ¬ÎÒÃÇÐèÒªÖ¸¶¨URLºÍ¿ØÖÆÆ÷µÄÒªÁì¡£URLÊÇWebÓ¦ÓóÌÐòÖеÄÖ÷»úÃûºóÃæµÄ·¾¶¡£Õâ¸ö·¾¶¿ÉÒÔÊǼòÆÓµÄ×Ö·û´®£¬ÀýÈç’/hello’£¬Ò²¿ÉÒÔ°üÀ¨²ÎÊý£¬ÀýÈç’/user/{id}’¡£¿ØÖÆÆ÷ÒªÁìÖ¸¶¨ÁË´¦ÀíÇëÇóµÄÂß¼´úÂë¡£
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÐí¶à·ÓÉ£¬²¢½«ËüÃÇ·ÖÅɸø²î±ðµÄ¿ØÖÆÆ÷¡£ÏÂÃæÊÇÒ»¸öʵÀý£¬ÆäÖÐÎÒÃǽç˵ÁËÁ½¸öURL£º’/home’ºÍ’/about’£¬²¢½«ËüÃÇ»®·Ö·ÖÅɸøÁ½¸ö²î±ðµÄ¿ØÖÆÆ÷¡£
//½ç˵·ÓÉ Route::get('/home', 'HomeController@index'); Route::get('/about', 'AboutController@index'); //½ç˵HomeController class HomeController extends Controller { public function index() { return view('home'); } } //½ç˵AboutController class AboutController extends Controller { public function index() { return view('about'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽç˵ÁËÁ½¸ö·ÓÉ£º’/home’ºÍ’/about’¡£ÕâÁ½¸ö·ÓÉ»®·ÖÖ¸Ïò²î±ðµÄ¿ØÖÆÆ÷HomeControllerºÍAboutController¡£Ã¿¸ö¿ØÖÆÆ÷¶¼ÓÐÒ»¸öÃûΪ’index’µÄÒªÁ죬Ëü´¦Àí¶ÔÓ¦µÄÇëÇó¡£ÔÚÕâ¸öÀý×ÓÖУ¬’index’ÒªÁì·µ»Ø²î±ðµÄÊÓͼ£¬»®·ÖΪ’home’ºÍ’about’¡£
ҪעÖصÄÊÇ£¬ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÎÒÃÇʹÓÃÁËLaravelµÄĬÈÏÊÓͼÎļþ´æ·ÅλÖ㬼´resources/viewsÎļþ¼Ð¡£ÔÚÕâ¸öÎļþ¼ÐÖУ¬ÎÒÃǽ¨ÉèÁËÁ½¸öÊÓͼÎļþ£ºhome.blade.phpºÍabout.blade.php¡£ÕâÁ½¸öÎļþ½«±»·µ»Ø¸ø¿Í»§¶Ë¡£
ÔÚÏÖʵӦÓóÌÐòÖУ¬ÎÒÃÇ¿ÉÄÜÐèÒªÔ½·¢ÖØ´óµÄ·ÓɺͿØÖÆÆ÷¡£ÀýÈ磬ÎÒÃÇ¿ÉÄÜÐèÒªÔÚURLÖаüÀ¨²ÎÊý£¬Ê¹ÓÿØÖÆÆ÷ÖеÄÄ£×ÓÀ´¼ìË÷Êý¾Ý£¬²¢½«Êý¾Ýת´ï¸øÊÓͼÎļþ¡£ÏÂÃæÊÇÒ»¸ö¸üÏÖʵµÄÀý×Ó£º
Route::get('/article/{id}', 'ArticleController@show'); class ArticleController extends Controller { public function show($id) { $article = Article::find($id); return view('article.show', ['article' => $article]); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö´øÓвÎÊýµÄURL£º’/article/{id}’¡£Õâ¸ö²ÎÊýÓÃÓÚÖ¸¶¨ÐèÒªÏÔʾµÄÎÄÕ¡£ÎÒÃÇʹÓÃÒ»¸öÃûΪ’show’µÄÒªÁìÀ´´¦ÀíÕâ¸öÇëÇó¡£ÔÚÕâ¸öÒªÁìÖУ¬ÎÒÃÇʹÓÃArticleÄ£×Ó´ÓÊý¾Ý¿âÖмìË÷ÎÄÕ¡£È»ºó£¬ÎÒÃǽ«Õâ¸öÎÄÕÂת´ï¸øÃûΪ’article.show’µÄÊÓͼ¡£
×ܽáÆðÀ´£¬Laravel¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖ²î±ðµÄURL»á¼û²î±ð¿ØÖÆÆ÷µÄ¹¦Ð§¡£ÎÒÃÇ¿ÉÒÔÔÚ·ÓÉÖÐÉèÖöà¸öURL£¬²¢½«ËüÃÇ»®·ÖÖ¸Ïò²î±ðµÄ¿ØÖÆÆ÷¡£ÔÚ¿ØÖÆÆ÷ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÊʵ±µÄÂß¼´úÂë´¦Àí¶ÔÓ¦µÄÇëÇó²¢·µ»ØÏìÓ¦µÄÊÓͼÎļþ¡£Õâ¸ö¹¦Ð§¹ØÓÚ´óÐÍÓ¦ÓóÌÐòµÄ¿ª·¢ºÜÊÇÓÐÓã¬ÓÉÓÚËüʹÎÒÃǵĴúÂëÔ½·¢ÎÞаºÍÒ×ÓÚά»¤¡£
ÒÔÉϾÍÊÇlaravel²î±ðurl²î±ð¿ØÖÆÆ÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡