【问题标题】:How do i fix this error i'm getting "Sorry, the page you are looking for could not be found."如何解决此错误,我收到“抱歉,找不到您要查找的页面”。
【发布时间】:2020-03-06 04:13:45
【问题描述】:

我有这个应用程序,我试图让问题的作者能够将答案标记为最佳答案。

我创建了一个AcceptAnswerController,它已在routes/web.php 文件中注册,如下所示:

AcceptAnswerController.php

class AcceptAnswerController extends Controller
{
    public function __invoke(Answer $answer) 
    {
        $this->authorize('accept', $answer);

        $answer->question->acceptBestAnswer($answer);
        return back();
    }
}

web.php

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::resource('questions', 'QuestionsController')->except('show');

Route::resource('questions.answers', 'AnswersController')->only(['store', 'edit', 'update', 'destroy']);

Route::get('questions/{slug}', 'QuestionsController@show')->name('questions.show');

Route::post('answers/{answer}/accept', 'AcceptAnswerController')->name('answers.accept');

在我的答案视图中,我有以下内容:

<div class="row mt-4">
    <div class="col-md-12">
        <div class="card">
            <div class="card-body">
                <div class="card-title">
                    <h2>{{ $answersCount . " " . str_plural('Answer', $answersCount) }}</h2>
                </div>
                <hr>
                @include ('layouts._messages')

                @foreach ($answers as $answer)
                    <div class="media">
                        <div class="d-fex flex-column vote-controls">
                            <a title="This answer is useful" class="vote-up">
                                <i class="fas fa-caret-up fa-3x"></i>
                            </a>
                            <span class="votes-count">1230</span>
                            <a title="This answer is not useful" class="vote-down off">
                                <i class="fas fa-caret-down fa-3x"></i>
                            </a>
                            @can ('accept', $answer)
                                <a title="Mark this answer as best answer" 
                                    class="{{ $answer->status }} mt-2"
                                    onclick="event.preventDefault(); document.getElementById('answer-{{ $answer->id }}').submit();"
                                    >
                                    <i class="fas fa-check fa-2x"></i>                                    
                                </a>
                                <form id="answer-{{ $answer->id }}" action="{{ route('answers.accept', ['answer' => $answer->id]) }}" method="POST" style="display:none;">
                                    @csrf
                                </form>
                            @else
                                @if ($answer->is_best)
                                    <a title="The question owner accepted this answer as best answer" 
                                        class="{{ $answer->status }} mt-2"                                        
                                        >
                                        <i class="fas fa-check fa-2x"></i>                                    
                                    </a>
                                @endif
                            @endcan
                        </div>
                        <div class="media-body">
                            {!! $answer->body_html !!}
                            <div class="row">
                                <div class="col-4">
                                    <div class="ml-auto">
                                        @can ('update', $answer)
                                            <a href="{{ route('questions.answers.edit', ['question' => $question->id, 'answer' => $answer->id]) }}" class="btn btn-sm btn-outline-info">Edit</a>
                                        @endcan
                                        @can ('delete', $answer)
                                            <form class="form-delete" method="post" action="{{ route('questions.answers.destroy', [$question->id, $answer->id]) }}">
                                                @method('DELETE')
                                                @csrf
                                                <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure?')">Delete</button>
                                            </form>
                                        @endcan
                                    </div>
                                </div>
                                <div class="col-4"></div>
                                <div class="col-4">
                                    <span class="text-muted">Answered {{ $answer->created_date }}</span>
                                    <div class="media mt-2">
                                        <a href="{{ $answer->user->url }}" class="pr-2">
                                            <img src="{{ $answer->user->avatar }}">
                                        </a>
                                        <div class="media-body mt-1">
                                            <a href="{{ $answer->user->url }}">{{ $answer->user->name }}</a>
                                        </div>
                                    </div>
                                </div>
                            </div>                            
                        </div>
                    </div>
                    <hr>
                @endforeach
            </div>
        </div>
    </div>
</div>

一旦问题的作者点击复选图标,它应该将答案标记为最佳答案,但我收到以下错误:

抱歉,找不到您要查找的页面

我看到url中缺少答案id如下:

http://localhost:8000/answers//accept

什么时候应该是这样的:

http://localhost:8000/answers/1/accept

当我将它作为表单操作中的路由参数传递时,我似乎无法弄清楚为什么。如果用户试图编辑他的答案,也会发生同样的事情。

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    我可以建议在这里稍微调整一下您的策略并绕过提交表单的需要吗?

    如果您将 POST 替换为 GET 请求,这实际上非常简单。

    Answers.blade.php

    @foreach( $answers as $answer )
        @can('accept', $answer)
            <a href="answers/{{ $answer->id }}/accept" title="Mark this answer as best answer" class="{{ $answer->status }} mt-2">
                <i class="fas fa-check fa-2x"></i>
            </a>
        @else
            <p>Do your thing</p>
        @endcan 
    @endforeach
    

    routes.web.php

    Route::get('answers/{answer}/accept', 'AcceptAnswerController');
    

    【讨论】:

      【解决方案2】:

      因此,在更仔细地检查了我的代码之后,控制器、路由和视图的其他一切似乎都还不错。我能够将问题隔离到该区域

      @can ('accept', $answer)
       <a title="Mark this answer as best answer" 
         class="{{ $answer->status }} mt-2"
        onclick="event.preventDefault(); document.getElementById('answer-{{ $answer->id }}').submit();"
                                         >
          <i class="fas fa-check fa-2x"></i>                                    
       </a>
       <form id="answer-{{ $answer->id }}" action="{{ route('answers.accept', ['answer' => $answer->id]) }}" method="POST" style="display:none;">
           @csrf
       </form>
      @endcan
      

      我确信 $answer-&gt;id 应该返回正确的 id,但我不太确定 $answer-&gt;status 所以我决定检查它在 Answer 模型中定义的访问器。

      public function getStatusAttribute()
      {
         return $this->isBest() ? 'vote-accepted' : '';
      }
      
      public function isBest()
      {
         return $this->id = $this->question->best_answer_id; /** here is the problem **/
      }
      

      问题出在我身上。上面的 isBest 方法应该返回一个布尔值,但我错误地分配了。这是简单的解决方法。

      public function isBest()
      {
         return $this->id === $this->question->best_answer_id; /** here is the problem **/
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-22
        • 2019-11-01
        • 2017-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-19
        相关资源
        最近更新 更多