Reputation: 5173
I am new to laravel and I am trying to use user model and controller doing singup => post after submitting the form I would like to get user info to store into users table, but I kept getting this error Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
from \laravel\framework\src\Illuminate\Routing\RouteCollection.php
I am not sure what part I messed up, any suggestion would help. Thank you. :
// ====route
Route::get('/signup', 'UserController@getSignup');
Route::get('/login', 'UserController@getLogin' );
Route::resource('user', 'UserController');
// ====model
<?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');
}
//====user migration table
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
Upvotes: 0
Views: 1381
Reputation: 38
'MethodNotAllowedHttpException' means that the route that is being hit isnt set up for the http type that has been sent.
So for /signup, you allow a GET request but your form is using POST to send data to it. Make a route that accepts POST to use the relevant controller and method.
Route::post('/signup', 'UserController@postSignup');
Edit:
Ah, I think it may be Route::resource('user', 'UserController') that's causing the problem. Laravel Docs says that ::resource('user', 'UserController') should have restful actions in your controller like UserController@index, UserController@create, UserController@store, etc.
Your controller can be autopopulated with the relevant actions using 'php artisan controller:make UserController' Note: this may delete what you currently have in place
If you're not utilising Route::resource to handle requests though, go ahead and remove it.
Upvotes: 1