PHP实战:利用laravel搭建一个迷你博客实战教程
|
《PHP实战:利用laravel搭建一个迷你博客实战教程》要点: 本文主要给大家介绍的是关于利用laravel搭建一个迷你博客的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:PHP应用 一、设计与思路 在开始写第一行代码之前,一定要尽量从头到尾将我们要做的产品设计好,避免写完又改,多写不必要的代码.PHP应用
二、创建路由 完成这个博客大概需要以下几条路由:PHP应用 | 路由 | 功能 | | -------- | ---------------- | | 文章列表页面路由 | 返回文章列表页面 | | 新增文章页面路由 | 返回新增文章页面 | | 文章保存功能路由 | 将文章保存到数据库 | | 查看文章页面路由 | 返回文章详情页面 | | 编辑文章页面路由 | 返回编辑文章页面 | | 编辑文章功能路由 | 将文章取出更新后重新保存到数据库 | | 删除文章功能路由 | 将文章从数据库删除 |PHP应用 可以看到几乎全部是对文章的数据操作路由,针对这种情况,Laravel 提供了非常方便的办法:RESTful 资源控制器和路由.PHP应用 打开routes.php加入如下代码:PHP应用
Route::resource('articles','ArticlesController');
只需要上面这样一行代码,就相当于创建了如下7条路由,且都是命名路由,我们可以使用类似route('articles.show') 这样的用法.PHP应用
Route::get('/articles','ArticlesController@index')->name('articles.index');
Route::get('/articles/{id}','ArticlesController@show')->name('articles.show');
Route::get('/articles/create','ArticlesController@create')->name('articles.create');
Route::post('/articles','ArticlesController@store')->name('articles.store');
Route::get('/articles/{id}/edit','ArticlesController@edit')->name('articles.edit');
Route::patch('/articles/{id}','ArticlesController@update')->name('articles.update');
Route::delete('/articles/{id}','ArticlesController@destroy')->name('articles.destroy');
三、创建控制器 利用 artisan 创建一个文章控制器:PHP应用 php artisan make:controller ArticlesController 四、创建基础视图 resources/views/layouts/art.blade.phpPHP应用 见模板index.html 五、新建文章表单
@extends('layouts.art')
@section('content')
<form class="form-horizontal" method="post" action="{{route('blog.store')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">标题</label>
<div class="col-sm-8">
<input type="title" class="form-control" id="title" name="title">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">内容</label>
<div class="col-sm-8">
<textarea class="form-control" rows="5" id="content" name="content"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">创建</button>
</div>
</div>
</form>
@endsection
六、文章存储 此时如果你填写新建文章表单点击提交也会跳到一个空白页面,同样的道理,因为我们后续的控制器代码还没写.PHP应用 要实现文章存储,首先要配置数据库,创建数据表,创建模型,然后再完成存储逻辑代码.PHP应用 1、配置数据库 修改.env文件PHP应用 2、创建数据表 利用 artisan 命令生成迁移:PHP应用 php artisan make:migration create_articles_talbe --create=articles 修改迁移文件PHP应用
public function up()
{
Schema::create('articles',function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longText('content');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('articles');
}
我们创建了一张 articles 表,包含递增的 id 字段,字符串title字段,长文本content字段,和时间戳.PHP应用 执行数据库迁移:PHP应用 php artisan migrate 登录mysql,查看数据表.PHP应用 3、创建模型 利用 artisan 命令创建模型:PHP应用 php artisan make:model Article 打开模型文件,输入以下代码:PHP应用 app/Article.phpPHP应用
namespace App;
use IlluminateDatabaseEloquentModel;
class Article extends Model
{
//对应的表
protected $table = 'articles';
//通过model可以写入的字段
protected $fillable = [
'title','content',];
}
4、存储逻辑代码 打开 ArticlesController.php 控制器,找到 store() 方法.PHP应用 app/Http/Controllers/ArticlesController.phpPHP应用
public function store(Request $request)
{
//数据验证 错误处理
$this->validate($request,[
'title'=>'required|max:50','content'=>'required|max:500',]);
// 1 orm方式写入
$article = Article::create([
'title'=>$request->title,'content'=>$request->content,]);
//2 或者
/* $article = new Article();
$article->title =$request->title;
$article->content = $request->content;
$article->save();*/
//3 db方式写入
//insert()方法返回值为true 和 false
//$res = DB::table('articles')->insert(['title'=>$request->title,'content'=>$request->content]);
return redirect()->route('blog.index');
}
验证错误显示PHP应用
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
七、文章列表展示 完成了添加文章功能后,就可以实现我们的文章列表展示页了.PHP应用 打开 ArticlesController.php 找到 app/Http/Controllers/ArticlesController.phpPHP应用
use AppArticle;
public function index()
{
$articles = Article::orderBy('created_at','asc')->get();
return view('articles.index',['articles'=>$articles]);
}
视图index.blade.phpPHP应用
@extends('layouts.art')
@section('content')
<a class="btn btn-primary" href="{{route('blog.create')}}" rel="external nofollow" >添加文章</a>
@foreach($articles as $article)
<div class="panel panel-default">
<div class="panel-body">
{{$article->title}}
<a href="{{route('blog.show',$article->id)}}" rel="external nofollow" class="btn btn-info">阅读</a>
<a href="{{route('blog.edit',$article->id)}}" rel="external nofollow" class="btn btn-info">修改</a>
<form action="{{ route('blog.destroy',$article->id) }}" method="post" style="display: inline-block;">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger">删除</button>
</form>
</div>
</div>
@endforeach
{!! $articles->render() !!}
@endsection
八、编辑文章表单 编辑文章表单其实和之前创建的新建文章表单很类似,只是需要额外将现有的数据读取出来填在表单上.PHP应用 首先我们在文章列表页的每个文章上添加一个编辑按钮:PHP应用 视图:PHP应用
@extends('layouts.art')
@section('content')
<form class="form-horizontal" method="post" action="{{route('blog.update',$article->id)}}">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">标题</label>
<div class="col-sm-10">
<input type="title" class="form-control" id="title" name="title" value="{{ $article->title }}">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">内容</label>
<div class="col-sm-10">
<textarea class="form-control" rows="5" id="content" name="content"> {{ $article->content }}</textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">修改</button>
</div>
</div>
</form>
@endsection
(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
