【发布时间】:2014-08-07 20:52:27
【问题描述】:
我是 laravel 的新手,我正在尝试使用用户模型和控制器执行 singup => 在提交表单后我想将用户信息存储到用户表中,但我不断收到此错误 Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 来自\laravel\framework\src\Illuminate\Routing\RouteCollection.php我不确定我搞砸了哪一部分,任何建议都会有所帮助。谢谢你。 :
// ====路线
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();
});
}
//====用户控制器
<?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
【问题讨论】:
标签: php laravel laravel-4 routing