码迷,mamicode.com
首页 > 其他好文 > 详细

菜鸟的蜕变:教你一步一步创建基于laravel5的简易论坛系统(3)

时间:2016-01-12 13:13:39      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

上篇文章我已经带大家熟悉了laravel框架的基本结构、路由功能、控制器、视图等各个部分交互方式,这篇文章继续带大家做一下其他页面。

创建帖子内容页和个人主页路由,增加

Route::get(‘article_show/{id}‘,‘Home\HomeController@article_show‘);                //帖子内容页
Route::get(‘Profile/{id}‘,‘Home\ProfileController@index‘);                        //个人主页

创建\luntan\app\Http\Controllers\User\ArticleController.php

<?php namespace App\Http\Controllers\User;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;



use App\Article;
use App\Comment;
use App\Category;
use DB;
use Redirect, Input, Auth;
class ArticleController extends Controller {
    /**
     * 显示新建消息页面
     *
     * @return view
     */
    public function index()
    {
        $Category = Category::all();
        return view(‘user.create_article‘)->withcategory($Category);
    }

    /**
     * 处理新建消息
     *
     * @return Redirect
     */
    public function create_article(Request $request)
    {
        $this->validate($request,[
                ‘title‘ => ‘required|unique:articles|max:20‘,
                ‘content‘    => ‘required‘,
            ]);
        
        $article = new Article;
        $article->title = Input::get(‘title‘);
        $article->content = Input::get(‘content‘);
        $article->user_id = Auth::user()->id;
        $article->Category = Input::get(‘optionsRadios‘);
        //如果optionsRadios不等于Categorys表中任何一个id,则重定向到上边的index方法
        $c = DB::table(‘categories‘)->select(‘id‘)->get();
        $arr = array();
        foreach ($c as $key => $value) {
            $arr[] = $value->id;
        }
        if(!in_array($article->Category,$arr)){
            return Redirect::to(‘/user/article‘);//分类不合法
        };

        if ($article->save())
        {
            return Redirect::to(‘/‘);
        }
        else
        {
            return Redirect::back()->withInput()->withErrors(‘发布失败!‘);
        }
    }

    /**
     * 评论
     * @return bool
     */
    public function comments(Request $request)
    {
        $comment = new Comment;
        $comment->content = $request->content;
        $comment->article_id = $request->article_id;
        $comment->user_id = Auth::id();
        $comment->article_user_id =Article::find($request->article_id)[‘user_id‘];
        $comment->save();
        return Redirect(‘/article_show‘.‘/‘.$request->article_id);
    }

    /**
     * 帖子动态
     */
    public function me()
    {
        $c = DB::table(‘comments as c‘)
        ->leftJoin(‘articles as m‘,‘c.article_id‘,‘=‘,‘m.id‘)
        ->leftJoin(‘users as u‘,‘c.user_id‘,‘=‘,‘u.id‘)
        ->where([‘c.article_user_id‘=>Auth::id()])
        ->select(‘c.*‘,‘m.title‘,‘u.name‘,‘m.category‘)
        ->orderBy(‘c.created_at‘,‘desc‘)
        ->paginate(30);
        foreach ($c as $key => $value) {
            $find = Category::find($value->category);
            $value->color = $find->color;
            $value->categoryid = $find->id;
            $value->category = $find->name;
            $value->created_at = $this->t_time($value->created_at);
            // $value->color = Category::find($value->category)->color;
            // $value->categoryid = Category::find($value->category)->id;
            // $value->category = Category::find($value->category)->name;
            // $value->created_at = $this->t_time($value->created_at);
        }
        return view(‘user.me‘)->withc($c);
    }
}

新建帖子详情页视图\luntan\resources\views\home\article_show.blade.php

@extends(‘home/default‘)

@section(‘content‘)


<!--版式-->
<link rel="stylesheet" href="{{ asset(‘Index/css/img-list.css‘) }}">
<style type="text/css">
#asdfs{
    height: 100px;
    width: 100px;
    border-radius: 50%;
}
</style>
<div class="header">
    <h2 class="swing animated"><a href="{{URL(‘/Profile/‘)}}/{{$article_show->user_id}}" title="了解他"><img id="asdfs" src="{{asset(‘/‘)}}{{ $article_show->face }}"></a></h2>
    <h1 class="fadeInDown animated" style="font-size:18px;" title="宣布">{{ $article_show->title }}</h1>
</div>


<style type="text/css">
#tp{
    display: block;
    float: left;
    margin-top: 5px;
    margin-right: 4px;
    height: 12px;
    width: 12px;
    opacity: 0.8;
    border-radius: 6px;
}
#k{
    float: right;
}
</style>
<div class="content" style="border-bottom: 1px solid #eee;">


<?php echo $article_show->content;?>


<h3 class="content-subhead" style="border-bottom: 0px;">发布于 {{date("Y:m:d - H时i分",strtotime($article_show->created_at))}}<div id=‘k‘><span id=‘tp‘ style="background: rgb({{ $article_show->color }});"></span><a href="{{asset(‘/‘)}}{{$article_show->class_id}}">{{ $article_show->class }}</a></div></h3>
<hr>
<style type="text/css">
.img_h{width:40px;height: 40px;border-radius: 20px;}
.nc{font-size: 12px;text-align: left;color: #7e7e7e;}
#ontt{font-size: 12px;color: #252525;text-align: left;}
#tm{color: #7e7e7e;font-size: 12px;margin-right: 100px;text-align: left;}

.pl{margin: 40px;}
</style>

@foreach ($comments as $com)
<div class="pl">
    <img class="img_h"  src="{{url(‘/‘)}}/{{$com->face}}">
    <a class="nc" href="{{url(‘/Profile‘)}}/{{ $com->uid }}" alt="{{ $com->word }}">{{ $com->name }}</a><p id=‘tm‘>{{ $com->created_at }}</p>
    <p id="ontt"><?php echo $com->content;?></p>
</div>
@endforeach
    <style scoped>

        .button-success,
        .button-error,
        .button-warning,
        .button-secondary {
            color: white;
            border-radius: 4px;
            text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
             font-size: 30%;
        }

        .button-success {
            background: rgb(28, 184, 65); /* this is a green */
            position: relative;
            top: -9px;
            left: 10px;
        }


    </style>
    <style type="text/css">
.pagination {
    border-radius: 4px;
    display: inline-block;
    margin: 20px 0;
    padding-left: 0;
}
.pagination > li {
    display: inline;
}
.pagination > li > a, .pagination > li > span {
    background-color: #ffffff;
    border: 1px solid #dddddd;
    color: #337ab7;
    float: left;
    line-height: 1.42857;
    margin-left: -1px;
    padding: 6px 12px;
    position: relative;
    text-decoration: none;
}
.pagination > li:first-child > a, .pagination > li:first-child > span {
    border-bottom-left-radius: 4px;
    border-top-left-radius: 4px;
    margin-left: 0;
}
.pagination > li:last-child > a, .pagination > li:last-child > span {
    border-bottom-right-radius: 4px;
    border-top-right-radius: 4px;
}
.pagination > li > a:hover, .pagination > li > span:hover, .pagination > li > a:focus, .pagination > li > span:focus {
    background-color: #eeeeee;
    border-color: #dddddd;
    color: #23527c;
}
.pagination > .active > a, .pagination > .active > span, .pagination > .active > a:hover, .pagination > .active > span:hover, .pagination > .active > a:focus, .pagination > .active > span:focus {
    background-color: #337ab7;
    border-color: #337ab7;
    color: #ffffff;
    cursor: default;
    z-index: 2;
}
</style>
<?php echo $comments->render(); ?>
<script type="text/javascript" src="{{ asset(‘editor/ckeditor/ckeditor.js‘) }}"></script>
<h3>发表回复</h3>
<form class="pure-form pure-form-stacked" action="{{URL(‘/user/comments‘)}}" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="message_id" value="{{ $article_show->id }}">
<textarea id="TextArea1" cols="20" rows="2" name="content" class="ckeditor"></textarea> <br>
    <input type=‘submit‘ class=‘pure-button pure-button-default‘ value=" 发表 ">
</form>



</div>
@endsection


点击首页帖子标题出现页面

技术分享

 

菜鸟的蜕变:教你一步一步创建基于laravel5的简易论坛系统(3)

标签:

原文地址:http://www.cnblogs.com/anzichen/p/5123784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!