̽ÌÖÔõÑùÐÞ¸ÄLaravelÏîÄ¿ÖÐViewÎļþµÄ·¾¶
laravelÊÇÒ»¿îºÜÊÇÊ¢ÐеÄphp web¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÐí¶à±ã½ÝµÄ¹¤¾ßºÍÌØÕ÷À´¼ò»¯¿ª·¢Àú³Ì¡£ÆäÖÐviewÊÇlaravelÓ¦ÓóÌÐòÖеÄÒ»¸öÖ÷Òª×é³É²¿·Ö¡£viewÈÏÕæÊä³öËùÓÐwebÒ³ÃæµÄhtml´úÂë¡£ÔÚĬÈÏÇéÐÎÏ£¬laravelµÄviewÎļþ´æ·ÅÔÚresources/viewsĿ¼ÖС£¿ÉÊÇ£¬ÔÚÏÖʵÏîÄ¿ÖУ¬¿ÉÄÜÐèÒªÐÞ¸ÄÕâЩviewÎļþµÄ·¾¶£¬ÒÔ±ãÓëÏîÄ¿½á¹¹ºÍ×éÖ¯·½·¨Ô½·¢ÆõºÏ¡£±¾ÆªÎÄÕ½«Ì½ÌÖÔõÑùÐÞ¸ÄlaravelÓ¦ÓóÌÐòÖÐviewÎļþµÄ·¾¶¡£
ÔÚconfig/view.phpÎļþÖÐÐÞ¸ÄĬÈÏViewÎļþ·¾¶
LaravelÌṩÁËÒ»¸öÉèÖÃÎļþconfig/view.php£¬ÆäÖаüÀ¨ÁËViewsµÄĬÈÏ·¾¶¡£Ä¬ÈÏÇéÐÎÏ£¬Õâ¸öÎļþÖеĒpaths’Êý×éÖ»°üÀ¨ÁËresources/viewsĿ¼£¬ÈçÏÂËùʾ£º
'paths' => [ resource_path('views'), ],
µÇ¼ºó¸´ÖÆ
ÈôÊÇÐèÒª½«ViewÎļþ·¾¶ÐÞ¸ÄΪÆäËû·¾¶£¬Ö»ÐèÔÚ’paths’Êý×éÖÐÌí¼Óеķ¾¶¼´¿É¡£ÀýÈ磬Ҫ½«ViewÎļþ·¾¶ÐÞ¸ÄΪresources/views/templatesĿ¼£¬¿ÉÒÔÐÞ¸ÄÉèÖÃÎļþÈçÏ£º
'paths' => [ resource_path('views/templates'), ],
µÇ¼ºó¸´ÖÆ
´Ëʱ£¬ÔÚLaravelÓ¦ÓóÌÐòÖÐʹÓÃView::make()ÒªÁìäÖȾһ¸öViewʱ£¬Ö»ÐèÖ¸¶¨ÏìÓ¦µÄViewÃû³Æ£¬¶ø²»ÐèÒªÌṩÍêÕûµÄView·¾¶¡£ÀýÈ磬ÈôÊÇÓÐÒ»¸öViewÎļþresources/views/templates/home.blade.php£¬¾Í¿ÉÒÔʹÓÃÒÔÏ´úÂëÔÚ¿ØÖÆÆ÷ÖÐäÖȾ¸ÃView£º
return View::make('home');
µÇ¼ºó¸´ÖÆ
ÔÚServiceProviderÖÐ×Ô½ç˵View·¾¶
ÔÚijЩÇéÐÎÏ£¬ÐèҪΪ²î±ðµÄ¹¦Ð§Ä£¿é»®·ÖÖ¸¶¨²î±ðµÄViewÎļþ·¾¶¡£´Ëʱ£¬¿ÉÒÔͨ¹ý×Ô½ç˵ServiceProviderÀ´ÊµÏÖ¡£
Ê×ÏÈ£¬ÔÚapp/ProvidersĿ¼Ï½¨ÉèÒ»¸öеÄServiceProvider£¬ÀýÈçCustomViewServiceProvider¡£È»ºó£¬ÔÚregister()ÒªÁìÖÐʹÓÃViewÀàµÄaddLocation()ÒªÁìÀ´Ö¸¶¨ÐµÄView·¾¶¡£ÀýÈ磬ÒÔÏ´úÂ뽫View·¾¶Ìí¼Óµ½resources/views/templatesĿ¼£º
use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\View; class CustomViewServiceProvider extends ServiceProvider { public function register() { View::addLocation(resource_path('views/templates')); } }
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÔÚconfig/app.phpÎļþÖн«×Ô½ç˵µÄServiceProviderÌí¼Óµ½providersÊý×éÖУº
'providers' => [ // Other Service Providers App\Providers\CustomViewServiceProvider::class, ],
µÇ¼ºó¸´ÖÆ
Íê³ÉÒÔÉÏ°ì·¨ºó£¬¾Í¿ÉÒÔÔÚLaravelÓ¦ÓóÌÐòÖÐʹÓÃView::make()ÒªÁìÀ´äÖÎʶ¦¶¨Ä£°åÎļþ¡£
ʹÓÃBladeÄ£°åÖеÄ@extendÖ¸ÁîÀ´¼ÌÐøÆäËûÄ£°å
LaravelµÄBladeÄ£°åÒýÇæÌṩÁ˺ÜÊÇÇ¿Ê¢µÄÄ£°å¼ÌÐø¹¦Ð§¡£Ê¹ÓÃ@extendÖ¸Áî¿ÉÒÔÈÃÒ»¸öÄ£°å¼ÌÐøÁíÒ»¸öÄ£°å£¬²¢ÔÚÆä»ù´¡ÉÏÌí¼Ó»òÐÞ¸ÄһЩÄÚÈÝ¡£Í¨¹ýÕâÖÖÒªÁ죬¿ÉÒÔÓÐÓõØ×éÖ¯ºÍÖÎÀíViewÎļþ¡£
ÀýÈ磬ÏÂÃæÊÇÒ»¸ö¼ÌÐødefaultÄ£°åµÄhomeÄ£°å£º
// defaultÄ£°å nbsp;html> <title>@yield('title')</title> <div> @yield('content') </div> // homeÄ£°å @extends('default') @section('title', 'Home Page') @section('content') <h1>Welcome home!</h1> <p>This is the home page.</p> @endsection
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÀý×ÓÖУ¬homeÄ£°åͨ¹ý¼ÌÐødefaultÄ£°åÀ´Í³Ò»ÖÎÀíºÍά»¤ÏàͬµÄ²¿·Ö¡£ÈôÊÇÐèÒªÐÞ¸ÄdefaultÄ£°åµÄ·¾¶£¬Ö»Ð轫ÆäÒƶ¯µ½ÐµÄĿ¼ÖУ¬È»ºóÏìÓ¦µØÐÞ¸Ä@extendÖ¸Áî¼´¿É¡£
×ܽá
LaravelµÄViewÊÇWebÓ¦ÓóÌÐòÖкÜÊÇÖ÷ÒªµÄÒ»²¿·Ö¡£ÐÞ¸ÄViewÎļþµÄ·¾¶¿ÉÒÔ×ÊÖú¸üºÃµØÖÎÀíºÍ×éÖ¯Ó¦ÓóÌÐòµÄ×ÊÔ´Îļþ¡£ÕâƪÎÄÕÂÏÈÈÝÁËÈýÖÖÐÞ¸ÄLaravelÓ¦ÓóÌÐòµÄViewÎļþ·¾¶µÄÒªÁ죬°üÀ¨ÐÞ¸ÄÉèÖÃÎļþ¡¢×Ô½ç˵ServiceProviderºÍʹÓÃBladeÄ£°åÒýÇæµÄ¼ÌÐø¹¦Ð§¡£ÎÞÂÛÄÄÖÖÒªÁ죬¶¼¿ÉÒÔÓÐÓõØÌá¸ßLaravelÓ¦ÓóÌÐòµÄ¿ª·¢Ð§ÂʺͿÉά»¤ÐÔ¡£
ÒÔÉϾÍÊÇ̽ÌÖÔõÑùÐÞ¸ÄLaravelÏîÄ¿ÖÐViewÎļþµÄ·¾¶µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡