Table of Contents
回复内容:
Home Backend Development PHP Tutorial 在Laravel5.2中,如何直接使用Auth包里的方法进行直接注册用户呢?

在Laravel5.2中,如何直接使用Auth包里的方法进行直接注册用户呢?

Jun 06, 2016 pm 08:13 PM
laravel php

有些场景下,我不需要使用表单来提交数据进行用户注册,我需要直接在控制器里进行注册,我调用下面的方法。

<code>\Illuminate\Foundation\Auth\RegistersUsers::postRegister($data);</code>
Copy after login
Copy after login

我直接调用这个方法,会提示错误。

<code>Non-static method Illuminate\Foundation\Auth\RegistersUsers::postRegister() should not be called statically, assuming $this from incompatible context</code>
Copy after login
Copy after login

其实我想知道的是。Laravel 提供了一个 方法用于验证用户登录

<code>Auth::attempt(['email' =&gt; $email, 'password' =&gt; $password])</code>
Copy after login
Copy after login

有没有类似的方法,我传入用户信息后直接注册成为会员呢?

回复内容:

有些场景下,我不需要使用表单来提交数据进行用户注册,我需要直接在控制器里进行注册,我调用下面的方法。

<code>\Illuminate\Foundation\Auth\RegistersUsers::postRegister($data);</code>
Copy after login
Copy after login

我直接调用这个方法,会提示错误。

<code>Non-static method Illuminate\Foundation\Auth\RegistersUsers::postRegister() should not be called statically, assuming $this from incompatible context</code>
Copy after login
Copy after login

其实我想知道的是。Laravel 提供了一个 方法用于验证用户登录

<code>Auth::attempt(['email' =&gt; $email, 'password' =&gt; $password])</code>
Copy after login
Copy after login

有没有类似的方法,我传入用户信息后直接注册成为会员呢?

首先,你调用方法的对象不是类(class),是特性(trait),不能直接调用,去看源代码。
其次,你调用的方法不是静态方法(static),不能如此(::)调用。
最后,Laravel是通过php artisan make:auth命令生成的控制器AuthController来进行用户注册的,以下是摘自控制器里注册用户的方法。
(ty0716简单的答案也同样告诉了你在控制器里注册用户其实就是通过User模型,将用户信息保存到数据库而已,所以你可能想多了。除此之外,数据验证Validation还需要你自己去做。)

<code>    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' =&gt; $data['name'],
            'email' =&gt; $data['email'],
            'password' =&gt; bcrypt($data['password']),
        ]);
    }
</code>
Copy after login

Laravel 5.2中自带的make:auth生成的是通用方法,具体的需要根据自己的情况调整。一般来说是不能直接用的,因为数据库结构不同。建议自己写方法。

<code>User::create(array(
    'name'     =&gt; 'your name',
    'username' =&gt; 'your_username',
    'email'    =&gt; 'name@domain.io',
    'password' =&gt; Hash::make('your_password'),
));</code>
Copy after login

在需要登录的路由前加 Route::auth();

你看下
Route::auth();
文件路径 vendor/laravel/framework/src/Illuminate/Routing/Router.php 第346行
是不用在routes.php 中指定登录,注册的路由的

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles