php Laravel 404 -页面未找到

6ojccjat  于 2023-04-28  发布在  PHP
关注(0)|答案(1)|浏览(138)

已关闭,此问题需要更focused。目前不接受答复。
**想改善这个问题吗?**更新问题,使其仅通过editing this post关注一个问题。

昨天关门了。
Improve this question
对不起,我是新的编码世界-我正在创建一个管理员用户,并完成管理后,我的评论部分,这是功能齐全的停止工作,每次我去发表评论,我得到的错误404没有找到,我知道与路由-我已经使用路由命令,试图解决这个问题,并没有效果-非常感谢你看我的问题!
photo.blade.php -评论区

<div>
                <form action="{{$photo->id}}/comments" method="POST" class="mb-0">
                  <input type="hidden" name="post_slug" value="{{$photo->id}}">
                   @csrf

                   <label class="mt-6 block text-sm font-medium text-gray-700">Comment</label>
                   <textarea name="comment_body" class="mt-1 py-2 px-3 block w-full borded border-gray-400 rounded-md shadow-sm" required>{{old('comment_body')}}</textarea>

                    <button
                     class="bg-laravel text-white rounded py-2 px-4 mt-6 hover:bg-black"
                     >
                     Post Comment
                      </button>
                  </form>
              </div>

注解控制器

<?php

namespace App\Http\Controllers;

use App\Models\Photos;
use App\Models\Comment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\RedirectResponse;
use App\Http\Requests\CommentsRequest;
use Illuminate\Support\Facades\Validator;

class CommentsController extends Controller
{
    //Store Comments
    public function store(Request $request)
    {
        if(Auth::check())
        {
            $validator = Validator::make($request->all(), [
                'comment_body' => 'required|string'
            ]);

            if($validator->fails())
            {
                return redirect()->back()->with('message', 'Comment area is mandatory');
            }

            $photo = Photos::where('id', $request->post_slug)->first();
            if($photo)
            {
                Comment::create([
                    'photos_id' => $photo->id,
                    'user_id' => Auth::user()->id,
                    'comment_body' => $request->comment_body
                ]);

                return redirect()->back()->with('message', 'Comment successfully posted!');
            }
            else
            {
               return redirect()->back()->with('message', 'No Post Found!');
            }
        }
        else
        {
            return redirect('login')->with('message', 'Login first to log in!');
        }
    }

    //Edit form
    public function edit(Comment $comment) {
        return view('edit-comments', ['comments' => $comment]);
    }

    //update comment
    public function update(Request $request, Comment $comment) {
        $formFields = $request->validate([
            'comment_body' => 'required'
        ]);

        $comment->update($formFields);

        return redirect()->back()->with('message', 'Post updated successfully!');
    }

    //Delete Listing
    public function destroy(Comment $comment){
        $comment->delete();
        return redirect()->back()->with('message', 'Comment deleted successfully!');
    }
}

评论路由

//Comments route
Route::post('/comments/{comment}',
[CommentsController::class, 'store']);

//Delete Comments
Route::delete('/comments/{comment}',
[CommentsController::class, 'destroy'])->name('comments.destroy')->middleware('auth');

//Edit form - Comments
Route::get('/comments/{comment}/edit',
[CommentsController::class, 'edit'])->middleware('auth');

//Update comments
Route::put('/comments/{comment}',
[CommentsController::class, 'update'])->middleware('auth');

如果有任何其他代码需要随时评论下的职位再次-非常感谢!

xxe27gdn

xxe27gdn1#

根据您提供的代码,似乎问题可能与您的注解路径有关。您正在为CommentsController中的“store”和“destroy”方法使用相同的URL参数“comment”。这可能会导致与路由冲突,从而导致404错误。
要解决此问题,您可以将“store”方法的参数名称更改为其他名称,例如“photo”,如下所示:

Route::post('/photos/{photo}/comments', [CommentsController::class, 'store']);

然后,您需要更新您的评论区表单,以使用正确的URL发布评论:

<form action="/photos/{{$photo->id}}/comments" method="POST" class="mb-0">

实际上,您已经有了/comments/{comment.id}的路由,但是您的表单正在向{{$photo->id}}/comments发送请求。将您的呼叫与路由对齐,所有功能都将按预期工作。

相关问题