Skip to content

Commit

Permalink
Use view composer for comment form andrewelkins#236
Browse files Browse the repository at this point in the history
With suggestions from @Aristona  and @gcphost
  • Loading branch information
baopham committed Nov 25, 2014
1 parent 2145257 commit 416dad7
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 30 deletions.
20 changes: 20 additions & 0 deletions app/Acme/Composers/CommentComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php namespace Acme\Composers;

class CommentComposer {

public function compose($view)
{

$viewData = $view->getData();
if (!\Auth::check()) {
return $view->nest('commentForm', 'site.partials.comment_auth');
}

if (!$viewData['canComment']) {
return $view->nest('commentForm', 'site.partials.comment_perm');
}

return $view->nest('commentForm', 'site.partials.comment_form', $viewData);
}

}
3 changes: 3 additions & 0 deletions app/composers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

View::composer('site/blog/view_post', 'Acme\Composers\CommentComposer');
9 changes: 9 additions & 0 deletions app/start/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,12 @@
*/

require __DIR__.'/../filters.php';

/*
|--------------------------------------------------------------------------
| Require The Composers
|--------------------------------------------------------------------------
|
*/

require __DIR__.'/../composers.php';
30 changes: 1 addition & 29 deletions app/views/site/blog/view_post.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,6 @@
<hr />
@endif

@if ( ! Auth::check())
You need to be logged in to add comments.<br /><br />
Click <a href="{{{ URL::to('user/login') }}}">here</a> to login into your account.
@elseif ( ! $canComment )
You don't have the correct permissions to add comments.
@else

@if($errors->has())
<div class="alert alert-danger alert-block">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{{ $commentForm }}

<h4>Add a Comment</h4>
<form method="post" action="{{{ URL::to($post->slug) }}}">
<input type="hidden" name="_token" value="{{{ Session::getToken() }}}" />

<textarea class="col-md-12 input-block-level" rows="4" name="comment" id="comment">{{{ Request::old('comment') }}}</textarea>

<div class="form-group">
<div class="col-md-12">
<input type="submit" class="btn btn-default" id="submit" value="Submit" />
</div>
</div>
</form>
@endif
@stop
2 changes: 2 additions & 0 deletions app/views/site/partials/comment_auth.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
You need to be logged in to add comments.<br /><br />
Click <a href="{{{ URL::to('user/login') }}}">here</a> to login into your account.
12 changes: 12 additions & 0 deletions app/views/site/partials/comment_form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h4>Add a Comment</h4>
<form method="post" action="{{{ URL::to($post->url()) }}}">
<input type="hidden" name="_token" value="{{{ Session::getToken() }}}" />

<textarea class="col-md-12 input-block-level" rows="4" name="comment" id="comment">{{{ Request::old('comment') }}}</textarea>

<div class="form-group">
<div class="col-md-12">
<input type="submit" class="btn btn-default" id="submit" value="Submit" />
</div>
</div>
</form>
1 change: 1 addition & 0 deletions app/views/site/partials/comment_perm.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You don't have the correct permissions to add comments.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
"app/database/migrations",
"app/database/seeds",
"app/tests/library"
]
],
"psr-0": {
"Acme": "app/"
}
},
"scripts": {
"pre-update-cmd": [
Expand Down

0 comments on commit 416dad7

Please sign in to comment.