duanping2809 2014-08-07 12:02
浏览 72
已采纳

userController和post方法在laravel中没有任何工作

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
  • 写回答

1条回答 默认 最新

  • dongluo1853 2014-08-07 12:13
    关注

    '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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号