Skip to content

qq3102328040/laravel5-blog

Repository files navigation

laravel5-blog

##简言 php初学者, 基础还没啥呢就作死开看laravel, 想写个博客玩玩, 顺便记录一下过程.

编写日志

###2016年05月12日

完成了简单的登陆页面, laravel5自带的auth默认登陆的表单字段是email,通过追踪源代码发现

return property_exists($this, 'username') ? $this->username : 'email';

AuthenticatesUsers类下添加

protected $username = 'name';

即可实现使用用户名登陆.

前端还是不会,登陆页面粘的bootstrap的模板,值得注意的是style标签要在link标签之后达到对bootstrap覆盖的效果. 更新:把style换成了link,放在public的单独文件里,发现不可用,加入

rel="stylesheet"

后正常, 百度到这是告诉浏览器怎么去编码 http://www.w3school.com.cn/tags/att_link_rel.asp

写好了导航栏navbar,感谢凯哥rvum帮忙解决问题.

问题: 看视频上可以一键引入bootstrap, 不知道怎么做到的, 百度无果

###2016年05月13日

将路径包上了url函数, 也就是变成了绝对路径, 看其他代码都是这样写, 不知道为什么.

完成了简单的创建文章和管理文章页面, 其实很简单,但对毫无前端美感的人来说是个励志的故事...

####关于创建文章逻辑

有两种写法

第一种比较传统 参考https://github.com/yccphp/laravel-5-blog/blob/master/app/Http/Controllers/backend/ArticleController.php 将接收参数的处理和数据的添加写在控制器的store()方法中, 简单明了.

第二种参考http://laravelacademy.org/post/2358.html 在request中添加了postFillData()方法来处理字段, 减少了store中的代码. 小菜表示不知道这两种那个好, 但感觉接收参数放在request(响应)请求里比较合乎常理,所以这样写了

调试时发现提示MassAssignmentException错误 在Content模型类中加入

protected $fillable = array('title', 'text', 'author', 'last_edit_time');

后解决

更改了AuthController, 禁止普通用户注册, 参考http://laravelacademy.org/post/2279.html 有时间应该读读Auth的代码.

问题:

  1. (已解决:Auth->user()->name就是...)Auth->user()返回的是一个类,暂时没发现返回名字字符串的方法(使用id Auth::id(), 返回一个数(弱类型语言只能这么理解))
  2. 一开始字段不是text而是content(和数据库字段不同) $request->content 内容为提交的所有东西, 暂时不知道为什么.

###2016年05月14日

####完成了管理文章页面

在blade中使用分页$content->render()时发现

使用{{ $content->render }}时输出

<ul class="pagination"><li class="disabled"><span>«</span></li> <li class="active"><span>1</span></li><li><a href="http://localhost:8000/admin/content?page=2">2</a></li><li><a href="http://localhost:8000/admin/content?page=3">3</a></li> <li><a href="http://localhost:8000/admin/content?page=2" rel="next">»</a></li></ul>
//页面为
 <ul class="pagination"><li class="disabled"><span>&laquo;</span></li> <li class="active"><span>1</span></li><li><a href="http://localhost:8000/admin/content?page=2">2</a></li><li><a href="http://localhost:8000/admin/content?page=3">3</a></li> <li><a href="http://localhost:8000/admin/content?page=2" rel="next">&raquo;</a></li></ul>

改用{!! $content->render() !!} 后浏览器正常解析

这是因为 {!! !!} 会对输出进行解析 而 {{ }} 会对输出进行转义 在这个教程里有提到过https://laracasts.com/series/laravel-5-fundamentals

使用Carbon类美化时间参考http://laravel5-book.kejyun.com/package/tool/package-tool-carbon.html 输出时用

\Carbon\Carbon::parse($content->created_at)->format('Y-m-d H:i')

将秒去掉了.

问题

  1. 表格对其问题,想让其他列固定,只有标题列长度浮动,不知道怎么弄
  2. 还有就是不知道如果自己需要写css怎么和bootstrap一起.

###2016年05月15日

####细节修改

在Content Model上添加

protected $dates = ['last_edit_time'];

使自己添加的字段'last_edit_time'被解析成Carbon类的形式,方便操作

看了修改器, 想改一下, 结果2b的在request里改半天.. 这玩意应该放在model里, 类似java的set 和 get方法, 通过追踪可以发现Model.php里有setAttribute方法. 关于动态修改Query Scope, 没看懂.. 参考 http://laravelacademy.org/post/146.html https://laravist.com/article/16 https://laracasts.com/series/laravel-5-fundamentals/episodes/11 http://blog.qiji.tech/archives/2578

问题:

  1. 作者为0时会抛异常,直接error了, 不知道怎么处理.

###2016年05月16日

####添加文章 增加错误提示

####添加关联模型方法

参考https://phphub.org/topics/364 一开始报错, 找了半天也没找到答案, 后来百度了下http://stackoverflow.com/questions/18084310/laravel-class-not-found-with-one-to-many 按这种方法加上绝对路径 解决

注意 hasMany 和 belongsTo 方法的参数是不一样的, 因为文章用的是cid(略蛋疼, find方法都不能用)而不是id , 所以一开始写的是cid, 报错 总不明白哪错了, 后来追踪了下代码, 明白了, 具体不说了, 可以追踪下代码看看

问题:

  1. 是简单了不少, 但一下把user表的类拿过来, 安全吗?
  2. 效率呢?

###2016年05月17日

####完成编辑文章

将创建文章和编辑文章共用, 发现问题, 编辑文章需要变量, 可以在页面上用isset()方法解决.或者中间件?(不会) 参考https://segmentfault.com/q/1010000004513381

一开始因为content表的主键不是id而各种蛋疼, 后来看了看Model类的源代码, 发现

protected $primaryKey = 'id';

后将content模型的$primaryKey设置为'cid', 完美解决

###2016年05月18日 增加了metas表, 准备把标签和分类(栏目)一块写, 创建和管理也写一块.

###2016年05月19日

####完成了分类部分

问题

  1. 在CategoryController中 index()方法和 edit()方法都传入了category的列表, 感觉这是一种浪费, 应该可以合并到一起. 尝试了用iframe , 失败..

###2016年05月20日

####完成标签创建

问题:

  1. 管理标签分级那一堆冗余代码, 不知道如何处理

###2016年05月21日

####完成标签修改

一开始删除标签的button没有定义动作, 但效果和submit一样, 也就是删除标签和创建标签效果一样, 好尴尬. 后来问凯哥, 发现黑科技, 把button标签换成a标签, class不变, 完美解决.

###2016年05月22日

####完成最最最最基本的上传

###2016年05月23日

####先完成了文件管理视图 先提交了, 跑步去

###2016年05月24日

文件管理写不出来, 无耻的抄袭了http://laravelacademy.org/post/2333.html#ipt_kb_toc_2333_4的manager类

###2016年05月25日

####完成了文件管理基本逻辑 界面细节以后再改, 快两点了, 虽然不困也得睡.

###2016年05月26日 添加了relationships表, 垃圾校园网又出问题了, 先修网

###2016年05月27日

####完成创建文章关联分类 在创建文章页面添加栏目是, 使用的是checkbox, 但是接收时发现接收到的总是最后一个数, 查资料后发现, 将name=category改为name=category[], 接收的变为数组, 解决 参考https://segmentfault.com/q/1010000005107466

创建relationship时, 默认增加了时间戳字段导致失败, 在模型类中加入

public $timestamps = false;

解决

###2016年05月28日

####添加了relationship和meta的关联模型

###2016年05月29日

####完成修改文章关联分类和标签

###2016年05月30日

问题

  1. 想用gulp引入bootstrap, 但我的package.json里面就两行, 为什么执行npm install的时候跑了那么长时间. 都跑了几百兆了..日了狗了, 根本停不下来

###2016年05月31日

引入了bootstrap和font-awesome, 给文件列表加了图标 完成删除分类

###2016年06月01日

儿童节了, 还有二十天考试, 要把这玩意放一放了, 基本的东西写的都差不多了, 剩下的东西有

明确的

  1. 部分删除(文章, 标签, 文件)
  2. 文件管理(新建文件夹, 把上传合并在管理页面)
  3. markdown(还不知道怎么弄)

不明确的

  1. 设置
  2. 其他小东西

时间不怎么够用了, 算今天连击21天吧, 有种说法叫21天养成习惯, 不过我这到21天就要断了, 先准备考试吧, 还有事不扯了, 这玩意会写完它的.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages