PHP表单中的Laravel csrf令牌

PHP表单中的Laravel csrf令牌,php,csrf,laravel-5.4,Php,Csrf,Laravel 5.4,我创建了一个用于接受好友请求的小助手函数。此函数位于PHP文件中(显然),如下所示: (仅适用于相关部分) foreach($friendrequests作为$request){ $username=DB::table('users')->where('id',$request->sender_id)->value('name'); $notify.='li>'; $notify.='möchte dein Freund sein'; $notify.='Akzeptieren'; $notif

我创建了一个用于接受好友请求的小助手函数。此函数位于PHP文件中(显然),如下所示:

(仅适用于相关部分)

foreach($friendrequests作为$request){
$username=DB::table('users')->where('id',$request->sender_id)->value('name');
$notify.='li>';
$notify.='
möchte dein Freund sein'; $notify.='Akzeptieren'; $notify.='Ablehnen'; $notify.=''; }
我知道有点乱。我对拉雷维尔还不太熟悉

总之,有两种形式。一个用于接受请求,一个用于拒绝请求。现在我正在努力解决的是csrf令牌


如何在PHP帮助文件中实现这一点?我知道如何在刀片模板中使用它们,但我似乎无法在帮助函数中使用它们。

尝试向代码中添加
\u token
隐藏元素,如下所示。您还可以使用
csrf\u token()
在表单中添加表单令牌

foreach($friendrequests as $request){
        $username = DB::table('users')->where('id', $request->sender_id)->value('name');
        $notify .= '<li>';
        $notify .= '<strong><a href="/profile/'.$username.'">'.$username.'</a></strong><br>möchte dein Freund sein';
        $notify .= '<form action="/friend/request/accept/'.$request->sender_id.'" method="post"><input type="hidden" name="_token" value="'.Session::token().'"><button type="submit">Akzeptieren</button></form>';
        $notify .= '<form action="/friend/request/deny/'.$request->sender_id.'" method="post"><input type="hidden" name="_token" value="'.Session::token().'"><button type="submit">Ablehnen</button></form>';
        $notify .= '</li>';
    }
foreach($friendrequests作为$request){
$username=DB::table('users')->where('id',$request->sender_id)->value('name');
$notify.='li>';
$notify.='
möchte dein Freund sein'; $notify.='Akzeptieren'; $notify.='Ablehnen'; $notify.=''; }
尝试将
\u标记
隐藏元素添加到代码中,如下所示。您还可以使用
csrf\u token()
在表单中添加表单令牌

foreach($friendrequests as $request){
        $username = DB::table('users')->where('id', $request->sender_id)->value('name');
        $notify .= '<li>';
        $notify .= '<strong><a href="/profile/'.$username.'">'.$username.'</a></strong><br>möchte dein Freund sein';
        $notify .= '<form action="/friend/request/accept/'.$request->sender_id.'" method="post"><input type="hidden" name="_token" value="'.Session::token().'"><button type="submit">Akzeptieren</button></form>';
        $notify .= '<form action="/friend/request/deny/'.$request->sender_id.'" method="post"><input type="hidden" name="_token" value="'.Session::token().'"><button type="submit">Ablehnen</button></form>';
        $notify .= '</li>';
    }
foreach($friendrequests作为$request){
$username=DB::table('users')->where('id',$request->sender_id)->value('name');
$notify.='li>';
$notify.='
möchte dein Freund sein'; $notify.='Akzeptieren'; $notify.='Ablehnen'; $notify.=''; }
您已经添加了字段,但需要将
csrf\u标记()
值连接到字符串中。现在,它将以文本形式打印
csrf\u令牌
作为值

试试这个:

$notify .= '<form action="/friend/request/accept/'.$request->sender_id.'" method="post"><input type="hidden" name="_token" value="' . csrf_token() . '"><button type="submit">Akzeptieren</button></form>';
$notify.='Akzeptieren';

另外,
csrf\u-field()
函数将带令牌值的输入字段回送到当前请求,
csrf\u-token()
将仅显示令牌值。

您已经添加了字段,但需要将
csrf\u-token()
值连接到字符串中。现在,它将以文本形式打印
csrf\u令牌
作为值

试试这个:

$notify .= '<form action="/friend/request/accept/'.$request->sender_id.'" method="post"><input type="hidden" name="_token" value="' . csrf_token() . '"><button type="submit">Akzeptieren</button></form>';
$notify.='Akzeptieren';
另外,
csrf\u-field()
函数将向当前请求回显带有令牌值的输入字段,
csrf\u-token()
将仅显示令牌值