我是laravel的新手,我正在尝试使用用户模型和控制器执行singup =>在提交表单后发帖我希望将用户信息存储到用户表中,但我不断从Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
收到此错误\laravel\framework\src\Illuminate\Routing\RouteCollection.php
我不确定我搞砸了哪个部分,任何建议都会有所帮助。谢谢。 :
// ==== route
Route::get('/signup', 'UserController@getSignup');
Route::get('/login', 'UserController@getLogin' );
Route::resource('user', 'UserController');
// ====模特
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
}
// ====用户迁移表
public function up() {
Schema::create('users', function($table) {
$table->increments('id');
$table->string('email')->unique();
$table->boolean('remember_token');
$table->string('password');
$table->timestamps();
});
}
// ==== UserController中
<?php
class UserController extends BaseController {
public function __construct() {
$this->beforeFilter('guest', array('only' => array('getLogin','getSignup')));
}
public function getSignup() {
return View::make('user_signup');
}
public function postSignup() {
# Step 1) Define the rules
$rules = array(
'email' => 'required|email|unique:users,email',
'password' => 'required|min:6'
);
# Step 2)
$validator = Validator::make(Input::all(), $rules);
# Step 3
if($validator->fails()) {
return Redirect::to('/signup')
->with('flash_message', 'Sign up failed; please fix the errors listed below.')
->withInput()
->withErrors($validator);
}
$user = new User;
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
try {
$user->save();
}
catch (Exception $e) {
return Redirect::to('/signup')
->with('flash_message', 'Sign up failed; please try again.')
->withInput();
}
# Log in
Auth::login($user);
return Redirect::to('/list')->with('flash_message', 'Welcome to Foobooks!');
}
public function getLogin() {
return View::make('user_login');
}
public function postLogin() {
$credentials = Input::only('email', 'password');
if (Auth::attempt($credentials, $remember = true)) {
return Redirect::intended('/')->with('flash_message', 'Welcome Back!');
}
else {
return Redirect::to('/login')
->with('flash_message', 'Log in failed; please try again.')
->withInput();
}
return Redirect::to('login');
}
public function getLogout() {
# Log out
Auth::logout();
# Send them to the homepage
return Redirect::to('/');
}
}
// ===== user_signup.blade.php
@extends('_master')
@section('title')
Sign up
@stop
@section('content')
<h1>Sign up</h1>
@foreach($errors->all() as $message)
<div class='error'>{{ $message }}</div>
@endforeach
{{ Form::open(array('url' => '/signup')) }}
Email<br>
{{ Form::text('email') }}<br><br>
Password:<br>
{{ Form::password('password') }}<br>
<small>Min: 6</small><br><br>
{{ Form::submit('Submit') }}
{{ Form::close() }}
@stop
答案 0 :(得分:1)
'MethodNotAllowedHttpException'表示正在命中的路由未设置为已发送的http类型。
因此对于/ signup,您允许GET请求,但您的表单正在使用POST向其发送数据。创建一个接受POST的路由以使用相关的控制器和方法。
路由:: 发布('/ signup','UserController @ postSignup');
编辑:
啊,我认为可能是导致问题的Route :: resource('user','UserController')。 Laravel Docs说:: resource('user','UserController')应该在你的控制器中有宁静的动作,如UserController @ index,UserController @ create,UserController @ store等。
您的控制器可以使用'php artisan controller:make UserController'自动填充相关操作注意:这可能会删除您当前拥有的内容
如果你没有使用Route :: resource来处理请求,请继续删除它。