【问题标题】:Laravel 5 : Method Not Allowed Http Exception in Route Collection.php line 201:Laravel 5:Route Collection.php 第 201 行中的方法不允许 Http 异常:
【发布时间】:2015-10-19 01:28:38
【问题描述】:

我有一个注册表单并在routes.php 中获取注册表单值。 我的rout.php 代码是:

Route::get('/', function () {

    return view('login');
});

Route::get('/index', function(){
    return view('index');
});

Route::get('/register', function(){
    return view('register');
});

Route::post('/register',function(){
    $user = new \App\User;
    $user->username = input::get('username');
    $user->email  = input::get('email');
    $user->password = Hash::make(input::get('username'));
    $user->designation = input::get('designation');
    $user->save();

});

表单操作是 index.php 并且还有 csrf_token() 的隐藏字段:

<form action="index" method="post">         
<input type="hidden" name="_token" value="{{ csrf_token() }}">

错误是: Method Not Allowed Http Exception in Route Collection.php line 201:

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    您已经在 /register 页面上使用 post 方法正确注册了一条路由,但您在表单中向 index 路由发布了一个帖子。改变

       <form action="index" method="post">         
       <input type="hidden" name="_token" value="{{ csrf_token() }}">
    

       <form action="register" method="post">         
       <input type="hidden" name="_token" value="{{ csrf_token() }}">
    

    将 post 值发送到寄存器路由而不是索引路由。您不会在注册函数的末尾返回视图或重定向,因此我将添加 return view('index');return redirect('index'); 作为注册函数的最后一行,以将用户重定向到索引页面(或者只返回索引视图)

    或者,您可以更改索引路由以接受帖子值:

    Route::post('/index', function(){
        return view('index');
    });
    

    【讨论】:

    • 兄弟现在在 route.php 上说 Class 'input' not found
    猜你喜欢
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 2015-09-22
    • 2018-05-28
    • 2017-06-08
    • 2015-04-09
    • 1970-01-01
    • 2015-10-15
    相关资源
    最近更新 更多