Skip to content

Commit

Permalink
教程四完成忘了提交了。。。
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlui committed Jun 3, 2016
1 parent 08a24a9 commit 7302dd1
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 7 deletions.
10 changes: 10 additions & 0 deletions app/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
//
}
65 changes: 61 additions & 4 deletions app/Http/Controllers/Admin/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,65 @@

class ArticleController extends Controller
{
public function index()
{
return view('admin/article/index')->withArticles(Article::all());
}
public function index()
{
return view('admin/article/index')->withArticles(Article::all());
}

public function create()
{
return view('admin/article/create');
}

public function edit($id)
{
return view('admin/article/edit')->withArticle(Article::find($id));
}

public function store(Request $request) // Laravel 的依赖注入系统会自动初始化我们需要的 Request 类
{
// 数据验证
$this->validate($request, [
'title' => 'required|unique:articles|max:255', // 必填、在 articles 表中唯一、最大长度 255
'body' => 'required', // 必填
]);

// 通过 Article Model 插入一条数据进 articles 表
$article = new Article; // 初始化 Article 对象
$article->title = $request->get('title'); // 将 POST 提交过了的 title 字段的值赋给 article 的 title 属性
$article->body = $request->get('body'); // 同上
$article->user_id = $request->user()->id; // 获取当前 Auth 系统中注册的用户,并将其 id 赋给 article 的 user_id 属性

// 将数据保存到数据库,通过判断保存结果,控制页面进行不同跳转
if ($article->save()) {
return redirect('admin/article'); // 保存成功,跳转到 文章管理 页
} else {
// 保存失败,跳回来路页面,保留用户的输入,并给出提示
return redirect()->back()->withInput()->withErrors('保存失败!');
}
}

public function update(Request $request, $id)
{
$this->validate($request, [
'title' => 'required|unique:articles,title,'.$id.'|max:255',
'body' => 'required',
]);

$article = Article::find($id);
$article->title = $request->get('title');
$article->body = $request->get('body');

if ($article->save()) {
return redirect('admin/article');
} else {
return redirect()->back()->withInput()->withErrors('更新失败!');
}
}

public function destroy($id)
{
Article::find($id)->delete();
return redirect()->back()->withInput()->withErrors('删除成功!');
}
}
2 changes: 1 addition & 1 deletion app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@

Route::group(['middleware' => 'auth', 'namespace' => 'Admin', 'prefix' => 'admin'], function() {
Route::get('/', 'HomeController@index');
Route::get('article', 'ArticleController@index');
Route::resource('article', 'ArticleController');
});
32 changes: 32 additions & 0 deletions resources/views/admin/article/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">新增一篇文章</div>
<div class="panel-body">

@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>新增失败</strong> 输入不符合要求<br><br>
{!! implode('<br>', $errors->all()) !!}
</div>
@endif

<form action="{{ url('admin/article') }}" method="POST">
{!! csrf_field() !!}
<input type="text" name="title" class="form-control" required="required" placeholder="请输入标题">
<br>
<textarea name="body" rows="10" class="form-control" required="required" placeholder="请输入内容"></textarea>
<br>
<button class="btn btn-lg btn-info">新增文章</button>
</form>

</div>
</div>
</div>
</div>
</div>
@endsection
33 changes: 33 additions & 0 deletions resources/views/admin/article/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">编辑文章</div>
<div class="panel-body">

@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>编辑失败</strong> 输入不符合要求<br><br>
{!! implode('<br>', $errors->all()) !!}
</div>
@endif

<form action="{{ url('admin/article/'.$article->id) }}" method="POST">
{{ method_field('PATCH') }}
{{ csrf_field() }}
<input type="text" name="title" class="form-control" required="required" placeholder="请输入标题" value="{{ $article->title }}">
<br>
<textarea name="body" rows="10" class="form-control" required="required" placeholder="请输入内容">{{ $article->body }}</textarea>
<br>
<button class="btn btn-lg btn-info">提交修改</button>
</form>

</div>
</div>
</div>
</div>
</div>
@endsection
10 changes: 8 additions & 2 deletions resources/views/admin/article/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<div class="panel panel-default">
<div class="panel-heading">文章管理</div>
<div class="panel-body">
@if (count($errors) > 0)
<div class="alert alert-danger">
{!! implode('<br>', $errors->all()) !!}
</div>
@endif

<a href="{{ url('admin/article/create') }}" class="btn btn-lg btn-primary">新增</a>

@foreach ($articles as $article)
Expand All @@ -21,8 +27,8 @@
</div>
<a href="{{ url('admin/article/'.$article->id.'/edit') }}" class="btn btn-success">编辑</a>
<form action="{{ url('admin/article/'.$article->id) }}" method="POST" style="display: inline;">
<input name="_method" type="hidden" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-danger">删除</button>
</form>
@endforeach
Expand Down

0 comments on commit 7302dd1

Please sign in to comment.