"Action App\Http\Controllers\CommentRepliesController@createReply not defined" Laravel 错误
"Action App\Http\Controllers\CommentRepliesController@createReply not defined" error on Laravel
我正在尝试添加一个选项来回复 post 上的评论,但我一直收到:
CommentRepliesController@createReply is not defined.
通过 PostCommentsController@store
添加对 post 的回复效果非常好。但是,当我尝试通过返回 post 或直接转到 URL 中的 comment/reply
来添加对评论的回复时,它会给我上面的错误。
以下是我的路线:
Route::group(['middleware'=>'auth'], function(){
Route::resource('comment/reply', 'CommentRepliesController@createReply');
});
以下是我的CommentRepliesController@createReply
:
public function createReply(Request $request){
$user = Auth::user();
if($user->photo){
$data = [
'comment_id' => $request->comment_id,
'author' => $user->name,
'email' => $user->email,
'photo' => $user->photo->file,
'body' => $request->body
];
} else{
$data = [
'comment_id' => $request->comment_id,
'author' => $user->name,
'email' => $user->email,
'body' => $request->body
];
}
CommentReply::create($data);
$request->session()->flash('reply_message', 'Your reply has been submitted
and is awaiting moderation.');
return redirect()->back();
}
下面是我的 post.blade.php:
@extends('layouts.blog-post')
@section('content')
<!-- Blog Post -->
<!-- Title -->
<h1>{{$post->title}}</h1>
<!-- Author -->
<p class="lead">
by <a href="#">{{$post->user->name}}</a>
</p>
<hr>
<!-- Date/Time -->
<p><span class="glyphicon glyphicon-time"></span>
Posted on {{$post->created_at->diffForHumans()}}</p>
<hr>
<!-- Preview Image -->
<img class="img-responsive" src="{{$post->photo->file}}" alt="">
<hr>
<!-- Post Content -->
<p class="lead">
<p>{{$post->body}}</p>
<hr>
@if(Session::has('comment_message'))
{{session('comment_message')}}
@endif
<!-- Blog Comments -->
@if(Auth::check())
<!-- Comments Form -->
<div class="well">
<h4>Leave a Comment:</h4>
{!! Form::open(['method'=>'POST', 'action'=>'PostCommentsController@store'])!!}
<input type="hidden" name="post_id" value="{{$post->id}}"/>
<!--<input type="hidden" name="_token" value="{{ csrf_token() }}">-->
{!! csrf_field() !!}
<div class="form-group">
{!! Form::label('body','Body: ') !!}
{!! Form::textarea('body', null, ['class'=>'form-control', 'rows'=>3]) !!}
</div>
<div class="form-group">
{!! Form::submit('Post Comments', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
@endif
<hr>
<!-- Posted Comments -->
@if(count($comments) > 0)
@foreach($comments as $comment)
<!-- Comment -->
<div class="media">
<a class="pull-left" href="#">
<img height="64" width="64" class="media-object" src="{{$comment->photo}}" alt="">
</a>
<div class="media-body">
<h4 class="media-heading">{{$comment->author}}
<small>{{$comment->created_at->diffForHumans()}}</small>
</h4>
<p>{{$comment->body}}</p>
<!-- Nested Comment -->
<div class="media">
<a class="pull-left" href="#">
<img class="media-object" src="http://placehold.it/400x400" alt="">
</a>
<div class="media-body">
<h4 class="media-heading">Nested Start Bootstrap
<small>August 25, 2014 at 9:30 PM</small>
</h4>
Cras sit amet nibh libero, in gravida nulla.
Nulla vel metus scelerisque ante sollicitudin
commodo. Cras purus odio, vestibulum in vulputate
at, tempus viverra turpis. Fusce condimentum nunc
ac nisi vulputate fringilla. Donec lacinia congue
felis in faucibus.
</div>
</div>
<!-- End Nested Comment -->
@if(Session::has('reply_message'))
<p class="bg-danger">{{session('reply_message')}}</p>
@endif
<!--Comment Reply Form-->
{!! Form::open(['method'=>'POST', 'action'=>'CommentRepliesController@createReply'])!!}
<div class="form-group">
<input type="hidden" name="comment_id" value="{{comment_id}}"/>
{!! Form::label('body','Body: ') !!}
{!! Form::textarea('body', null, ['class'=>'form-control', 'rows'=>1]) !!}
</div>
<div class="form-group">
{!! Form::submit('Reply', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
<!--End of Comment Reply Form-->
</div>
</div>
@endforeach
@endif
@stop
提前致谢。
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code.
Route::resource
自动使用资源控制器中的 "CRUD" 操作。请参阅文档 here
如果您想在控制器中使用特定功能,请不要使用 Route::resource
。使用任何你想要的方法。例如,如果您的方法是 POST
,请像这样使用 Route::post
:
Route::group(['middleware'=>'auth'], function(){
Route::post('comment/reply', 'CommentRepliesController@createReply');
});
我正在尝试添加一个选项来回复 post 上的评论,但我一直收到:
CommentRepliesController@createReply is not defined.
通过 PostCommentsController@store
添加对 post 的回复效果非常好。但是,当我尝试通过返回 post 或直接转到 URL 中的 comment/reply
来添加对评论的回复时,它会给我上面的错误。
以下是我的路线:
Route::group(['middleware'=>'auth'], function(){
Route::resource('comment/reply', 'CommentRepliesController@createReply');
});
以下是我的CommentRepliesController@createReply
:
public function createReply(Request $request){
$user = Auth::user();
if($user->photo){
$data = [
'comment_id' => $request->comment_id,
'author' => $user->name,
'email' => $user->email,
'photo' => $user->photo->file,
'body' => $request->body
];
} else{
$data = [
'comment_id' => $request->comment_id,
'author' => $user->name,
'email' => $user->email,
'body' => $request->body
];
}
CommentReply::create($data);
$request->session()->flash('reply_message', 'Your reply has been submitted
and is awaiting moderation.');
return redirect()->back();
}
下面是我的 post.blade.php:
@extends('layouts.blog-post')
@section('content')
<!-- Blog Post -->
<!-- Title -->
<h1>{{$post->title}}</h1>
<!-- Author -->
<p class="lead">
by <a href="#">{{$post->user->name}}</a>
</p>
<hr>
<!-- Date/Time -->
<p><span class="glyphicon glyphicon-time"></span>
Posted on {{$post->created_at->diffForHumans()}}</p>
<hr>
<!-- Preview Image -->
<img class="img-responsive" src="{{$post->photo->file}}" alt="">
<hr>
<!-- Post Content -->
<p class="lead">
<p>{{$post->body}}</p>
<hr>
@if(Session::has('comment_message'))
{{session('comment_message')}}
@endif
<!-- Blog Comments -->
@if(Auth::check())
<!-- Comments Form -->
<div class="well">
<h4>Leave a Comment:</h4>
{!! Form::open(['method'=>'POST', 'action'=>'PostCommentsController@store'])!!}
<input type="hidden" name="post_id" value="{{$post->id}}"/>
<!--<input type="hidden" name="_token" value="{{ csrf_token() }}">-->
{!! csrf_field() !!}
<div class="form-group">
{!! Form::label('body','Body: ') !!}
{!! Form::textarea('body', null, ['class'=>'form-control', 'rows'=>3]) !!}
</div>
<div class="form-group">
{!! Form::submit('Post Comments', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
@endif
<hr>
<!-- Posted Comments -->
@if(count($comments) > 0)
@foreach($comments as $comment)
<!-- Comment -->
<div class="media">
<a class="pull-left" href="#">
<img height="64" width="64" class="media-object" src="{{$comment->photo}}" alt="">
</a>
<div class="media-body">
<h4 class="media-heading">{{$comment->author}}
<small>{{$comment->created_at->diffForHumans()}}</small>
</h4>
<p>{{$comment->body}}</p>
<!-- Nested Comment -->
<div class="media">
<a class="pull-left" href="#">
<img class="media-object" src="http://placehold.it/400x400" alt="">
</a>
<div class="media-body">
<h4 class="media-heading">Nested Start Bootstrap
<small>August 25, 2014 at 9:30 PM</small>
</h4>
Cras sit amet nibh libero, in gravida nulla.
Nulla vel metus scelerisque ante sollicitudin
commodo. Cras purus odio, vestibulum in vulputate
at, tempus viverra turpis. Fusce condimentum nunc
ac nisi vulputate fringilla. Donec lacinia congue
felis in faucibus.
</div>
</div>
<!-- End Nested Comment -->
@if(Session::has('reply_message'))
<p class="bg-danger">{{session('reply_message')}}</p>
@endif
<!--Comment Reply Form-->
{!! Form::open(['method'=>'POST', 'action'=>'CommentRepliesController@createReply'])!!}
<div class="form-group">
<input type="hidden" name="comment_id" value="{{comment_id}}"/>
{!! Form::label('body','Body: ') !!}
{!! Form::textarea('body', null, ['class'=>'form-control', 'rows'=>1]) !!}
</div>
<div class="form-group">
{!! Form::submit('Reply', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
<!--End of Comment Reply Form-->
</div>
</div>
@endforeach
@endif
@stop
提前致谢。
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code.
Route::resource
自动使用资源控制器中的 "CRUD" 操作。请参阅文档 here
如果您想在控制器中使用特定功能,请不要使用 Route::resource
。使用任何你想要的方法。例如,如果您的方法是 POST
,请像这样使用 Route::post
:
Route::group(['middleware'=>'auth'], function(){
Route::post('comment/reply', 'CommentRepliesController@createReply');
});