【问题标题】:userController and post method nothing working in laraveluserController 和 post 方法在 laravel 中不起作用
【发布时间】: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


    【解决方案1】:

    'MethodNotAllowedHttpException'表示被命中的路由没有为已经发送的http类型设置。

    因此,对于 /signup,您允许 GET 请求,但您的表单使用 POST 向其发送数据。创建一个接受 POST 的路由以使用相关的控制器和方法。

    Route::post('/signup', 'UserController@postSignup');

    编辑:

    啊,我认为可能是 Route::resource('user', 'UserController') 导致了问题。 Laravel Docs 表示 ::resource('user', 'UserController') 应该在您的控制器中具有 restful 操作,例如 UserController@index、UserController@create、UserController@store 等。

    您的控制器可以使用 'php artisan controller:make UserController' 自动填充相关操作 注意:这可能会删除您当前拥有的内容

    如果您没有使用 Route::resource 来处理请求,请继续删除它。

    【讨论】:

    • 在我添加 Route::post('/signup', 'UserController@postSignup');我仍然得到同样的错误...Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
    • 之前你说你得到'MethodNotAllowedHttpException',现在变成'NotFoundHttpException'了吗?完整的错误是什么?
    • 啊,我认为可能是 Route::resource('user', 'UserController') 导致了问题。 laravel.com/docs/controllers#restful-controllers 表示 ::resource('user', 'UserController') 应该在您的控制器中具有安静的操作,例如 UserController@index、UserController@create、UserController@store 等。您的控制器可以使用 ' 自动填充相关操作php artisan controller:make UserController' 注意:这可能会删除您当前拥有的内容如果您没有使用 Route::resource 来处理请求,请继续删除它。
    • 我已经删除了 restful userController,它现在可以工作了。谢谢。
    猜你喜欢
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 2019-02-20
    • 2016-09-04
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多