laravel简书

Posted able-woman

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel简书相关的知识,希望对你有一定的参考价值。

用户注册

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:\php\bianshu>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、在App\Policies\PostPolicy.php中修改以下内容:

protected $policies = [
    //‘App\Model‘ => ‘App\Policies\ModelPolicy‘,
   
‘App\Post‘=>‘App\Policies\PostPolicy‘,
];

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
{{[email protected]}}
@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‘,‘\App\Http\Controllers\[email protected]‘);

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(‘App\Post‘);
    }
}

 

实现评论列表

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(‘App\User‘);
}

 

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‘,‘\App\Http\Controllers\[email protected]‘);
//取消赞
Route::get(‘/{post}/unzan‘,‘\App\Http\Controllers\[email protected]‘);

 

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(\App\Zan::class)->where(‘user_id‘,$user_id);
}
//文章的所有赞
public function zans() {
    return $this->hasMany(\App\Zan::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>


























































































































以上是关于laravel简书的主要内容,如果未能解决你的问题,请参考以下文章

Laravel5.4 快速开发简书网站

laravel特殊功能代码片段集合

需要一种有效的方法来避免使用 Laravel 5 重复代码片段

Laravel:如何在控制器的几种方法中重用代码片段

Laravel(PHP)使用Swagger生成API文档不完全指南 - 基本概念和环境搭建 - 简书

(◍•ᴗ•◍)ゝ