【问题标题】:NotFoundHttpException when updating AuthController更新 AuthController 时出现 NotFoundHttpException
【发布时间】:2016-01-15 01:12:36
【问题描述】:

访问POST:http://localhost:8000/auth/register 时,我得到了NotFoundHttpException。当我更新 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>

【问题讨论】:

  • 你能把php artisan route:list命令的输出贴出来
  • @HamzaOuaghad pastebin.com/Xq24AQLK
  • 您是通过表单还是通过什么方式访问/auth/register/?
  • @HamzaOuaghad 不通过休息客户端,它是一个 API
  • 好的,当你得到这个错误时,上面的链接,是/auth/register/还是别的什么?根据跟踪,您实际上从未去过auth/controller,因此您的routes.php 很可能有问题。或者,就像我的问题一样,我怀疑你是否真的成功地执行了postRegister 并被重定向到默认的重定向路径,但你没有在你的 routes.php 中指定它,所以你得到了这个错误。试试这个,所以我们可以找到:在您的routes.php 中为/home 指定一条路线,该路线只是回声打招呼。

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


【解决方案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());
}

我们可以看到,注册后,该方法重定向到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';
}

我们可以看到,如果没有指定'redirectTo'属性,该方法默认重定向到'/home',所以你有两种可能的解决方案:

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

在AuthController.php

    class AuthController extends Controller
    {
        use AuthenticatesAndRegistersUsers, ThrottlesLogins;
        protected $redirectPath = '/myspecifiedroute';
        ...
        ...

【讨论】:

  • 关于unique:users,到目前为止我没有处理任何有用的信息。也许我会建议检查您的数据库驱动程序版本。
【解决方案2】:

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

class AuthController extends Controller {

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

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

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

Further Reading

【讨论】:

  • 公共 $redirectTo = '';就我而言,它是一个 API
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2016-03-20
    • 2014-10-17
    • 2016-03-31
    • 2016-06-17
    • 2016-06-28
    • 2016-11-20
    • 2016-10-31
    • 2020-01-26
    • 2015-11-09
    相关资源
    最近更新 更多