laravel ¶¯Ì¬ÉèÖÃÖ÷Ìâ
ÔÚwebÓ¦ÓóÌÐò¿ª·¢ÖУ¬Ö÷ÌâÊÇÒ»¸öÖ÷ÒªµÄ¿´·¨¡£Ö÷Ìâ¿ÉÒÔÓÃÀ´¸Ä±äwebÓ¦ÓóÌÐòµÄÍâ¹ÛºÍ¸ÐÊÜ£¬°üÀ¨ÑÕÉ«¼Æ»®¡¢Å侰ͼƬ¡¢×ÖÌåºÍ½á¹¹µÈ¡£Ðí¶àwebÓ¦ÓóÌÐò¶¼ÓжàÖÖÖ÷Ìâ¿É¹©Ñ¡Ôñ£¬²î±ðµÄÖ÷Ìâ¿ÉÒÔ˳Ӧ²î±ðµÄÓû§ÐèÇóºÍ²î±ðµÄ³¡¾°¡£laravel×÷Ϊһ¸öÊ¢ÐеÄphp webÓ¦ÓóÌÐò¿ò¼Ü£¬×ÔȻҲΪÖ÷ÌâÇл»ÌṩÁËÖ§³Ö¡£
LaravelÖ÷ÌâÊÇÖ¸ÓÉÍøÕ¾µÄÉè¼Æʦ»òÍøÕ¾¿ª·¢Õß×Ô½ç˵µÄÒ»×éÆøÑæÆøÑæºÍÑùʽ£¬ÓÃÓÚ·ºÆðÍøÕ¾µÄÍâ¹ÛºÍ½á¹¹¡£LaravelÖ÷Ìâ¿ÉÒÔ×ÊÖúWebÓ¦ÓóÌÐò¸üÈÝÒ×µØʵÏÖ×Ô¼ºµÄÆæÒìÍâ¹ÛºÍÓû§ÌåÑé¡£Ðí¶àWebÓ¦ÓóÌÐò¶¼Ö§³ÖÇл»Ö÷Ì⣬Óû§¿ÉÒÔƾ֤×Ô¼ºµÄϲ»¶Ñ¡Ôñϲ»¶µÄÖ÷Ìâ¡£
LaravelÖ÷Ìâ¿ÉÒÔͨ¹ý¶àÖÖ·½·¨ÊµÏÖ¡£×î»ù±¾µÄÒªÁìÊÇΪÿ¸öÖ÷Ì⽨Éè²î±ðµÄÊÓͼÎļþ¼Ð£¬°üÀ¨²î±ðµÄCSS¡¢JSºÍͼƬµÈ×ÊÔ´Îļþ¡£Ã¿¸öÖ÷ÌⶼÓÐÒ»¸ö×ÔÁ¦µÄĿ¼£¬Æ¾Ö¤Óû§µÄÑ¡ÔñÀ´¼ÓÔØCSS¡¢JSºÍͼƬÎļþ¡£¿ÉÊÇ£¬ÕâÖÖÒªÌå»áÔöÌíÐí¶àÖظ´µÄÊÂÇéÁ¿£¬ÓÉÓÚÿ¸öÖ÷ÌⶼÐèÒª½¨ÉèÒ»¸ö×ÔÁ¦µÄĿ¼ºÍ×ÊÔ´Îļþ¡£²¢ÇÒ£¬µ±Óû§ÏëÒªÇл»Ö÷Ìâʱ£¬ÐèÒªÊÖ¶¯Ð޸ĴúÂ룬¸üÐÂÖ÷Ìâ·¾¶ºÍ×ÊÔ´Îļþ·¾¶¡£
ΪÏàʶ¾öÕâ¸öÎÊÌ⣬Laravel¿ª·¢ÍŶÓÌṩÁËÒ»ÖÖ¶¯Ì¬ÉèÖÃÖ÷ÌâµÄ¼Æ»®¡£Í¨¹ýÕâÖּƻ®£¬LaravelÓ¦ÓóÌÐò¿ÉÒÔÔÚ²»ÖØмÓÔØÒ³ÃæµÄÇéÐÎÏÂʵÏÖÖ÷ÌâÇл»¡£ÏêϸÀ´Ëµ£¬ÕâÖּƻ®ÊÇ»ùÓÚSession»òCookieµÄ´æ´¢»úÖÆ£¬ÓÃÓÚÉúÑÄÓû§µÄÖ÷ÌâÑ¡Ôñ¡£µ±Óû§Ñ¡Ôñ²î±ðµÄÖ÷Ìâʱ£¬»á½«Ö÷ÌâÃû³Æ´æ´¢µ½Session»òCookieÖУ¬¸ÃÖµ»áÔÚºóÐøÒ³ÃæÇëÇóÖг¤ÆÚ»¯ÉúÑÄ¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄLaravelÖ÷ÌâʾÀý£¬ÑÝʾÁËÔõÑùͨ¹ýSession»òCookie»úÖÆʵÏÖÖ÷ÌâÇл»£º
1.½¨ÉèÖ÷ÎÊÌâ¼
Ê×ÏÈ£¬ÐèÒªÔÚLaravelÓ¦ÓóÌÐòµÄpublicĿ¼Ï½¨ÉèÒ»¸öthemesĿ¼£¬ÓÃÓÚ´æ´¢ËùÓеÄÖ÷ÎÊÌ⼺Í×ÊÔ´Îļþ¡£
public/ ©¸©¤©¤ themes/ ©À©¤©¤ theme_1/ ©¦ ©À©¤©¤ css/ ©¦ ©À©¤©¤ js/ ©¦ ©¸©¤©¤ images/ ©¸©¤©¤ theme_2/ ©À©¤©¤ css/ ©À©¤©¤ js/ ©¸©¤©¤ images/
µÇ¼ºó¸´ÖÆ
ÔÚÿ¸öÖ÷ÎÊÌâ¼Öж¼°üÀ¨ÁËÒ»¸ö×ÓĿ¼£¬ÓÃÓÚ´æ´¢CSS¡¢JSºÍͼƬµÈ×ÊÔ´Îļþ¡£Ã¿¸ö×ÓĿ¼¶¼°üÀ¨Í¬ÃûµÄ×ÊÔ´Îļþ£¬ÀýÈçtheme_1Ŀ¼ÖеÄcssĿ¼°üÀ¨µÄÊÇtheme_1.cssÎļþ¡£
2.½ç˵·ÓÉ
ÔÚLaravelÓ¦ÓóÌÐòµÄroutesĿ¼Ï£¬½ç˵һ¸öÓÃÓÚ´¦ÀíÖ÷ÌâÇл»ÇëÇóµÄ·ÓÉ¡£Â·ÓÉ¿ÉÒÔƾ֤²î±ðµÄHTTPÒªÁì¾ÙÐÐÏìÓ¦µÄ´¦Àí£¬ÀýÈçGET¡¢POSTµÈ¡£µ±Óû§Ñ¡ÔñÖ÷Ìâʱ£¬LaravelÓ¦ÓóÌÐò»á½«Ö÷ÌâÑ¡Ôñ´æ´¢µ½Session»òCookieÖУ¬Ö®ºó»áͨ¹ýÕâ¸ö·ÓÉÀ´»ñÈ¡Ä¿½ñÉ趨µÄÖ÷Ìâ¡£
// Ö÷ÌâÇл»Â·ÓÉ Route::get('/theme/{name}', function ($name) { // ½«Ö÷ÌâÃû³ÆÉúÑĵ½ Session »ò Cookie ÖÐ if (request()->hasCookie('theme')) { return response()->cookie('theme', $name); } else { session(['theme' => $name]); } return redirect()->back(); });
µÇ¼ºó¸´ÖÆ
3.½ç˵ÖÐÐļþ
ΪÁËÔÚLaravelÓ¦ÓóÌÐòÖÐʵÏÖÖ÷ÌâÇл»»úÖÆ£¬»¹Ðè½ç˵һÆäÖÐÐļþ¡£ÖÐÐļþƾ֤Session»òCookieÖеÄÖµÀ´¼ÓÔØÏìÓ¦µÄCSS¡¢JSºÍͼƬµÈ×ÊÔ´Îļþ¡£ÏÂÃæÊÇÖÐÐļþµÄ´úÂëʾÀý£º
namespace AppHttpMiddleware; use Closure; use IlluminateSupportFacadesCookie; use IlluminateSupportFacadesStorage; class LoadTheme { /** * Handle an incoming request. * * @param IlluminateHttpRequest $request * @param Closure $next * @return mixed */ public function handle($request, Closure $next) { // ¼ì²é Session »ò Cookie ÖÐÊÇ·ñ±£´æ theme Ñ¡Ïî if ($request->session()->has('theme')) { $theme = $request->session()->get('theme'); } elseif ($request->hasCookie('theme')) { $theme = $request->cookie('theme'); } else { $theme = 'default'; } // ʵÑé¼ÓÔØÖ÷Ìâ CSS Îļþ $css = public_path(sprintf('themes/%s/css/%s.css', $theme, $theme)); if (file_exists($css)) { $css = asset(sprintf('themes/%s/css/%s.css', $theme, $theme)); echo '<link rel="stylesheet" href="'.$css.'">'; } // ʵÑé¼ÓÔØÖ÷Ìâ JS Îļþ $js = public_path(sprintf('themes/%s/js/%s.js', $theme, $theme)); if (file_exists($js)) { $js = asset(sprintf('themes/%s/js/%s.js', $theme, $theme)); echo '<script src="'.$js.'"></script>'; } // ʵÑé¼ÓÔØÖ÷ÌâͼƬÎļþ $image_path = public_path(sprintf('themes/%s/images/', $theme)); if (is_dir($image_path)) { Storage::disk('public')->makeDirectory(sprintf('themes/%s/images/', $theme)); } $images = Storage::disk('public')->files(sprintf('themes/%s/images/', $theme)); foreach ($images as $image) { $image_url = Storage::disk('public')->url($image); echo '<img src="'.$image_url.'" alt="laravel ¶¯Ì¬ÉèÖÃÖ÷Ìâ" >'; } return $next($request); } }
µÇ¼ºó¸´ÖÆ
¸ÃÖÐÐļþ»á¼ì²éSession»òCookieÖÐÊÇ·ñÓÐÖ÷ÌâÊôÐÔ£¬²¢ÊµÑé¼ÓÔضÔÓ¦µÄCSS¡¢JSºÍͼƬµÈ×ÊÔ´Îļþ¡£ÈôÊÇÕÒ²»µ½¶ÔÓ¦µÄÎļþ£¬¾Í»á¼ÓÔØĬÈÏÖ÷Ìâ¡£
4.×¢²áÖÐÐļþ
ÔÚLaravelÓ¦ÓóÌÐòÖÐ×¢²á×Ô½ç˵µÄÖÐÐļþ£¬ÎªËùÓÐÇëÇóÆôÓÃÖ÷ÌâÇл»»úÖÆ¡£LaravelµÄÖÐÐļþ×¢²á»úÖÆ¿ÉÒÔÓÃÓÚÈ«¾ÖºÍ¾Ö²¿ÖÐÐļþ¡£
// ×¢²áÈ«¾ÖÖÐÐļþ // ÔÚ app/Http/Kernel.php ÖÐÕÒµ½ $middlewareGroups Êý×飬¼ÓÈëÖÐÐļþ 'web' => [ AppHttpMiddlewareLoadTheme::class, // ÆäËüÖÐÐļþ ],
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬ËùÓнøÈëÓ¦ÓóÌÐòµÄÇëÇ󶼻á×Ô¶¯ÆôÓÃÖ÷ÌâÇл»¹¦Ð§¡£
ÒÔÉÏÊÇʹÓÃLaravelʵÏÖ¶¯Ì¬ÉèÖÃÖ÷ÌâµÄʾÀý¡£LaravelµÄÖÐÐļþ»úÖÆʹµÃʵÏÖÖ÷ÌâÇл»±äµÃÈÝÒ×ÇÒÎÞа¡£Í¨¹ýÕâÖÖ·½·¨£¬²»µ«¿ÉÒÔÌá¸ßÓ¦ÓóÌÐòµÄÓû§ÌåÑ飬»¹¿ÉÒÔÈÃÓû§Ô½·¢Öª×ã²¢Ìá¸ßWebÓ¦ÓõÄʹÓöȡ£
ÒÔÉϾÍÊÇlaravel ¶¯Ì¬ÉèÖÃÖ÷ÌâµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡