更新AuthController时出现NotFoundHttpException

时间:2015-10-16 10:08:55

标签: laravel authentication laravel-5 laravel-routing php-5.6

访问NotFoundHttpException时收到了POST:http://localhost:8000/auth/register。更新AuthController验证器和创建方法时出现此错误。使用默认的AuthController,没有错误,但数据没有存储在数据库中

AuthController

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }
    protected function create(array $data)
    {
        return User::create([
            'lastname' => $data['lastname'],
            'firstname' => $data['firstname'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'firstname' => 'required|max:255',
            'lastname' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:8',
        ]);
    }
}

路线

Route::post('auth/register', 'Auth\AuthController@postRegister');

用户模型

namespace App;

use Illuminate\Auth\Authenticatable;
use Jenssegers\Mongodb\Model as Eloquent;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Eloquent implements AuthenticatableContract,
                                AuthorizableContract,
                                CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
    protected $collection = 'users_collection';
    protected $fillable = ['firstname', 'lastname', 'email', 'password'];
    protected $hidden = ['password', 'remember_token'];

    public function websites(){
        return $this->hasMany('App\Website');
    }
}

路线列表 http://pastebin.com/Xq24AQLK

的.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

3 个答案:

答案 0 :(得分:1)

我的AuthController中有一个$redirectTo属性,在成功登录后会覆盖默认重定向:

class AuthController extends Controller {

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    public $redirectTo = '/';
    protected $loginPath = '/account/login';
    ...

我不确定它是$ redirectTo还是$ redirectPath(至少根据文档,我在我的项目中找不到这个)

Further Reading

答案 1 :(得分:1)

正如我在那里的评论中提到的那样,并且根据你的评论提到用户实际上已经在数据库中注册,所以为了调试问题,让我们来看看{{1的源代码方法:

postRegister

正如我们所看到的,在注册后,该方法重定向到 /** * Handle a registration request for the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function postRegister(Request $request) { $validator = $this->validator($request->all()); if ($validator->fails()) { $this->throwValidationException( $request, $validator ); } Auth::login($this->create($request->all())); return redirect($this->redirectPath()); } 返回的redirction路径,让我们看看它的源代码:

redirectPath

我们可以看到,如果未指定/** * Get the post register / login redirect path. * * @return string */ public function redirectPath() { if (property_exists($this, 'redirectPath')) { return $this->redirectPath; } return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home'; } 属性,默认情况下该方法会重定向到&#39; / home&#39;,因此您有两种可能的解决方案:

  1. 'redirectTo'指定路线并快速完成
  2. /home中指定'redirectTo'redirectPath属性,如下所示:
  3. AuthController

    AuthController.php

答案 2 :(得分:0)

@ hamza-ouaghad回答是我的问题的正确答案。但是我遇到了另一个问题:我的数据没有存储在我的数据库中。使用laravel-mongodb并且您需要在数据模型中使用唯一字段(如电子邮件)时,您必须创建以下迁移:

Schema::create('users_collection', function($collection) {
    $collection->unique('email');
});