laravel·ÓÉǰ׺Ôõô»á¼û
laravel ÊÇÒ»¸öÊ¢ÐÐµÄ php ¿ò¼Ü£¬ËüʹÓÃÁËÓÅÑŵÄÓï·¨ºÍÇ¿Ê¢µÄ¹¤¾ßÀ´×ÊÖú¿ª·¢Õß¿ìËÙ¹¹½¨ web Ó¦ÓóÌÐò¡£ÔÚ laravel ÖУ¬Â·ÓÉÊǺÜÊÇÖ÷ÒªµÄÒ»¸ö¿´·¨£¬ËüÔÊÐí¿ª·¢Õß½ç˵ӦÓóÌÐòÖÐµÄ url£¬²¢Ö¸¶¨Ó¦ÓóÌÐòÖеÄÏìÓ¦´¦Àí³ÌÐò¡£ÔÚÐí¶àÇéÐÎÏ£¬ÎÒÃÇÐèҪΪÎÒÃǵÄÓ¦ÓóÌÐòÖеÄÒ»×éÏà¹Ø·ÓÉÌí¼ÓÒ»¸öͳһµÄǰ׺£¬Í¨³£ÊÇÓÉÓÚËüÃǶ¼ÊôÓÚͳһ¸öÄ£¿é»ò¹¦Ð§Çø¡£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃǽ«Ñ§Ï°ÔõÑùÔÚ laravel Öлá¼û´øÓзÓÉǰ׺µÄ·ÓÉ¡£
ÉèÖ÷ÓÉǰ׺
ÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà Route ÀàµÄ prefix ÒªÁìÀ´ÎªÒ»×é·ÓÉÌí¼Óǰ׺¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬ÆäÖÐÎÒÃǽç˵ÁËÒ»¸ö´øÓÐǰ׺µÄ·ÓÉ£º
Route::prefix('admin')->group(function () { Route::get('dashboard', function () { return view('admin.dashboard'); }); Route::get('users', function () { return view('admin.users'); }); });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓà Route ÀàµÄ prefix ÒªÁìÀ´½ç˵ÁË·ÓÉǰ׺Ϊ ‘admin’¡£ÎÒÃÇʹÓÃÁËÒ»¸öÄäÃûº¯ÊýÀ´½ç˵ÁËÁ½¸ö´øÓÐǰ׺µÄ·ÓÉ£¬»®·ÖÊÇ ‘/admin/dashboard’ ºÍ ‘/admin/users’¡£ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǵÄǰ׺ÊÇ ‘admin’£¬Òò´Ë£¬ËùÓдøÓÐǰ׺µÄ·ÓÉÐèÒªÔÚ URL ÖÐÌí¼Ó ‘admin’¡£ÀýÈ磬Ҫ»á¼û ‘/admin/dashboard’ Ò³Ã棬ÎÒÃÇÐèÒªÔÚ URL ÖÐÊäÈë http://example.com/admin/dashboard¡£
ʹÓÃÃüÃû·ÓÉ
³ýÁËʹÓà Route ÀàµÄ prefix ÒªÁìÒÔÍ⣬ÎÒÃÇ»¹¿ÉÒÔʹÓ÷ÓɵÄÃüÃûÀ´ÎªÒ»×é·ÓÉÌí¼Óǰ׺¡£¼ÙÉèÎÒÃÇÓÐÒ»¸öÃüÃûǰ׺Ϊ ‘admin.’ µÄ·ÓÉ×飬ÄÇôÎÒÃÇ¿ÉÒÔÔÚ½ç˵Ïêϸ·ÓÉʱʹÓÃÕâ¸öǰ׺À´»á¼û·ÓÉ¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º
Route::name('admin.')->group(function () { Route::get('dashboard', function () { return view('admin.dashboard'); })->name('dashboard'); Route::get('users', function () { return view('admin.users'); })->name('users'); });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁË Route ÀàµÄ name ÒªÁìÀ´½ç˵ÁËÒ»¸öǰ׺Ϊ ‘admin.’ µÄÃüÃû·ÓÉ×é¡£ÀàËÆÓÚʹÓÃǰ׺ҪÁ죬ÃüÃû·ÓÉ×éÊÇÒ»×é·ÓÉÃû³ÆµÄǰ׺¡£ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃÇʹÓÃÁË ‘admin.’ ǰ׺À´ÎªËùÓдøÓÐǰ׺µÄ·ÓÉÃüÃû£¬°üÀ¨ ‘admin.dashboard’ ºÍ ‘admin.users’¡£Òª»á¼û´øÓÐǰ׺µÄ·ÓÉ£¬ÎÒÃÇ¿ÉÒÔʹÓ÷ÓÉÃû³ÆÀ´¹¹½¨ URL£¬ÀýÈ磬Ҫ»á¼û ‘admin.dashboard’ Ò³Ã棬ÎÒÃÇ¿ÉÒÔÔÚ URL ÖÐÊäÈë http://example.com/dashboard¡£
ÔÚÓ¦ÓóÌÐòÖлá¼ûǰ׺·ÓÉ
ÏÖÔÚÎÒÃÇÒѾ½ç˵ÁË´øÓÐǰ׺µÄ·ÓÉ£¬½ÓÏÂÀ´ÎÒÃÇ¿ÉÒÔÔÚÓ¦ÓóÌÐòÖлá¼ûÕâЩ·ÓÉ¡£ÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà Laravel µÄĬÈÏ URL ×ÊÖúº¯Êý¿ìËÙ¹¹½¨´øÓÐǰ׺µÄ URL¡£ÎÒÃÇ¿ÉÒÔʹÓà url »ò route º¯ÊýÀ´¹¹½¨ URL£¬Ïêϸȡ¾öÓÚÎÒÃÇÊÇÒªÌìÉúÒ»¸öÍêÕûµÄ URL ÕÕ¾ÉÌìÉúÒ»¸öÖ¸Ïò·ÓÉµÄ URL¡£
ʹÓà url º¯Êý
ÈôÊÇÎÒÃÇÏëÒªÌìÉúÍêÕûµÄ URL£¬ÎÒÃÇ¿ÉÒÔʹÓà Laravel µÄ url º¯Êý¡£ÏÂÃæÊÇÒ»¸öʹÓà url º¯ÊýµÄʾÀý£º
$url = url('/admin/dashboard');
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓà url º¯ÊýÌìÉú’/admin/dashboard’ Ò³ÃæµÄÍêÕû URL¡£ÈôÊÇÎÒÃÇÓÐÒ»¸öǰ׺Ϊ ‘admin’ µÄ·ÓÉ£¬ÔòÎÒÃÇÐèÒªÔÚ URL ÖÐÌí¼Ó¸Ãǰ׺¡£
ʹÓà route º¯Êý
ÈôÊÇÎÒÃÇÏëÒªÌìÉúÖ¸Ïò´øÓÐǰ׺µÄ·ÓÉµÄ URL£¬ÎÒÃÇ¿ÉÒÔʹÓà Laravel µÄ route º¯Êý¡£ÏÂÃæÊÇÒ»¸öʹÓà route º¯ÊýµÄʾÀý£º
$url = route('admin.dashboard');
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓà route º¯ÊýÌìÉúÁËÖ¸Ïò ‘admin.dashboard’ Ò³ÃæµÄ URL¡£ÈôÊÇÎÒÃÇÓÐÒ»¸öǰ׺Ϊ ‘admin’ µÄÃüÃû·ÓÉ×飬ÔòÎÒÃÇ¿ÉÒÔÔÚ·ÓÉÃû³ÆÖмÓÈë¸Ãǰ׺¡£
×ܽá
ÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà Route ÀàµÄ prefix ÒªÁìºÍÃüÃû·ÓÉÀ´ÎªÒ»×é·ÓÉÌí¼Óǰ׺¡£ÎÞÂÛÎÒÃÇʹÓÃÄÄÖÖ·½·¨£¬ÎÒÃǶ¼¿ÉÒÔʹÓà Laravel µÄ url ºÍ route º¯Êý¿ìËÙ¹¹½¨´øÓÐǰ׺µÄ URL¡£Ï£Íû±¾ÎÄ¿ÉÒÔ×ÊÖúÄãÃ÷È·ÔÚ Laravel Öлá¼û´øÓÐǰ׺µÄ·ÓɵÄÒªÁì¡£
ÒÔÉϾÍÊÇlaravel·ÓÉǰ׺Ôõô»á¼ûµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡