【问题标题】:MethodNotAllowedHttpException - Laravel 5.1MethodNotAllowedHttpException - Laravel 5.1
【发布时间】:2015-10-28 19:24:46
【问题描述】:

我在主页上有两个按钮 - ReaderWriter - 知道profession 的情况下将用户引导到注册表单。

Route::post('register', [
'as' => 'profession_path',
'uses' => 'ProfessionController@displayForm'
]);

Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

我平时去.com/auth/register的时候可以成功注册用户,所以表单效果很好。

职业控制者

class ProfessionController extends Controller
{

   public function displayForm()
    {
    $input = \Input::get();
    $profession = $input['profession'];
    return view('auth/register', ['profession' => $profession]);
    }
}  

当我单击按钮并重定向到.com/register 并识别$profession 时,它也可以成功运行。但是,当我单击注册表单上的提交按钮(通常位于 .com/auth/register 并在那里成功)时,它会引发错误:

RouteCollection.php 第 201 行中的 MethodNotAllowedHttpException:

我错过了什么?

【问题讨论】:

  • 您是否尝试为读者和作者使用相同的注册表单?读者和作者的注册有什么区别?
  • 是的,我在注册表中设置了一个隐藏字段$profession,所以客人不必再次选择它。但如果客人直接来到注册表单页面,它会显示单选按钮。登录后读写器的区别会有所不同。
  • 如何在控制器中调用该字段?
  • 我还没有。是这个原因吗?我应该将'profession' => 'required', 放入protected function validator(array $data)'profession' => $data['profession'], 放入protected function create(array $data) { return User::create([ 吗?
  • 好吧,我认为您应该从定义如何将用户保存到数据库中开始,所以也许它会有所帮助。

标签: php laravel laravel-5.1


【解决方案1】:

根据您的问题,我认为您的表格做错了。 在您的路线中,您正在获取和发布请求。

Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

通常表单提交是post方法,你必须在表单中指定

{{ Form::open(array('method' => 'post')) }}

{{ Form::open(array('method' => 'get')) }} 

如果你使用的是普通的html,那么

<form method="POST" action="http://currenturl" accept-charset="UTF-8">
<form method="GET" action="http://currenturl" accept-charset="UTF-8">

laravel 路由请求如下所示

Route::get() will respond to GET requests.
Route::post() will respond to POST requests.
Route::delete() will respond to DELETE requests (this includes when adding the custom DELETE
Route::put() will respond to PUT requests (this includes when adding the custom PUT
Route::patch() will respond to PATCH requests (this includes when adding the custom PATCH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-23
    • 2018-03-30
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 2019-04-15
    相关资源
    最近更新 更多