用户注册

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
{{--@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','\App\Http\Controllers\PostController@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('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\PostController@zan');
//取消赞
Route::get('/{post}/unzan','\App\Http\Controllers\PostController@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(\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简书(2)的更多相关文章

  1. laravel简书(1)

    Laravel的社区生态 中文社区(http://laravel-china.org) 5.4中文文档(http://d.laravel-china.org/docs/5.4) Laravel源码地址 ...

  2. Laravel 5.4 快速开发简书:

    Laravel 5.4 快速开发简书第1章 课程介绍 介绍课程的大体脉络和课程安排 第2章 Laravel 5.4介绍 本节课会带领大家介绍laravel的各个版本历史以及讨论php框架的未来发展趋势 ...

  3. iOS离屏渲染简书

    更详细地址https://zsisme.gitbooks.io/ios-/content/chapter15/offscreen-rendering.html(包含了核心动画) GPU渲染机制: CP ...

  4. openlayers 3 简书

    1. 简书http://www.jianshu.com/p/6785e755fa0d 2. 文档 http://anzhihun.coding.me/ol3-primer/ch03/03-02.htm ...

  5. Python 2.7_发送简书关注的专题作者最新一篇文章及连接到邮件_20161218

    最近看简书文章关注了几个专题作者,写的文章都不错,对爬虫和数据分析都写的挺好,因此想到能不能获取最新的文章推送到Ipad网易邮箱大师.邮件发送代码封装成一个函数,从廖雪峰大神那里学的  http:// ...

  6. 从刚刚「简书」平台的短暂异常,谈Nginx An error occurred报错~

    09.26简书平台的短暂异常 An error occurred. Sorry, the page you are looking for is currently unavailable. Plea ...

  7. swift调用oc语言文件,第三方库文件或者自己创建的oc文件——简书作者

    Swift是怎样调用OC的第三方库的呢?请看下面详情: 情况一: 1.首先打开Xcode,iOS->Application->Single View Application, 选Next. ...

  8. iOS实现简书的账号识别方式(正则表达式)

    通过简书iOS客户端登录,我们会看到请输入手机号或者邮箱登录,但是我们随机输入1234567的时候,便会弹出手机格式不正确,同样也会识别我们的邮箱格式,那么我们在项目中怎么实现这种判断呢? 0E471 ...

  9. 倒戈了,转投简书 -------->

    深情自白 还记得数月前那个月黑风高的晚上,笔主偶遇简书,被那婀娜多姿的Markdown输出深深吸引不能自拔,从此立下毒誓要两边同时发布.然而天有不测风云(这边的太丑),前思后想寝食难安之后作出决定,正 ...

随机推荐

  1. phpmyadmin nginx设置

    1,解压缩phpmyadmin4.2.8压缩包到/usr/local/phpMyAdmin 2,复制config.sample.inc.php为config.inc.php 3,修改nginx.con ...

  2. web前端常用代码于面试等资源

    https://www.cnblogs.com/moqiutao/p/4766146.html

  3. apache开启验证登录

    对某个目录开启验证登录 <Directory /var/www/html/admin > AllowOverride All Order allow,deny Allow from all ...

  4. 邮件报警以及服务端能否ping通客户端的小例子(三)

           就这个小小的东西,弄了一天,弄的头晕眼花,毕竟第一次弄这个,记录下来,若干年之后,回看这些笔记,不知是什么样的感想,哈哈.我学一个东西的时候喜欢系统的来,一点一点的来,做这个的时候想法很 ...

  5. Ubuntu 16.04出现:Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/cache/app-info -a -e /usr/bin/appstreamcli; then appstreamcli refresh > /dev/null; fi'

    错误: Reading package lists... Done E: Problem executing scripts APT::Update::Post-Invoke-Success 'if ...

  6. 3.1 MathType上标位置调整的两种方法

    具体操作步骤如下: 1.打开MathType窗口后在工作区域中编辑好公式. 2.调整上标位置有两种方法: (1)选中要调整的上标,按下“Ctrl+↑,Ctrl+↓,Ctrl+←,Ctrl+→”进行调整 ...

  7. mac os high sierra下搭建php多版本-php5.2+php5.6-nginx

    xampp的apache彻底启动不来了. php52的编译参数 ./configure --prefix=/usr/local/Cellar/php52bysk/ --with-config-file ...

  8. Flask--(登录注册)抽取视图函数

    视图函数抽取: 在info目录下准备视图业务模块包:modules 在modules中添加首页模块包index 在index包的__init__中导入蓝图 在index的__init__创建蓝图 在i ...

  9. seleniuim面试题1

    seleniuim面试题1 乙醇 创建于 4 个月 之前 最后更新时间 2018-09-11 selenium中如何判断元素是否存在? selenium中没有提供原生的方法判断元素是否存在,一般我们可 ...

  10. php post和get请求

    1. POST请求 public function post($url, $params = array()) { /*初始化*/ $ch = curl_init(); /*设置变量*/ curl_s ...