【发布时间】: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