Home  >  Article  >  PHP Framework  >  How to implement page inclusion in laravel

How to implement page inclusion in laravel

PHPz
PHPzOriginal
2023-04-23 09:16:19626browse

Laravel is an open source PHP web application framework. It is a simple and elegant MVC-based web application framework. It greatly simplifies web development and allows developers to focus more on the business logic level. Don’t pay too much attention to other underlying technologies. In this article, we will introduce how to use Laravel to implement page inclusion, that is, to divide a page into multiple small blocks and share these small blocks in multiple pages at the same time, which greatly improves the reusability and usability of the code. Maintainability.

Why do we need page inclusion?

With the continuous development of Web applications, Web pages are becoming more and more complex, and each module in the page is also becoming more and more complex. Therefore, sometimes we need to divide the page into multiple small blocks and share these small blocks in multiple pages at the same time, thereby improving the reusability and maintainability of the code. This is the page containing the problems faced and their solutions.

Steps to implement page inclusion using Laravel

The Laravel framework provides a very flexible view system, so page inclusion can be implemented very conveniently. The following are the steps to implement page inclusion:

  1. Create a public view file

First, create a public view file in the Laravel project. This public view file contains multiple small blocks that will be shared among multiple pages. This public view file can be saved in resources/views/layouts.

  1. Include in the view files that need to use these small blocks

In the view files that need to use the small blocks included in the public view file, use the following code:

@extends('layouts.master')
@section('content')
    <!-- 这里是当前页面的内容 -->
@endsection

Among them, the @extends directive indicates that this view file inherits the layouts/master view file, and the @section directive is used to define a content area, which will contain the content of the current page.

  1. Define small blocks in the public view file

In the public view file, use the following code to define the small blocks that need to be included:

<!DOCTYPE html>
<html>
<head>
    <title>@yield('title')</title>
</head>
<body>
    <div class="container">
        <header>
            @include('layouts.partials.header')
        </header>
        @yield('content')
        <footer>
            @include('layouts.partials.footer')
        </footer>
    </div>
</body>
</html>

where :

  • @yield('title') means that a small block named title is defined, which is used to replace the tag in the public view file. </li> <li>@yield('content') means that a small block named content is defined, which is used to replace the page content part in the public view file. </li> <li>@include('layouts.partials.header') means that a partial view file named header is included. </li> <li>@include('layouts.partials.footer') means that a partial view file named footer is included. </li> </ul> <p>This completes the steps of defining small blocks in the public view file. </p> <ol start="4"><li> Define the content of the small block in the partial view file </li></ol> <p>Finally, define it in the partial view file (such as header.blade.php and footer.blade.php) Small chunks of content will do. For example, in header.blade.php, you can define the content of a small block as follows: </p> <pre class="brush:php;toolbar:false"><nav>     <ul>         <li><a href="{{ url(&#39;/&#39;) }}">Home</a></li>         <li><a href="{{ url(&#39;/about&#39;) }}">About</a></li>         <li><a href="{{ url(&#39;/contact&#39;) }}">Contact</a></li>     </ul> </nav></pre> <p>This small block defines a navigation bar and uses Laravel's URL helper function to generate links. </p> <p>Through these steps, the process of using Laravel to implement page inclusion is completed. </p> <p>Summary</p> <p>Page inclusion is a very important web development technology. In the Laravel framework, we can implement page inclusion very conveniently, thus greatly improving the reusability and usability of the code. Maintainability. This article introduces how to use Laravel to implement page inclusion. I hope it will be helpful to everyone. If you have any questions or ideas about Laravel or web development, please leave a message in the comment area and we will discuss it together. </p><p>The above is the detailed content of How to implement page inclusion in laravel. For more information, please follow other related articles on the PHP Chinese website!</p></div><div class="nphpQianMsg"><a href="javascript:void(0);">php</a> <a href="javascript:void(0);">laravel</a> <a href="javascript:void(0);">mvc</a> <a href="javascript:void(0);">include</a> <a href="javascript:void(0);">继承</a><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="How to close mysql in laravel" href="http://m.php.cn/faq/524669.html">How to close mysql in laravel</a></span><span>Next article:<a class="dBlack" title="How to close mysql in laravel" href="http://m.php.cn/faq/524673.html">How to close mysql in laravel</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="http://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="http://m.php.cn/faq/407215.html" title="Laravel environment setup: steps on how to deploy laravel to Alibaba Cloud or Tencent Cloud" class="aBlack">Laravel environment setup: steps on how to deploy laravel to Alibaba Cloud or Tencent Cloud</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/407217.html" title="Lamp environment configuration: LAMP environment configuration under Alibaba Cloud server under CentOS 6.3" class="aBlack">Lamp environment configuration: LAMP environment configuration under Alibaba Cloud server under CentOS 6.3</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/407219.html" title="Quick installation steps for Apache+PHP+MySQL+phpMyAdmin under Ubuntu" class="aBlack">Quick installation steps for Apache+PHP+MySQL+phpMyAdmin under Ubuntu</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/407220.html" title="Quick installation steps for Nginx + PHP + MySQL under Ubuntu" class="aBlack">Quick installation steps for Nginx + PHP + MySQL under Ubuntu</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/407260.html" title="The basic working principle of middleware in the pipeline design pattern in the Laravel framework" class="aBlack">The basic working principle of middleware in the pipeline design pattern in the Laravel framework</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><div class="nphpFoot"><div class="nphpFootBg"><ul class="nphpFootMenu"><li><a href="http://m.php.cn/"><b class="icon1"></b><p>Home</p></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><p>Course</p></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li><li><a href="http://m.php.cn/login"><b class="icon5"></b><p>My</p></a></li><div class="clear"></div></ul></div></div><div class="nphpYouBox" style="display: none;"><div class="nphpYouBg"><div class="nphpYouTitle"><span onclick="$('.nphpYouBox').hide()"></span><a href="http://m.php.cn/"></a><div class="clear"></div></div><ul class="nphpYouList"><li><a href="http://m.php.cn/"><b class="icon1"></b><span>Home</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><span>Course</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/article.html"><b class="icon3"></b><span>Article</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><span>Q&A</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/dic.html"><b class="icon6"></b><span>Dictionary</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course/type/99.html"><b class="icon7"></b><span>Manual</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/xiazai/"><b class="icon8"></b><span>Download</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/faq/zt" title="Topic"><b class="icon12"></b><span>Topic</span><div class="clear"></div></a></li><div class="clear"></div></ul></div></div><div class="nphpDing" style="display: none;"><div class="nphpDinglogo"><a href="http://m.php.cn/"></a></div><div class="nphpNavIn1"><div class="swiper-container nphpNavSwiper1"><div class="swiper-wrapper"><div class="swiper-slide"><a href="http://m.php.cn/" >Home</a></div><div class="swiper-slide"><a href="http://m.php.cn/article.html" class="hover">Article</a></div><div class="swiper-slide"><a href="http://m.php.cn/wenda.html" >Q&A</a></div><div class="swiper-slide"><a href="http://m.php.cn/course.html" >Course</a></div><div class="swiper-slide"><a href="http://m.php.cn/faq/zt" >Topic</a></div><div class="swiper-slide"><a href="http://m.php.cn/xiazai" >Download</a></div><div class="swiper-slide"><a href="http://m.php.cn/game" >Game</a></div><div class="swiper-slide"><a href="http://m.php.cn/dic.html" >Dictionary</a></div><div class="clear"></div></div></div><div class="langadivs" ><a href="javascript:;" class="bg4 bglanguage"></a><div class="langadiv" ><a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a><a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>English</span><span>(EN)</span></a><a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a><a onclick="javascript:setlang('ja');" class="language course-right-orders chooselan " href="javascript:;"><span>日本語</span><span>(JA)</span></a><a onclick="javascript:setlang('ko');" class="language course-right-orders chooselan " href="javascript:;"><span>한국어</span><span>(KO)</span></a><a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a><a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a><a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a></div></div><script> var swiper = new Swiper('.nphpNavSwiper1', { slidesPerView : 'auto', observer: true,//修改swiper自己或子元素时,自动初始化swiper observeParents: true,//修改swiper的父元素时,自动初始化swiper }); </script></div></div><!--顶部导航 end--><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>