×ðÁú¿­Ê±

̽ÌÖÔõÑùÐÞ¸Ä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ÎļþµÄ·¾¶µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿