laravel如何引用外部文件

laravel如何引用外部文件

(1)、首先在appHttproutes.php中定義路由;

Route::get('view','ViewController@view'); Route::get('article','ViewController@article'); Route::get('layout','ViewController@layout');

(2)、然后在HttpControllersViewController.php中寫入方法;

????public?function?view(){ ????????return?view('index'); ????} ????public?function?article(){ ????????return?view('article'); ????} ????public?function?layout(){ ????????return?view('layout'); ????}

(3)、然后在新建不同的視圖文件,路徑為:resourcesviews

index.blade.php article.blade.php layout.blade.php

重點:?

1、使用include的方式:?

一、在views下建立common目錄文件,用于存放公共文件;?

二、將公共內容放入common下,如在common建立了一個header.blade.php;?

三、在視圖中引入公共文件:

@include('common.header') //這樣的書寫方式來引入:目錄名稱.公共文件名

另外,如果在header公共區(qū)域中有不同的數(shù)據(jù),那么可以使用以下方式來傳遞數(shù)據(jù):

//視圖中的代碼 @include('common.header',['page'?=>?'詳細頁面']) //header.blade.php公共文件中的代碼 {{$page}}--公共部分

那么,以上會輸出:詳細頁面–公共部分?

即傳遞成功

2、使用子視圖的方式來引入,并且擁有相互傳遞數(shù)據(jù)的功能:?

一、在views下建立layouts目錄,其下放主視圖。views下的則為子視圖。?

二、在layouts下建立home.blade.php主視圖文件??梢怨┳右晥D調用。?

三、在views目錄下的layout.blade.php中引入主視圖文件:采用繼承的方式:

home主視圖里:

??????<div> ????????<!-- @yield(&#39;content&#39;) --> ????????yield是一個標識,標識是不一樣的變量數(shù)據(jù) ????????@section('content') ????????????<b>我是主模板里的內容</b> ????????@show ????????//在主視圖想獲取子視圖變量數(shù)據(jù)的情況下,必須使用show關鍵字而不是endsection<p>子視圖里:</p> <p>//繼承使用主視圖</p> <p>@extends('layouts.home')</p> <p>//section可以獲取主模板的內容</p> <p>@section('content')</p> <p><!--@parent-->? //parent意為:子模板可以獲取主模板里的內容</p> <p>我是layout的替換內容123</p> <p>@endsection</p> <p>更多laravel相關技術文章,請訪問<a href="http://www.php.cn/phpkj/laravel/" target="_self">Laravel教程</a>欄目進行學習!</p> </div>

? 版權聲明
THE END
喜歡就支持一下吧
點贊7 分享