简体   繁体   English

如何解决此错误,我收到“抱歉,找不到您要查找的页面。”

[英]How do i fix this error i'm getting “Sorry, the page you are looking for could not be found.”

I have this application where I am trying to get the the author of a question to be able to mark an answer as the best answer.我有这个应用程序,我试图让问题的作者能够将答案标记为最佳答案。

I have an AcceptAnswerController created which has been registered in the routes/web.php file as below:我创建了一个AcceptAnswerController ,它已在routes/web.php文件中注册,如下所示:

AcceptAnswerController.php AcceptAnswerController.php

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

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

web.php 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');

In my Answers view, i have the following:在我的答案视图中,我有以下内容:

<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>

Once the author of the question clicks on the check icon, it should mark the answer as the best answer but i get the following error:一旦问题的作者单击复选图标,它应该将答案标记为最佳答案,但我收到以下错误:

Sorry, the page you are looking for could not be found抱歉,找不到您要查找的页面

I see that the answer id is missing in the url as follows:我看到 url 中缺少答案 ID,如下所示:

http://localhost:8000/answers//accept http://localhost:8000/answers//接受

when it should actually be something like this:当它实际上应该是这样的:

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

I can't seem to figure out why as i passed it as a route parameter in the form action.我似乎无法弄清楚为什么当我将它作为表单操作中的路由参数传递时。 Same thing happens if a user is trying to edit his answer.如果用户试图编辑他的答案,也会发生同样的事情。

Can I suggest tweaking your strategy a little bit here and bypassing the need for a form submission?我可以建议在这里稍微调整一下您的策略并绕过提交表单的需要吗?

If you swap out the POST for a GET request, this is actually pretty straight forward.如果您将 POST 换成 GET 请求,这实际上非常简单。

Answers.blade.php 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路线.web.php

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

So, after scrutinizing my code a bit more carefully everything else seemed okay the controller, routes, views.因此,在仔细检查了我的代码之后,controller、路线、视图等一切似乎都还不错。 I was able to isolate the problem down to this region我能够将问题隔离到该区域

@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

I was certian the $answer->id was supposed to return the correct id but i wasn't too sure about the $answer->status so I decided to check its accessor defined in the Answer model.我确信$answer->id应该返回正确的 id,但我不太确定$answer->status所以我决定检查它在 Answer model 中定义的访问器。

public function getStatusAttribute()
{
   return $this->isBest() ? 'vote-accepted' : '';
}

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

There the problem was staring right back at me.那里的问题正盯着我看。 The isBest method above was supposed to return a boolean value but i was mistakingly assigning.上面的 isBest 方法应该返回一个 boolean 值,但我错误地分配了。 This was the simple fix.这是简单的解决方法。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试重定向到laravel 5.6中的另一个页面时,出现类似“抱歉,找不到您要搜索的页面”的错误。 - when i am try to redirect to another page in laravel 5.6 then getting error like “Sorry, the page you are looking for could not be found.” 提交表单后,我收到错误消息“抱歉,找不到您要查找的页面。” - After submitting a form i get an error “Sorry, the page you are looking for could not be found.” 抱歉,找不到您要查找的页面。 silex错误 - Sorry, the page you are looking for could not be found. silex error 错误“抱歉,找不到您要查找的页面。” Laravel 5.5 - Error "Sorry, the page you are looking for could not be found." Laravel 5.5 当我用表格发布回复时,我得到“抱歉,找不到您要查找的页面。” - When I post a reply with a form i get “Sorry, the page you are looking for could not be found.” 抱歉,找不到您要查找的页面。 Laravel 5.5 - Sorry the page you're looking could not be found. Laravel 5.5 抱歉,找不到您要查找的页面。 RouteCollection.php第179行中的1/1 NotFoundHttpException: - Sorry, the page you are looking for could not be found. 1/1 NotFoundHttpException in RouteCollection.php line 179: 抱歉,找不到您要寻找的页面Laravel 5.5 - Random Sorry, the page you are looking for could not be found Laravel 5.5 抱歉,找不到您要查找的页面-Laravel 5.2 - Sorry, the page you are looking for could not be found - Laravel 5.2 我第一次使用 php 路由,我收到错误对不起! 网页未找到 - I am using php routing for the first time and I have been getting error Sorry! Page not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM