Laravel5.1 搭建博客 --展示简单的首页

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

 


 

 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>

 

转载于:https://www.cnblogs.com/sun-kang/p/7594681.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值