php laravel 搭建博客,写PHP的老王

Laravel5.6 博客搭建系列一

24 Apr 2018

Category:

Laravel框架目前已经发展到5.6版本了,但是目前官方的入门教程还是基于5.1的博客教程。为了更多的人能快速上手新版本,本教程使用Laravel5.6

一部一部跟大家分享如何搭建一个博客系统。下面来看一下如何用十分钟使用Laravel5.6搭建简单博客

安装环境

Laravel 框架对PHP版本和扩展有一定要求

PHP >= 7.1.3

PHP OpenSSL 扩展

PHP PDO 扩展

PHP Mbstring 扩展

PHP Tokenizer 扩展

PHP XML 扩展

PHP Ctype 扩展

PHP JSON 扩展

下载安装PHP7,composer,mysql

执行 composer global require "laravel/installer", 安装laravel之后配置环境变量,执行laravel new blog

配置

编辑.env 修改数据库用户名密码,数据库名称,.env本身是隐藏文件,注意开启显示隐藏文件

创建博客文章模型数据迁移文件

执行php artisan make:model --migration Post,会在database/migrations目录下建立”日期create_posts_table.php”文件,编辑该文件

Schema::create('posts', function (Blueprint $table) {

$table->increments('id');

$table->string('slug')->unique();

$table->string('title');

$table->text('content');

$table->timestamps();

$table->timestamp('published_at')->nullable()->index();

});

这其实是在设置文字内容表的数据字段

id 自增文章id

slug seo url 唯一

title 标题

content 内容

timestamps()会自动创建一个默认是create_at的字段,用于记录数据的创建时间

published_at 发布时间 不能为空 并且建立索引

创建博客文章模型

命令同时会在app目录下建立一个Post.php文件,编辑文件

protected $dates = ['published_at'];

public function setTitleAttribute($value)

{

$this->attributes['title'] = $value;

if (! $this->exists) {

$this->attributes['slug'] = str_slug($value);

}

}

执行数据迁移,在数据库创建数据表

执行php artisan migrate,该命令会在数据库中根据配置创建数据表

填充数据

Laravel提供数据填充,添加factory 文件 database/factories/PostFactory.php,内容如下:

use Faker\Generator as Faker;

$factory->define(App\Post::class, function (Faker $faker) {

return [

'title' => $faker->sentence(mt_rand(3, 10)),

'content' => join("\n\n", $faker->paragraphs(mt_rand(3, 6))),

'published_at' => $faker->dateTimeBetween('-1 month', '+3 days'),

];

});

执行php artisan make:seed PostsTableSeeder 创建生博客文章数据成器,命令会在database/seeds下建立PostsTableSeeder.php文件。然后执行composer dump-autoload 导入(否则class not found)

function run(){

factory(App\Post::class, 50)->create();

}

编辑database/seeds/DatabaseSeeder.php中编辑

public function run()

{

$this->call(PostsTableSeeder::class);

}

执行 php artisan db:seed ,命令会根据数据生成器生成50条post数据

添加博客配置

在config目录添加blog.php文件,文件内容如下:

return [

'title' => 'My Blog',

'posts_per_page' => 5

];

添加路由

修改routes/web.php文件

Route::get('/', function () {

return redirect('/blog');

});

Route::get('blog', 'BlogController@index');

Route::get('blog/{slug}', 'BlogController@showPost');

创建控制器

执行php artisan make:controller BlogController,在app/Http/Controllers下建立BlogController.php,修改内容如下

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Post;

use Illuminate\Support\Carbon;

class BlogController extends Controller

{

public function index()

{

$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')->withPost($post);

}

}

创建视图模板

在resources/view下建立blog文件夹,分别添加index.blade.php,post.blade.php文件分别用户博客列表和详情的展示

index.blade.php

{{ config('blog.title') }}

{{ config('blog.title') }}

Page {{ $posts->currentPage() }} of {{ $posts->lastPage() }}

@foreach ($posts as $post)

{{ $post->title }}

({{ $post->published_at }})

{{ str_limit($post->content) }}

@endforeach


{!! $posts->render() !!}

post.blade.php

{{ $post->title }}

{{ $post->title }}

{{ $post->published_at }}

{!! nl2br(e($post->content)) !!}


« Back

效果

执行php artisan serve 启动服务,在浏览器中输入http://127.0.0.1:8080 浏览器会跳转到http://127.0.0.1:8080/blog 并展示下面内容

blog1.png

如果报错:

1071 Specified key was too long; max key length is 767 bytes,修改App\Providers\AppServiceProvider.php,引入use Illuminate\Support\Facades\Schema; ,在boot中添加:Schema::defaultStringLength(191);

本教程代码 下载

评论

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值