Laravel NotFoundHttpException虽然路由退出

时间:2017-06-13 09:36:12

标签: laravel laravel-5

我添加了一条新路线:

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

这是有道理的,因为我通过它的名字&#39; friends.add&#39;来称呼它。它甚至没有去控制器。

我已经尝试了以下内容:

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";
 }
}

更新:

手动输入url / friendSend / 2(或任何数字)可以正常工作。

2 个答案:

答案 0 :(得分:0)

您在' javascript中缺少onclick

<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是什么?您是否在一个页面上有多个这些表单?

答案 1 :(得分:0)

我在设置提交表单中的网址时出错了。您正在添加

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

将其修改为

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

您可以参考Laravel Named Routes