【问题标题】:Laravel NotFoundHttpException although route exitsLaravel NotFoundHttpException 虽然路由退出
【发布时间】:2017-11-15 00:08:14
【问题描述】:

我添加了一条新路线:

Route::post('friendSend/{uid}','FriendController@sendFriendRequest')->name('friends.add');

并将其称为提交表单的超链接:

<a href="{{route('friends.add',$user->uid)}}"
                      onclick="event.preventDefault();
                               document.getElementById('addfriend-form).submit();">
                         <i class="glyphicon glyphicon-facetime-video" style="color:#F44336;"></i> Add Friend
</a>

<form action="{{route('friends.add',$user->uid)}}" method="post" id="addfriend-form">
                 {{ csrf_field() }}
</form>

但是,当我单击上述链接时,我会被重定向到 /friendSend 并出现上述错误。

路线可见于:

php artisan route:list

这是有道理的,因为我通过它的名字“friends.add”来称呼它。它甚至不去控制器。

我已经尝试过以下方法:

Laravel NotFoundHttpException although route exists

控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Friend;
use Auth;

class FriendController extends Controller
{
 public function __construct()
 {
   $this->middleware('auth');
 }

 public function index()
 {
    return redirect()->route('home');
 }

 public function sendFriendRequest($id)
 {
   echo "hello world";
 }
}

更新:

手动输入网址为 /friendSend/2 (或任何数字)有效。

【问题讨论】:

  • 控制器代码?
  • 您在控制器中使用 findOrFail() 吗?如果是这样会引发 404,请发布您的控制器。
  • idk 为什么这很重要,但要调试它,FriendController@sendFriendRequest 只包含一个 echo 语句。
  • 添加了控制器。
  • 所以如果我手动执行此操作,我不会收到错误friendSend/2

标签: laravel laravel-5


【解决方案1】:

您在onclick javascript 中缺少'

<a href="{{route('friends.add',$user->uid)}}"
                      onclick="event.preventDefault();
                               document.getElementById('addfriend-form').submit();">
                         <i class="glyphicon glyphicon-facetime-video" style="color:#F44336;"></i> Add Friend
</a>

<form action="{{route('friends.add',$user->uid)}}" method="post" id="addfriend-form">
                 {{ csrf_field() }}
</form>

如果这不起作用,生成页面上的 href 是什么?您是否在一个页面上有多个这样的表单?

【讨论】:

  • 啊,这可能是在发布问题时发生的,' 不是代码中的问题,不,我在一个页面上没有多个这些表单。
  • @Siddhant Blade 生成 url 后页面上的 href 是什么?
【解决方案2】:

我认为你在表单提交中设置 url 时做错了。你正在添加类似

的路由
href="{{route('friends.add',$user->uid)}}"

只是修改为

href="{{route('friends.add',['uid' => $user->uid])}}"

您可以参考Laravel Named Routes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 2017-08-10
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多