简体   繁体   English

如何在laravel nova中使用用户名而不是电子邮件登录?

[英]how to login using username instead of email in laravel nova?

In my existing laravel project, I handle logins with a username.在我现有的 Laravel 项目中,我使用用户名处理登录。

I've already replaced email with username on app\\Nova\\User.php我已经在 app\\Nova\\User.php 上用用户名替换了电子邮件

please help me how can I do login with username in laravel nova.请帮助我如何在 laravel nova 中使用用户名登录。 Thanks in advance.提前致谢。

After study about this issue, I've solved my own question.在研究了这个问题之后,我已经解决了我自己的问题。 here's the way I go.这是我要走的路。

app\\Provider\\NovaServiceProvider.php app\\Provider\\NovaServiceProvider.php

use Illuminate\Support\Facades\Route;

    //.....

    protected function routes()
    {
        $this->myLoginRoutes();
        Nova::routes()
                // ->withAuthenticationRoutes()
                ->withPasswordResetRoutes()
                ->register();        
    }

    /** ADD YOUR OWN LOGIN ROUTE */
    public function myLoginRoutes($middleware = ['web'])
    {
        Route::namespace('App\Nova\Http\Controllers\Auth')
            ->middleware($middleware)
            ->as('nova.')
            ->prefix(Nova::path())
            ->group(function () {
                Route::get('/login', 'LoginController@showLoginForm');
                Route::post('/login', 'LoginController@login')->name('login');
            });
    }

Copy复制
nova\\src\\resources\\views\\*
to
app\\resources\\views\\vendor\\nova\\*
and you can free to modify what you want in view.您可以随意修改您想要查看的内容。

Copy复制
nova\\src\\Http\\Controllers\\LoginController.php
to
app\\Nova\\Http\\Controllers\\Auth\\LoginController.php
and modify namespace to App\\Nova\\Http\\Controllers\\Auth;并将命名空间修改为 App\\Nova\\Http\\Controllers\\Auth;

app\\Nova\\Http\\Controllers\\Auth\\LoginController.php app\\Nova\\Http\\Controllers\\Auth\\LoginController.php

// ADD THIS METHOD TO OVERRIDE 
    public function username()
    {
        return 'username';
    }

assume you've changed email to username in users migration假设您已在用户迁移中将email更改为username

I realize this is an old post but I haven't found anything more recent and I figured I'd show my method of handling this situation.我意识到这是一篇旧帖子,但我没有找到任何更新的内容,我想我会展示我处理这种情况的方法。

I've created a new controller我创建了一个新的控制器

<?php

namespace App\Http\Controllers\Nova;

use Illuminate\Http\Request;

class LoginController extends \Laravel\Nova\Http\Controllers\LoginController
{
    public function authLogin(Request $request)
    {
        $request->request->add(['username' => $request->email]);

        return $this->login($request);
    }
    public function username()
    {
        return 'username';
    }
}

and create a new route to override the nova route并创建一个新路由来覆盖 nova 路由

Route::post('nova/login', 'Nova\LoginController@authLogin');

So you'll notice I extended the Nova controller.所以你会注意到我扩展了 Nova 控制器。 In our controller we'll add the username function that will return your 'username' field.在我们的控制器中,我们将添加用户名函数,该函数将返回您的“用户名”字段。
Now Nova will use the username field provided from your new function to login.现在 Nova 将使用您的新功能提供的用户名字段进行登录。

BUT...... it's not that simple buddy boy. BUT……不是那么简单的哥们儿。

Now Nova thinks the form is going to pass your username field as 'username' provided by your new username function.现在 Nova 认为表单会将您的用户名字段作为由您的新用户名函数提供的“用户名”传递。 So let's create a function called authLogin in our new controller.因此,让我们在新控制器中创建一个名为 authLogin 的函数。 We'll grab the current form field 'email' and append a new 'username' field to our request.我们将获取当前表单字段“email”并将新的“用户名”字段附加到我们的请求中。 Now pass the request off to the login function provided by Nova with the new field appended to the request and let Nova handle the rest.现在将请求传递给 Nova 提供的登录函数,并将新字段附加到请求中,让 Nova 处理其余的工作。

I'm not totally sure this method is a 'correct' way but it prevents from overriding core files or relying on copied core functions.我不完全确定这种方法是一种“正确”的方法,但它可以防止覆盖核心文件或依赖复制的核心功能。

for me it helped to use the loadViewsFrom method in the boot function of the NovaServiceProvider对我来说,它有助于在 NovaServiceProvider 的启动函数中使用 loadViewsFrom 方法

$this->loadViewsFrom(__DIR__.'/../resources/views/vendor/nova', 'nova');

Just copy the templates needed to app/resources/views/vendor/nova/...只需将所需的模板复制到 app/resources/views/vendor/nova/...

See also: https://laravel.com/docs/5.6/packages#views另见: https : //laravel.com/docs/5.6/packages#views

This change in addition to the change in the user model should do most of the trick除了用户模型的更改之外,此更改应该可以解决大部分问题

/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
   return 'username';
}

regards,问候,

Andreas安德烈亚斯

Add this into your将此添加到您的

novaFolder\src\Http\Controllers

-- ——

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function username()
{
    return 'username';
}

This should do it, don't forget to add username column field into your table这应该这样做,不要忘记将用户名列字段添加到您的表中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM