Laravel帖子请求不起作用

时间:2017-11-15 09:17:59

标签: laravel

我在Laravel中有Form,当我提交表单以重定向具有输入值的另一个页面(“action = panel”)时。但问题是,当我进入另一个链接时,它会显示错误。怎么了?

这是表格

enter image description here

this is another page when submit form

this is error when i enter in link again

这是表单代码:

<form action="{{route('adminPanel')}}" class="form" method="POST">
    <p>Name:</p>
    <input type="text" name="name"><br>
    <p>Password:</p>
    <input type="password" name="password"><br>
    <input type="submit" name="submit" value="Enter As Admin" class="submit">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

这是路线:

Route::get('/', [

    'uses' => 'AdminController@getAdminIndex',

    'as' => 'index.admin'
]);
Route::post('/panel', [
    'uses' => 'AdminController@getAdminPanel',
    'as' => 'adminPanel'

]);

这是控制器:

class AdminController extends Controller
{
    public function getAdminIndex(){
        return view('admin/index');
    }
    public function getAdminPanel(Request $request){
        return view('admin/admin', ['name' => $request->name]);
    }
}

3 个答案:

答案 0 :(得分:1)

这是因为当您在地址栏中输入地址时,您实际上是在发送get请求。但是您已使用post方法定义了您的路线! 要解决此问题,您可以使用any

Route::any('/panel', [
    'uses' => 'AdminController@getAdminPanel',
    'as' => 'adminPanel'

]);

并在控制器中:

use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{
    public function getAdminIndex(){
        return view('admin/index');
    }
    public function getAdminPanel(Request $request){
       $name = $request->name ?: Auth::user()->name; 
       return view('admin/admin', ['name' => $name]);
    }
}

答案 1 :(得分:0)

尝试在表单中使用以下语句{{csrf_field()}}

答案 2 :(得分:0)

有时候路线可能会给您带来麻烦。请尝试以下代码段。

<form action="{{route('adminPanel')}}" class="form" method="POST">
  <p>Name:</p>
  <input type="text" name="name"><br>

  <p>Password:</p>
  <input type="password" name="password"><br>

  <input type="submit" name="submit" value="Enter As Admin" class="submit">
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

如下所示纠正您的路线并尝试。

Route::post('/adminPanel', ['uses' => 'AdminController@getAdminPanel', 'as' => 'adminPanel' ]);