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


1 创建 配置项目

1.1 创建项目

  1. composer create-project laravel/laravel blog 5.1.1

1.2 配置数据库

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

  1. DB_HOST=127.0.0.1
  2. DB_DATABASE=blog
  3. DB_USERNAME=root
  4. DB_PASSWORD=

1.3 创建一个配置文件

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

  1. <?php
  2. return [
  3. 'title' => "Larger K's Blog",
  4. 'posts_pre_page' => 5,
  5. ];

2 准备数据

2.1 创建Post模型和迁移文件

  1. php artisan make:model Post -m

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

  1. class CreatePostsTable extends Migration
  2. {
  3. /**
  4. * Run the migrations.
  5. *
  6. * @return void
  7. */
  8. public function up()
  9. {
  10. Schema::create('posts', function (Blueprint $table) {
  11. $table->increments('id');
  12. $table->string('slug')->unique(); // 用于 SEO
  13. $table->string('title'); // 标题
  14. $table->text('content'); // 内容
  15. $table->timestamp('published_at'); // 发布时间
  16. $table->timestamps();
  17. });
  18. }
  19.  
  20. /**
  21. * Reverse the migrations.
  22. *
  23. * @return void
  24. */
  25. public function down()
  26. {
  27. Schema::drop('posts');
  28. }
  29. } 

然后migrate就行了。

2.3 设置Post模型

  1. class Post extends Model
  2. {
  3. // 指定白名单
  4. protected $fillable = ['slug', 'title', 'content', 'published_at'];
  5.  
  6. // 添加published_at到时间
  7. protected $dates = ['published_at'];
  8.  
  9. /**
  10. * @param $value
  11. * 在设置Title字段时 设置slug属性。
  12. */
  13. public function setTitleAttribute($value)
  14. {
  15. $this->attributes['title'] = $value;
  16.  
  17. if (! $this->exists){
  18. $this->attributes['slug'] = str_slug($value);
  19. }
  20. }
  21. }

2.4 编写ModelFactory

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

2.5 创建/编写seeder

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

3 编写路由和控制器

3.1 路由编写

  1. Route::get('/', function () {
  2. // 重定向到 /blog 路由
  3. return redirect('/blog');
  4. });
  5.  
  6. Route::get('/blog', 'BlogController@index');
  7. Route::get('/blog/{slug}', 'BlogController@showPost');

3.2 创建/编写控制器

  1. class BlogController extends Controller
  2. {
  3. public function index()
  4. {
  5. /**
  6. * 过滤 published_at 必须小于现在的时间
  7. * 按 published_at 降序排序
  8. * 分页
  9. */
  10. $posts = Post::where('published_at', '<=', Carbon::now())
  11. ->orderBy('published_at', 'desc')
  12. ->paginate(config('blog.posts_per_page'));
  13. return view('blog.index', compact('posts'));
  14. }
  15.  
  16. public function showPost($slug)
  17. {
  18. $post = Post::whereSlug($slug)->firstOrFail();
  19. return view('blog.post', compact('post'));
  20. }
  21. }

4 编写前端

4.1 index

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>{{ config('blog.title') }}</title>
  5. <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
  6. </head>
  7.  
  8. <body>
  9. <div class="container">
  10. <h1>{{ config('blog.title') }}</h1>
  11. <h5>Page {{ $posts->currentPage() }} of {{ $posts->lastPage() }}</h5>
  12. <hr>
  13. <ul>
  14. @foreach($posts as $post)
  15. <li>
  16. <a href="/blog/{{ $post->slug }}">{{ $post->title }}</a>
  17. <em>{{ $post->published_at }}</em>
  18. <p>{{ str_limit($post->content) }}</p>
  19. </li>
  20. @endforeach
  21. </ul>
  22. {!! $posts->render() !!}
  23. </div>
  24. </body>
  25. </html>

4.2 post

  1. <html>
  2. <head>
  3. <title>{{ $post->title }}</title>
  4. <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
  5. </head>
  6. <body>
  7. <div class="container">
  8. <h1>{{ $post->title }}</h1>
  9. <h5>{{ $post->published_at }}</h5>
  10. <hr>
  11. {!! nl2br(e($post->content)) !!}
  12. <hr>
  13. <button class="btn btn-primary" onclick="history.go(-1)">
  14. « Back
  15. </button>
  16. </div>
  17. </body>
  18. </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. Hibernate继承类的实现

    版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5603724.html  对于继承关系类的映射.比如在论坛中文章(Ar ...

  2. 【php】在php代码嵌入HTML代码(适用于公众号开发)

    核心:HTML的双引号["]一定要转义,不废话: $link = "<a href=\"http://www.baidu.com\">最新活动链接& ...

  3. Codeigniter 使用 Mysql 存储过程

    本篇文章由:http://xinpure.com/codeigniter-using-mysql-stored-procedures/ 执行存储过程 $query = $this -> db - ...

  4. Yii Framework2.0开发教程(3)数据库mysql入门

    沿用教程(2)的代码 第一步.在本地mysql数据库中新建数据库zhyoulun 第二步.在数据库中新建表并插入若干条数据 CREATE TABLE `country` ( `code` CHAR(2 ...

  5. 使用 GROUP BY WITH ROLLUP 改善统计性能

    使用 GROUP BY 的 WITH ROLLUP 字句可以检索出更多的分组聚合信息,它不仅仅能像一般的 GROUP BY 语句那样检索出各组的聚合信息,还能检索出本组类的整体聚合信息. 下面我们的例 ...

  6. SiteWhere物联网云平台架构

    SystemArchitecture系统架构 Thisdocument describes the components that make up SiteWhere and how theyrela ...

  7. 点滴积累【C#】---C#实现上传word以流形式保存到数据库和读取数据库中的word文件。

    本文修改来源:http://www.cnblogs.com/zmgdpg/archive/2005/03/31/129758.html 效果: 数据库: 思路: 首先保存word到数据库:获取上传文件 ...

  8. Atitit.分区对索引的影响 分区索引和全局索引 attilax总结

    Atitit.分区对索引的影响 分区索引和全局索引 attilax总结 1. 分区的好处1 2. 分区键:2 3. 分区的建议:2 4. 分区索引和全局索引:2 5. 全局索引就是在全表上创建索引, ...

  9. matplotlib之极坐标系的极角网格线(thetagrids)的显示刻度

    极坐标系的极角网格线(thetagrids)的显示刻度 #!/usr/bin/env python3 #-*- coding:utf-8 -*- ########################### ...

  10. poj1703 Find them, Catch them(并查集的应用)

    Find them, Catch them   Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32225   Accepte ...