今天起开始搭建博客,把之前学的东西运用下。


1 创建 配置项目

1.1 创建项目

composer create-project laravel/laravel blog 5.1.1 

1.2 配置数据库

在.env文件中配置你的数据库

DB_HOST=127.0.0.1
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=

1.3 创建一个配置文件

在config文件夹中创建一个blog.php(配置文件)

<?php
return [
'title' => "Larger K's Blog",
'posts_pre_page' => 5,
];

2 准备数据

2.1 创建Post模型和迁移文件

php artisan make:model Post -m 

2.2 编写迁移文件/设置表结构

class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('slug')->unique(); // 用于 SEO
$table->string('title'); // 标题
$table->text('content'); // 内容
$table->timestamp('published_at'); // 发布时间
$table->timestamps();
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('posts');
}

然后migrate就行了。

2.3 设置Post模型

class Post extends Model
{
// 指定白名单
protected $fillable = ['slug', 'title', 'content', 'published_at']; // 添加published_at到时间
protected $dates = ['published_at']; /**
* @param $value
* 在设置Title字段时 设置slug属性。
*/
public function setTitleAttribute($value)
{
$this->attributes['title'] = $value; if (! $this->exists){
$this->attributes['slug'] = str_slug($value);
}
}
}

2.4 编写ModelFactory

/**
* Post
*/
$factory->define(App\Post::class, function (Faker\Generator $faker) {
return [
'title' => $faker->sentence(mt_rand(4, 8)),
'content' => join("\n\n", $faker->paragraphs(mt_rand(3, 6))),
'published_at' => $faker->dateTimeBetween('-1 month'),
];
});

2.5 创建/编写seeder

php artisan make:seeder PostSeeder
class PostSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// truncate方法是清除自增ID,通常我们清除整张表后ID是不会清零的,如果你加上这个方法 之前所有数据被清空 并且ID会清零。
App\Post::truncate();
factory(App\Post::class, 20)->create();
}
}
php artisan db:seed

3 编写路由和控制器

3.1 路由编写

Route::get('/', function () {
// 重定向到 /blog 路由
return redirect('/blog');
}); Route::get('/blog', 'BlogController@index');
Route::get('/blog/{slug}', 'BlogController@showPost');

3.2 创建/编写控制器

class BlogController extends Controller
{
public function index()
{
/**
* 过滤 published_at 必须小于现在的时间
* 按 published_at 降序排序
* 分页
*/
$posts = Post::where('published_at', '<=', Carbon::now())
->orderBy('published_at', 'desc')
->paginate(config('blog.posts_per_page'));
return view('blog.index', compact('posts'));
} public function showPost($slug)
{
$post = Post::whereSlug($slug)->firstOrFail();
return view('blog.post', compact('post'));
}
}

4 编写前端

4.1 index

在 resources/views 中创建 post目录 并创建index.blade.php

<!DOCTYPE html>
<html>
<head>
<title>{{ config('blog.title') }}</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head> <body>
<div class="container">
<h1>{{ config('blog.title') }}</h1>
<h5>Page {{ $posts->currentPage() }} of {{ $posts->lastPage() }}</h5>
<hr>
<ul>
@foreach($posts as $post)
<li>
<a href="/blog/{{ $post->slug }}">{{ $post->title }}</a>
<em>{{ $post->published_at }}</em>
<p>{{ str_limit($post->content) }}</p>
</li>
@endforeach
</ul>
{!! $posts->render() !!}
</div>
</body>
</html>

4.2 post

<html>
<head>
<title>{{ $post->title }}</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>{{ $post->title }}</h1>
<h5>{{ $post->published_at }}</h5>
<hr>
{!! nl2br(e($post->content)) !!}
<hr>
<button class="btn btn-primary" onclick="history.go(-1)">
« Back
</button>
</div>
</body>
</html>

Laravel5.1 搭建博客 --展示简单的首页的更多相关文章

  1. Laravel5.1 搭建博客 --编译前端文件

    上篇文章写了Gulp编译前端文件,这篇记录下在搭建博客中使用Gulp 1 引入bootstrap和js 1.1 首先先在项目本地安装Bower sudo npm install bower 1.2 创 ...

  2. Laravel5.1 搭建博客 --构建标签

    博客的每篇文章都是需要有标签的,它与文章也是多对多的关系 这篇笔记也是记录了实现标签的步骤逻辑. 在我们之前的笔记中创建了Tag的控制器和路由了 所以这篇笔记不在重复 1 创建模型与迁移文件 迁移文件 ...

  3. Laravel5.1 搭建博客 --文章的增删改查

    教程源于:Laravel学院 继文件上传后呢,咱来搞一搞文章的事情. 1 更改数据表 我们需要改改数据表的结构 因为涉及到重命名列名 所以咱需要引入一个包:Doctrine: composer req ...

  4. Laravel5.1 搭建博客 --上传文件及文件管理

    教程源自:Laravel学院 这一节 咱来说说上传文件的功能实现,我们会把上传的文件保存到项目本地,不仅上传 还有删除和预览功能. 1 配置 我们先从配置开始做起,先修改我们自己创建的 blog.ph ...

  5. Laravel5.1 搭建博客 --后台登录

    今天咱来实现后台的登录. 首先我们的后台需要三个控制器: PostController:管理文章. TagController:管理文章标签. UploadController:上传文件. 当我们访问 ...

  6. 在github上搭建博客(使用Jekyll)

    简单说,只需要三步,就可以在 Github 搭建起一个博客: 在 Github 上建一个名为 xxx.github.io 的库: 把看中了的 Jekyll 模板 clone 到本地: 把这个模板 pu ...

  7. flask tutorial => make a blog :) flask 搭建博客系统从零开始!

    please follow the tutorial from the official site :) http://flask.pocoo.org/docs/ You could download ...

  8. github+hexo搭建博客

    引言     之前用阿里云弹性web托管采用wordpress搭建的个人博客,经过我使用一段时间之后发现存在很多问题: 网站的响应速度非常慢,估计打开主页需要3-4s的时间,我经过搜索发现很多人都有这 ...

  9. django学习笔记——搭建博客网站

    1. 配置环境,创建django工程 虚拟环境下建立Django工程,即创建一个包含python脚本文件和django配置文件的目录或者文件夹,其中manage.py是django的工程管理助手.(可 ...

随机推荐

  1. FIFO、LRU、LFU的含义和原理(转)

    题目:请简要介绍FIFO.LRU.LFU的含义和原理   含义: FIFO:First In First Out,先进先出LRU:Least Recently Used,最近最少使用 LFU:Leas ...

  2. 【Hibernate步步为营】--锁机制具体解释

    上篇文章具体讨论了hql的各种查询方法.在讨论过程中写了代码演示样例.hql的查询方法类似于sql,查询的方法比較简单,有sql基础的开发者在使用hql时就会变得相当的简单. Hibernate在操作 ...

  3. linux route命令使用

    说明:route命令是打印和操作ip路由表描述:route操作基于内核ip路由表,它的主要作用是创建一个静态路由让指定一个主 机或者一个网络通过一个网络接口,如eth0.当使用"add&qu ...

  4. Phone

    User-Agent Switcher for Chrome EditThisCookie cornerstone SVN

  5. 基于Verilog语言的FIR滤波【程序和理解】

    一直想找一个简单.清晰.明了的fir滤波器的设计,终于找到了一个可以应用的,和大家分享一下,有助于FPGA新手入门. 1.说道fir滤波器,滤波系数肯定是最重要的,因为后面程序中涉及到滤波系数问题,所 ...

  6. DirectShow中写push模式的source filter流程 + 源码(内附具体凝视)

    尽管网上已有非常多关于DirectShow写source filter的资料.只是非常多刚開始学的朋友总说讲的不是非常清楚(可能当中作者省略了很多他觉得简 单的过程).读者总希望看到象第一步怎么做,第 ...

  7. LFCS 系列第八讲:管理用户和用户组、文件权限和属性以及启用账户 sudo 访问权限

    由于 Linux 是一个多用户的操作系统(允许多个用户通过不同主机或者终端访问一个独立系统),因此你需要知道如何才能有效地管理用户:如何添加.编辑.禁用和删除用户账户,并赋予他们足以完成自身任务的必要 ...

  8. Windows7 64位安装最新版本号MySQLserver

    Windows7 64位安装最新版本号MySQLserver 最近,一直在研究MySQL数据库.常常改动配置文件.导致MySQL数据库无法使用.不得不重复重装MySQL数据库.下面是在Windows7 ...

  9. 使用Nginx Lua实现redis高性能http接口

    使用Nginx Lua实现redis高性能http接口 时间 -- :: 峰云就她了 原文 http://xiaorui.cc/2015/01/27/使用nginx-lua实现redis高性能http ...

  10. MapReduce实战(六)共同粉丝

    需求: 利用mapReduce实现类似微博中查找共同粉丝的功能.如下: A:B,C,D,F,E,OB:A,C,E,KC:F,A,D,ID:A,E,F,LE:B,C,D,M,LF:A,B,C,D,E,O ...