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:\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)的更多相关文章
- laravel简书(1)
Laravel的社区生态 中文社区(http://laravel-china.org) 5.4中文文档(http://d.laravel-china.org/docs/5.4) Laravel源码地址 ...
- Laravel 5.4 快速开发简书:
Laravel 5.4 快速开发简书第1章 课程介绍 介绍课程的大体脉络和课程安排 第2章 Laravel 5.4介绍 本节课会带领大家介绍laravel的各个版本历史以及讨论php框架的未来发展趋势 ...
- iOS离屏渲染简书
更详细地址https://zsisme.gitbooks.io/ios-/content/chapter15/offscreen-rendering.html(包含了核心动画) GPU渲染机制: CP ...
- openlayers 3 简书
1. 简书http://www.jianshu.com/p/6785e755fa0d 2. 文档 http://anzhihun.coding.me/ol3-primer/ch03/03-02.htm ...
- Python 2.7_发送简书关注的专题作者最新一篇文章及连接到邮件_20161218
最近看简书文章关注了几个专题作者,写的文章都不错,对爬虫和数据分析都写的挺好,因此想到能不能获取最新的文章推送到Ipad网易邮箱大师.邮件发送代码封装成一个函数,从廖雪峰大神那里学的 http:// ...
- 从刚刚「简书」平台的短暂异常,谈Nginx An error occurred报错~
09.26简书平台的短暂异常 An error occurred. Sorry, the page you are looking for is currently unavailable. Plea ...
- swift调用oc语言文件,第三方库文件或者自己创建的oc文件——简书作者
Swift是怎样调用OC的第三方库的呢?请看下面详情: 情况一: 1.首先打开Xcode,iOS->Application->Single View Application, 选Next. ...
- iOS实现简书的账号识别方式(正则表达式)
通过简书iOS客户端登录,我们会看到请输入手机号或者邮箱登录,但是我们随机输入1234567的时候,便会弹出手机格式不正确,同样也会识别我们的邮箱格式,那么我们在项目中怎么实现这种判断呢? 0E471 ...
- 倒戈了,转投简书 -------->
深情自白 还记得数月前那个月黑风高的晚上,笔主偶遇简书,被那婀娜多姿的Markdown输出深深吸引不能自拔,从此立下毒誓要两边同时发布.然而天有不测风云(这边的太丑),前思后想寝食难安之后作出决定,正 ...
随机推荐
- sql 50题
/* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50717 Source Host : ...
- zabbix自动发现zabbix_agent后添加到所属组和链接到某些模块(九)
自动发现的两个操作:discovery(自动发现) and actions(发现后执行某个操作) 需求: 1:自动发现 Zabbix agent运行的主机 2:执行的动作 1)添加到所属组 ...
- 哈密顿绕行世界问题、n皇后问题
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- java volatile
volatile可以保证变量的可见性 当一个变量定义为volatile后,此变量对所有的线程具有可见性.这里的可见性是指当一个线程修改了这个变量的值,新值对于其他线程来说是可以立即得知的. 每次使用v ...
- Property referenced in indexed property path is neither an array nor a List nor a Map
记一次传参请求报错,没有解决 Invalid property 'distributeCars[0][ackStatus]' of bean class [com.api6.plate.prototy ...
- *浅解嵌入式中的BootLoader
本文只作为本人学习过程中的记录及时不时的突发奇想偶记.鄙人菜鸟一只,文中如有错误或疏漏,若读者肯不吝赐教,在下感激零涕.文章一直不断更新中 一.何为Bootloader 在嵌入式系统中,Bootloa ...
- Python的socket
第一部分socket的简单示例 服务器部分: """ Description: Author:Nod Date: Record: #------------------- ...
- storj白皮书v3最全面解读,Docker创始人的加入能否扳倒AWS S3
Storj新发了白皮书v3,地址是:https://storj.io/storjv3.pdf. 这次白皮书一共有90页,看完还真要费不少时间.如果你没有时间看,可以看一下我这篇快速技术解读. 上次St ...
- JS购物车编辑
实现了:第一件商品的加减实现了:全选/全不选(使用prop而不是attr)实现了:删除(遮罩层) 未实现:第二件商品的删除未实现:小计及应付款额的初始化(写死的) 计算小数乘法时,要先乘100 < ...
- Android Studio Fragment 无法获取 id的方法
在Fragment中,因为继承的父类的不同,导致在Fragment中无法获取到控件的id,此时,只要在获取findviewbyid前加上 getView()就可以了.