Laravel中如何在同一/register URL下实现用户注册功能?
Hey there! No need to use resource controllers to make this work—we can set up separate HTTP method routes for the same /register URL, and tweak your controller and form to match. Let's break this down step by step:
1. Adjust Your Routes (web.php)
Laravel lets you define routes for different HTTP methods on the same URL. We'll keep the GET route for showing the registration form, and add a POST route that points to your create() method to handle form submissions:
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); // Show register form (responds to GET requests) Route::get('register', 'Authentication\RegisterController@index'); // Handle register form submission (responds to POST requests) Route::post('register', 'Authentication\RegisterController@create'); Route::get('/login', function() { return view('auth.login'); })->name('login');
2. Fix Up the RegisterController
Your current controller has a couple of small issues we need to resolve to get validation and user creation working properly:
- A typo in the validation rules (
lameinstead oflname) - Incorrect validation logic in the
validator()method - Missing user creation logic after validation
Here's the corrected controller:
<?php namespace App\Http\Controllers\Authentication; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\User; // Import your User model class RegisterController extends Controller { public function index() { return view('auth.register'); } // Returns validation rules as an array protected function validator() { return [ 'fname' => 'required|string|max:255', 'lname' => 'required|string|max:255', // Fixed typo from 'lame' 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:6', ]; } protected function create(Request $request) { // Run validation using our predefined rules $validatedData = $request->validate($this->validator()); // Create and save the new user to the database User::create([ 'fname' => $validatedData['fname'], 'lname' => $validatedData['lname'], 'email' => $validatedData['email'], 'password' => bcrypt($validatedData['password']), // Always hash passwords! ]); // Redirect to login page with a success message (adjust to your dashboard if needed) return redirect()->route('login')->with('success', 'Registration successful!'); } }
3. Update the Form Action in register.blade.php
Right now your form is pointing to /register/create, but we need it to submit to our new POST route at /register. Update the action attribute:
<form class="form-signin" method="post" accept-charset="utf8" action="/register"> @csrf <!-- Rest of your form content remains the same --> </form>
Quick Additional Tips
- Make sure your
Usermodel hasfname,lname,email, andpasswordlisted in its$fillableproperty to allow mass assignment - The success message we added can be displayed in your login view using
@if(session('success')) <div class="alert-success">{{ session('success') }}</div> @endif - You can customize the redirect destination after registration to point to your app's dashboard instead of the login page
内容的提问来源于stack exchange,提问作者Axel

