我如何使用laravel中的按钮进行正确的路线

时间:2019-10-14 05:36:57

标签: php laravel

我有show.blade.php,并且有按钮共享可转到share.blade.php

这是我的show.blade.php

@extends('layouts.app')
@section('content')

<div class="container">
    <form method="post" action="/student/share-approval/{{$approval->uniid}}">
     @csrf
      <p> Hello  Trst show page , If you click share butoon should show the share page</p><br>
        <button type="submit" class="btn btn-primary"> share </button>
     </body>
    </form>
  </div>
@endsection

这是我的share.plade.php

@extends('layouts.app')
@section('content')

 <div class="container">
    <form method="post" action="/sendemail">
      @csrf
      <p> Share page</p>
     <button type="submit" class="btn btn-primary">send </button><br>
  </form>
</div>
@endsection

这是我在网上的路线

Route::post('/student/share','StudentController@shareapproval');
Route::get('/student/show-approval/{uniid}','StudentController@showapproval');
Route::get('/student/share-approval/{uniid}', 'StudentController@shareapproval');

这是我在StudentController中的功能

 public function showapproval($uniid)
{
  $approval = Student :: where ('uniid', $uniid)->firstOrFail();
   return view('ApprovalStudent.Request.show',compact('approval'));
}
   public function shareapproval($uniid)
   {
   $approval = Student :: where ('uniid', $uniid)->firstOrFail();
   return view('SendEmail.Request.share',compact('approval'));
   }

错误是(Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 没有消息)

2 个答案:

答案 0 :(得分:0)

将方法更改为post

  Route::post('/student/share','StudentController@shareapproval');
  Route::get('/student/show-approval/{uniid}','StudentController@showapproval');
  Route::post('/student/share-approval/{uniid}', 'StudentController@shareapproval');

答案 1 :(得分:0)

方法是以这种形式发布的方式

<form method="post" action="/student/share-approval/{{$approval->uniid}}">

将您的路线设置为 POST

Route::post('/student/show-approval/{uniid}','StudentController@showapproval');

并在控制器中以

捕获请求

public function showapproval(Request $request,$uniid)
{

  $approval = Student :: where ('uniid', $uniid)->firstOrFail();
   return view('ApprovalStudent.Request.show',compact('approval'));
}

并且作为习惯,在操作中使用 url()

<form action="{{url('/')}}/student/share-approval/{{$approval->uniid}}">