In Laravel5.2, how to directly use the method of the Auth package directly registered users?

Source: Internet
Author: User
Keywords Php laravel
In some scenarios, I do not need to use the table forms submit data for user registration, I need to register directly in the controller, I call the following method.

\Illuminate\Foundation\Auth\RegistersUsers::postRegister($data);

I call this method directly, will prompt the error.

Non-static method Illuminate\Foundation\Auth\RegistersUsers::postRegister() should not be called statically, assuming $this from incompatible context

Actually, what I want to know is. Laravel provides a way to verify that a user is logged on

Auth::attempt(['email' => $email, 'password' => $password])

Is there a similar method for me to register as a member directly after I have passed in the user information?

Reply content:

In some scenarios, I do not need to use the table forms submit data for user registration, I need to register directly in the controller, I call the following method.

\Illuminate\Foundation\Auth\RegistersUsers::postRegister($data);

I call this method directly, will prompt the error.

Non-static method Illuminate\Foundation\Auth\RegistersUsers::postRegister() should not be called statically, assuming $this from incompatible context

Actually, what I want to know is. Laravel provides a way to verify that a user is logged on

Auth::attempt(['email' => $email, 'password' => $password])

Is there a similar method for me to register as a member directly after I have passed in the user information?

First of all, the object you call the method is not a class, it is an attribute (trait) and cannot be called directly to see the source code.
Second, the method you are calling is not a static method (static) and cannot be called (::).
Finally, Laravel is the user registration by the controller Authcontroller generated by the PHP artisan Make:auth command, and the following is a method for registering users from the controller.
(ty0716 's simple answer also tells you that registering a user in a controller is a way to save user information to a database, so you might think more.) In addition, data validation validation needs to be done yourself. )

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

Laravel 5.2 In the make:auth generated is a common method, the specific needs to adjust according to their own circumstances. In general, it cannot be used directly because the database structure is different. Suggest writing your own method.

User::create(array(    'name'     => 'your name',    'username' => 'your_username',    'email'    => 'name@domain.io',    'password' => Hash::make('your_password'),));

Add Route::auth () before the route that requires login;

You see
Route::auth();
文件路径 vendor/laravel/framework/src/Illuminate/Routing/Router.php 第346行
It is not necessary to specify the login in routes.php, the registered route

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.