laravel简书(2)

用户注册

public function register() {
        //验证
       
$this->validate(request(),[
            'name'=>'required|min:3|unique:users,name',//设置user表里的字段name是唯一的
           
'email'=>'required|unique:users,email|email',
            'password'=>'required|min:5|max:10|confirmed',
        ]);
        //逻辑
       
$name = request('name');
        $email = request('email');
        $password = bcrypt(request('password'));//bcrypt:使用明文加密
       
$user = User::create(compact('name','email','password'));
        //渲染
       
return redirect('/login');
    }
}

<form class="form-signin" method="POST" action="/register">
    {{ csrf_field() }}
@include('layout.error')
<button class="btn btn-lg btn-primary btn-block" type="submit">注册</button>

用户登录

//登录行为
public function login() {
    //验证
   
$this->validate(request(),[
        'email'=>'required|email',
        'password'=>'required|min:5|max:10',
        'is_remember'=>'integer'
    ]);
    //逻辑
   
$user = request(['email','password']);
    $is_remember = boolval(request('is_remember'));
    if(Auth::attempt($user,$is_remember)) {
        return redirect('/posts');
    }
    //渲染
   
return Redirect::back()->withErrors('邮箱密码不匹配');
}

用户登出

//登出行为
public function logout() {
    Auth::logout();
    return redirect('/login');
}

使用policy实现文章权限控制:

在首页显示用户名:{{$post->user->name}}

1、在命令行中创建PostPolicy.php

F:phpianshu>php artisan make:policy PostPolicy

Policy created successfully.

并在PostPolicy.php中增加两个方法:

//修改权限
public function update(User $user,Post $post) {
    return $user->id == $post->user_id;
}
//删除权限
public function delete(User $user,Post $post) {
    return $user->id == $post->user_id;
}

2、在AppPoliciesPostPolicy.php中修改以下内容:

protected $policies = [
    //'AppModel' => 'AppPoliciesModelPolicy',
   
'AppPost'=>'AppPoliciesPostPolicy',
];

3、在PostController.php中的update和delete方法中分别增加以下内容:

$this->authorize('update',$post);

$this->authorize('delete',$post);

4、使除了自己没有权限的用户查看文章详情页时不显示编辑和删除的图标:增加can方法

@can('update',$post)
<a style="margin: auto"  href="/posts/{{$post->id}}/edit">
    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
</a>
@endcan
{{--@endif--}}
@can('update',$post)
<a style="margin: auto"  href="/posts/{{$post->id}}/delete">
    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</a>
@endcan

评论

1、配置路由

//提交评论
Route::post('/posts/{post}/comment','AppHttpControllersPostController@comment');

2、编写comment方法

//提交评论
public function comment(Post $post) {
    //验证
   
$this->validate(request(),[
        'content'=>'required|min:3',
    ]);
    //逻辑
   
$comment = new Comment();
    $comment->user_id = App::id();
    $comment->content = request('content');
    $post->comments()->save($comment);
    //渲染
   
return back();
}

3、详情页配置

<form action="/posts/{{ $post->id }}/comment" method="POST">
    {{ csrf_field() }}

4、Comment.php模型

class Comment extends Model
{
    //评论所属文章
   
public function post() {
        return $this->belongsTo('AppPost');
    }
}

实现评论列表

Show.blade.php

@foreach($post->comments as $comment)
<li class="list-group-item">
    <h5>{{$comment->created_at}} by {{$comment->user->name}}</h5>
    <div>
       {{$comment->content}}
    </div>
</li>
@endforeach

Comment.php

//评论所属用户
public function user() {
    return $this->belongsTo('AppUser');
}

public function show(Post $post) {
    $post->load('comments');

实现评论数

//文章列表页
public function index() {
    $posts =Post::orderBy('created_at','desc')->withCount('comments')->paginate(6);

<p class="blog-post-meta">赞 0  | 评论 {{$post->comments_count}}</p>

点赞

1、  路由配置

//
Route::get('/{post}/zan','AppHttpControllersPostController@zan');
//取消赞
Route::get('/{post}/unzan','AppHttpControllersPostController@unzan');

2、PostController.php

//
public function zan(Post $post) {
    $param = [
        'user_id'=>Auth::id(),
        'post_id'=>$post->id
   
];
    Zan::firstOrCreate($param);
    return back();//回退
}
//取消赞
public function unzan(Post $post) {
    $post->zan(Auth::id())->delete();
    return back();
}

3、Post.php

//和用户进行关联
public function zan($user_id) {
    //文章对应的某个ID是否有赞
   
return $this->hasOne(AppZan::class)->where('user_id',$user_id);
}
//文章的所有赞
public function zans() {
    return $this->hasMany(AppZan::class);
}

4、show.blade.php

@if($post->zan(Auth::id())->exists())
<a href="/posts/{{$post->id}}/unzan" type="button" class="btn btn-default btn-lg">取消赞</a>
@else
<a href="/posts/{{$post->id}}/zan" type="button" class="btn btn-primary btn-lg">赞</a>
@endif

列表页展示赞的数量

1、PostController.php

//文章列表页
public function index() {
    $posts =Post::orderBy('created_at','desc')->withCount(['comments','zans'])->paginate(6);

2、  index.blade.php

<p class="blog-post-meta">赞 {{$post->zans_count}} | 评论 {{$post->comments_count}}</p>

原文地址:https://www.cnblogs.com/jiaoda/p/7309132.html