Kommentar- System-Teil-2

Alternatives Kommentarsystem zu Laravel Beauty Teil 2

Posted on November 13, 2015 in laravel, php, tutorial

Inhalt

  • Einführung
  • Controller
  • Request
  • View

Einführung

Im zweiten Teil des Kommentar- und Antwortsystem bearbeiten und wird der Adminbereich zur Freischaltung von Kommentaren, Antworten die nicht durch unser SpamPrevention gefiltert wurden, erstellt. Dieser Bereich ist sehr einfach gehalten. Es auch möglich die komplette SpamPreventionVerwaltung hier zu hinterlegen. Die Datentablle dynamischer zu gestalten und und und. Es ist nur eine einfache Möglichkeit zur Freischaltung.

Controller

Der Adminbereich bekommt seinen einen Resource - Controller und der wird über einen einfach Artisan-Befehl angelegt.

´´´ php artisan make:controller Admin/CommentController


Nach der erfolgreichen Erstellung, wird der Controller angepasst und mit den entsprechenden Methoden ausgefüllt. ```.php <?php namespace App\Http\Controllers\Admin; use App\Comment; use App\Reply; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; class CommentController extends Controller { /** * Display a listing of the resource. * * @return Response */ public function index() { $comments['comment'] = Comment::all(); $comments['replies'] = Reply::all(); return view('admin.comment.index') ->withComments($comments); } /** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { $explode = explode('.', $id); $cat = $explode[0]; $namespace = '\\App\\' . ucfirst(str_singular($explode[0])); $id = $explode[1]; $comment = $namespace::findOrFail($id); return view('admin.comment.edit', compact('cat', 'comment')); } /** * Update the specified resource in storage. * * @param Request $request * @param int $id * @return Response */ public function update(Requests\CommentUpdateRequest $request, $id) { $explode = explode('.', $id); $namespace = '\\App\\' . ucfirst(str_singular($explode[0])); $id = $explode[1]; $model = $namespace::findOrFail($id); $model->approved = $request->get('approved'); $model->content = $request->get('content'); $model->save(); return redirect("/admin/comment/". implode('.' ,$explode) . "/edit") ->withSuccess("Changes saved."); } /** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { $explode = explode('.', $id); $cat = $explode[0]; $namespace = '\\App\\' . ucfirst(str_singular($explode[0])); $id = $explode[1]; $model = $namespace::findOrFail($id); $model->delete(); return redirect('/admin/comment') ->withSuccess("The " . ucfirst($cat) . ": " . $model->content . " has been deleted."); } }

Nach der Bearbeitung des Controllers, wird die routes.php angepasst. Die Show-Methode wird nicht benötigt, daher Whitelist zur Methodennutzung.

//app\Http\routes.php
// nach resource('admin/tag', 'TagController', ['except' => 'show']); folgendes einfügen

resource('admin/comment', 'CommentController', ['only' => ['index', 'edit', 'update', 'destroy']]);

Request

Im Controller werden Userdaten weitergereicht, diese müssen noch zur weiteren Bearbeitung validiert werden. Die Requestdatei übernimmt hier die Arbeit. Als erstes wird artisan zur Erstellung von der Requestdatei genutzt. Im Anschluss wird dann die Dateien angepasst.

php artisan make:request CommentUpdateRequest
// app/Http/Requests/CommentUpdateRequest.php

<?php
namespace App\Http\Requests;

class CommentUpdateRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules()
    {
        return [
            'approved' => 'required',
            'content' => 'required',
        ];
    }
}

Der CommentUpdateRequest benötigt zwingend einen User, sowie auch Inhalt. Die post_id muss zwingend in der Datenbank vorhanden sein.

Views

Folgende Order- und Dateistruktur legen wir uns an.

resources/views/admin/comment
--> _form.blade.php
--> _modal.blade.php
--> edit.blade.php
--> index.blade.php
// comment/_form
<div class="form-group">
    <label for="approved" class="col-md-3 control-label">
        Approved
    </label>
    <div class="col-md-7">
        <label class="radio-inline">
            <input type="radio" name="approved"
                   id="approved"
            @if (! $comment->approved)
                   checked="checked"
                   @endif
                   value="0"> Denied
        </label>
        <label class="radio-inline">
            <input type="radio" name="approved"
            @if ($comment->approved)
                   checked="checked"
                   @endif
                   value="1"> Allowed
        </label>
    </div>
</div>

<div class="form-group">
    <label for="content" class="col-md-3 control-label">
        Content
    </label>
    <div class="col-md-8">
    <textarea class="form-control" id="content" data-provide="markdown" data-hidden-buttons="cmdPreview"
              name="content" rows="3">{{ $comment->content }}

    </textarea>
    </div>
</div>
// comment/_modal
{{-- Confirm Delete --}}
<div class="modal fade" id="modal-delete-{{ $cat . '-' . $comment->id }}" tabIndex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    ×
                </button>
                <h4 class="modal-title">Please Confirm</h4>
            </div>
            <div class="modal-body">
                <p class="lead">
                    <i class="fa fa-question-circle fa-lg"></i>
                    Are you sure you want to delete this comment?
                </p>
            </div>
            <div class="modal-footer">
                <form method="POST" action="/admin/comment/{{ $cat . '.' . $comment->id }}">
                    <input type="hidden" name="_token" value="{{ csrf_token() }}">
                    <input type="hidden" name="_method" value="DELETE">
                    <button type="button" class="btn btn-default"
                            data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-danger">
                        <i class="fa fa-times-circle"></i> Yes
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
// comment/edit
@extends('admin.layout')

@section('content')
    <div class="container-fluid">
        <div class="row page-title-row">
            <div class="col-md-12">
                <h3>Comments / Replies <small>» Edit</small></h3>
            </div>
        </div>

        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">Comment Edit Form</h3>
                    </div>
                    <div class="panel-body">

                        @include('admin.partials.errors')
                        @include('admin.partials.success')

                        <form class="form-horizontal" role="form" method="POST"
                              action="/admin/comment/{{ $cat . '.' . $comment->id }}">
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">
                            <input type="hidden" name="_method" value="PUT">
                            <input type="hidden" name="id" value="{{ $cat . '.' . $comment->id }}">

                            <div class="form-group">
                                <label for="comment" class="col-md-3 control-label">Category</label>
                                <div class="col-md-3">
                                    <p class="form-control-static">{{ $cat }}</p>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="comment" class="col-md-3 control-label">Created</label>
                                <div class="col-md-3">
                                    <p class="form-control-static">{{ $comment->created_at->format('jS \o\f F, Y g:i:s a') }}</p>
                                </div>
                            </div>

                            @include('admin.comment._form')

                            <div class="form-group">
                                <div class="col-md-7 col-md-offset-3">
                                    <button type="submit" class="btn btn-primary btn-md">
                                        <i class="fa fa-save"></i>
                                        Save Changes
                                    </button>
                                    <button type="button" class="btn btn-danger btn-md"
                                            data-toggle="modal" data-target="#modal-delete-{{ $cat . '-' . $comment->id }}">
                                        <i class="fa fa-times-circle"></i>
                                        Delete
                                    </button>
                                </div>
                            </div>

                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>
    @include('admin.comment._modal')

@stop
// comment/index
@extends('admin.layout')

@section('content')
    <div class="container-fluid">
        <div class="row page-title-row">
            <div class="col-md-6">
                <h3>Comments <small>» Listing</small></h3>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-12">

                @include('admin.partials.errors')
                @include('admin.partials.success')

                <table id="comments-table" class="table table-striped table-bordered">
                    <thead>
                    <tr>
                        <th class="hidden-sm">Datetime</th>
                        <th class="hidden-sm">User</th>
                        <th class="hidden-sm">Cat</th>
                        <th class="hidden-sm">Approved</th>
                        <th class="hidden-md">Content</th>
                        <th data-sortable="false">Actions</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach ($comments as $cat => $value)
                        @foreach($value as $comment)
                        <tr>
                            <td class="hidden-sm">{{ $comment->created_at->format('jS \o\f F, Y g:i:s a') }}</td>
                            <td class="hidden-sm">{{ $comment->user }}</td>
                            <td class="hidden-sm">{{ $cat }}</td>
                            <td class="hidden-sm">{{ $comment->approved }}</td>
                            <td class="hidden-md">{!! $comment->content_html !!}</td>
                            <td>
                                <a href="/admin/comment/{{ $cat . '.' . $comment->id }}/edit"
                                   class="btn btn-xs btn-info">
                                    <i class="fa fa-edit"></i> Edit
                                </a>
                                <button type="button" class="btn btn-xs btn-danger"
                                        data-toggle="modal" data-target="#modal-delete-{{ $cat . '-' . $comment->id }}">
                                    <i class="fa fa-times-circle"></i>
                                    Delete
                                </button>
                                @include('admin.comment._modal')
                            </td>
                        </tr>
                        @endforeach
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

@stop

@section('scripts')
    <script>
        $(function() {
            $("#comments-table").DataTable({
            });
        });
    </script>
@stop

Somit haben wir nun auch unseren Adminbereich durch. Ich wünsche viel Spass damit.

Für Fragen und Anregungen bin ich jederzeit offen. Hinterlasst mir dazu einfach einen kurzen Kommentar.

Viele Grüße

_ Cyrix _

This article has no comments. You can leave the first comment.