资讯专栏INFORMATION COLUMN

Laravel 5.4 入门系列 5. 博客通用布局

xuweijian / 2987人阅读

摘要:接下来执行迁移即可通用布局通用布局首先是博客首页,定义路由控制器视图博客首页访问下网站根目录,显示博客首页,框架基本搭建完成了。首先是通用布局通用布局里面除了使用之外,还使用了,用于加载其他模板。

5. 博客的通用布局 初始化 创建控制器、模型、迁移

博客的核心是文章,可以先来实现和文章有关的功能,根据前几节的介绍可知,我们至少需要创建这几类:

PostsController:控制器

Post:模型

create_posts_table:迁移任务

虽然可以分别创建,但是也可以批量进行创建,只需要在创建 Model 的时候指定即可:

$ php artisan make:model Post -mc

不过创建的控制器为单数形式 PostController,我们需要手动将文件名和类名改成复数的。

创建表格

接下来是生成 posts 表,通过迁移来完成:

// /database/migrations/2017_04_12_124622_create_posts_table.php

public function up()
{
    Schema::create("posts", function (Blueprint $table) {
        $table->increments("id");
        $table->string("title");
        $table->body("text");
        $table->timestamps();
    });
}

一开始,为了便于操作,我们只包含了标题和内容两个基本字段。接下来执行迁移即可:

$ php artisan migrate
通用布局 通用布局

首先是博客首页,定义路由:

/routes/web.php

Route::get("/posts","PostsController@index");

控制器:

/app/Http/Controllers/PostsController.php

public function index()
{
    return view("posts.index");
}

视图:

/resources/views/posts/index.blade.php




    
    Document


    博客首页

访问下网站根目录,显示「博客首页」,框架基本搭建完成了。现在,可以回顾下,我们之前所创建的视图,每个视图都包括了一些共同的东西,比如头部、尾部等,造成大量的重复工作。Laravel 提供了优雅的解决方案,首先,我们创建一个页面用于存放这些共同的东西:

/resources/views/layouts/master.blade.php



    
    博客首页


    
@yield("content")

@yield 指令相当于一个占位符,这就意味着,其他页面只需要继承该页面,就可以共享该页面的内容,同时也可以定义具体的 content 以满足不同的显示:

/resources/views/posts/index.blade.php

@extends("layout")

@section("content")
    
    博客首页
    
@stop

子模板要声明继承于哪个模板,使用 @extends 指令。同时,使用@section@stop 来定义各自的自己的 content 的内容。

@yield 也可以定义默认值,如果子模板不继承的话就会显示该默认值

@yield("title","默认首页")

如果子模板既要继承父模板的内容,也要加载自己的内容,通用视图里需要使用以下语法:

@section("sidebar")
    这是侧边栏
@show

继承时使用 @parent 代表加载父模板的内容:

@section("sidebar")
    @parent
    自定义内容
@endsection
使用 Bootstrap Blog 模板

接下来,我们使用 Bootstrap Blog 模板来创建博客的前台,该模板的效果如图所示:

我们根据上图划分来对源代码进行分割。首先是通用布局:

/resources/views/layouts/master.blade.php



  
    
    

    Blog Template for Bootstrap

    

    

  

  
  
    @include("layouts.nav")
    @include("layouts.header");
  
    
@yield("content") @include("layouts.siderbar")
@include("layouts.footer")

通用布局里面除了使用 @yield 之外,还使用了 @include,用于加载其他模板。

导航:

/resources/views/layouts/nav.blade.php

头部:

/resources/views/layouts/header.blade.php

The Bootstrap Blog

An example blog template built with Bootstrap.

侧边栏:

/resources/views/layouts/siderbar.blade.php

底部:

/resources/views/layouts/footer.blade.php

最后是博客的样式:

/public/css/blog.css

/*
 * Globals
 */

@media (min-width: 48em) {
  html {
    font-size: 18px;
  }
}

body {
  font-family: Georgia, "Times New Roman", Times, serif;
  color: #555;
}

h1, .h1,
h2, .h2,
h3, .h3,
h4, .h4,
h5, .h5,
h6, .h6 {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-weight: normal;
  color: #333;
}


/*
 * Override Bootstrap"s default container.
 */

.container {
  max-width: 60rem;
}


/*
 * Masthead for nav
 */

.blog-masthead {
  margin-bottom: 3rem;
  background-color: #428bca;
  -webkit-box-shadow: inset 0 -.1rem .25rem rgba(0,0,0,.1);
          box-shadow: inset 0 -.1rem .25rem rgba(0,0,0,.1);
}

/* Nav links */
.nav-link {
  position: relative;
  padding: 1rem;
  font-weight: 500;
  color: #cffffdeb;
}
.nav-link:hover,
.nav-link:focus {
  color: #fff;
  background-color: transparent;
}

/* Active state gets a caret at the bottom */
.nav-link.active {
  color: #fff;
}
.nav-link.active:after {
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 0;
  margin-left: -.3rem;
  vertical-align: middle;
  content: "";
  border-right: .3rem solid transparent;
  border-bottom: .3rem solid;
  border-left: .3rem solid transparent;
}


/*
 * Blog name and description
 */

.blog-header {
  padding-bottom: 1.25rem;
  margin-bottom: 2rem;
  border-bottom: .05rem solid #eee;
}
.blog-title {
  margin-bottom: 0;
  font-size: 2rem;
  font-weight: normal;
}
.blog-description {
  font-size: 1.1rem;
  color: #999;
}

@media (min-width: 40em) {
  .blog-title {
    font-size: 3.5rem;
  }
}


/*
 * Main column and sidebar layout
 */

/* Sidebar modules for boxing content */
.sidebar-module {
  padding: 1rem;
  /*margin: 0 -1rem 1rem;*/
}
.sidebar-module-inset {
  padding: 1rem;
  background-color: #f5f5f5;
  border-radius: .25rem;
}
.sidebar-module-inset p:last-child,
.sidebar-module-inset ul:last-child,
.sidebar-module-inset ol:last-child {
  margin-bottom: 0;
}


/* Pagination */
.blog-pagination {
  margin-bottom: 4rem;
}
.blog-pagination > .btn {
  border-radius: 2rem;
}


/*
 * Blog posts
 */

.blog-post {
  margin-bottom: 4rem;
}
.blog-post-title {
  margin-bottom: .25rem;
  font-size: 2.5rem;
}
.blog-post-meta {
  margin-bottom: 1.25rem;
  color: #999;
}


/*
 * Footer
 */

.blog-footer {
  padding: 2.5rem 0;
  color: #999;
  text-align: center;
  background-color: #f9f9f9;
  border-top: .05rem solid #e5e5e5;
}
.blog-footer p:last-child {
  margin-bottom: 0;
}

现在,访问 /posts,就可以看到整个页面效果了 :)


Blog Template for Bootstrap

Laravel 的 Blade 模板引擎 | Laravel 5.4 中文文档

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/22705.html

相关文章

  • Laravel 5.4 入门系列 10.文章归档

    摘要:将上述的一系列查询进行封装模型到了这一步,我们基本上实现了文章归档的功能。但是有一个问题,文章归档实际上包括在通用视图中,这就意味着,网站的所有请求都需要返回,否则就会报错。数据库之数据库请求构建器中文文档的视图功能中文文档 首先,要实现的是按照日期来统计文章,原始的 SQL 如下: select year(created_at) year, monthname(c...

    Nekron 评论0 收藏0
  • 最适合入门Laravel 初级教程 (二)

    摘要:最适合入门的初级教程二看这篇文章的时候你需要安装好配置好本地环境环境搞定后咱来说的下载这里先解决一些童鞋可能有的疑惑的版本更新的那么快从到现在的了我应该下载那个学习呢新出的版本的文档资料丰富么作为一个过来人可以大胆的说学习最新版本没问题除了 最适合入门的 Laravel 初级教程 (二) 看这篇文章的时候;你需要安装好 composer ;配置好本地环境; 环境搞定后; 咱来说lara...

    rubyshen 评论0 收藏0
  • Laravel 5.4 入门系列 6. 文章的创建

    摘要:基本功能创建文章的第一步是用户发请求,然后返回创建文章的页面。实际上,会报错添加保护虽然我们完成了基本功能,但是提交请求的时候还是会报错,其实这是防止攻击。假如违反了规则,错误信息会自动被保存在闪存的中,即只对下一次请求生效。 基本功能 创建文章的第一步是用户发请求,然后返回创建文章的页面。 路由:处理用户「创建文章」的请求 /routes/web.php Route::get(/po...

    levius 评论0 收藏0
  • Laravel 5.4 入门系列 1. 安装

    摘要:的安装与使用是什么是的一个依赖管理工具。它以项目为单位进行管理,你只需要声明项目所依赖的代码库,会自动帮你安装这些代码库。 Composer 的安装与使用 Composer 是什么 Composer 是 PHP 的一个依赖管理工具。它以项目为单位进行管理,你只需要声明项目所依赖的代码库,Composer 会自动帮你安装这些代码库。 安装 Composer Mac 下的安装只需要在命令行...

    hqman 评论0 收藏0
  • Laravel 5.4 入门系列 13. 终篇: 小白也能看懂的 Laravel 核心概念讲解

    摘要:但是服务通常由服务提供者来管理的。小结通过上述的例子,基本上可以理解服务容器和服务提供者的使用。懂得了服务容器和服务提供者,理解门面也就不难了。 自动依赖注入 什么是依赖注入,用大白话将通过类型提示的方式向函数传递参数。 实例 1 首先,定义一个类: /routes/web.php class Bar {} 假如我们在其他地方要使用到 Bar 提供的功能(服务),怎么办,直接传入参数即...

    BenCHou 评论0 收藏0

发表评论

0条评论

xuweijian

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<