


Introduction to field verification methods under Laravel multiple domain names (with code)
This article brings you an introduction to the field verification method under Laravel's multi-domain name (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Foreword: A unified author backend is being developed to allow authors to submit software to the website. We have developed an author backend for one of the websites, and now we plan to provide this backend to other websites. It has the following characteristics:
The domain names we visit are inconsistent. For the solution, see one of my articles, Laravel Routing Research: Domain to Solve Multiple Domain Name Problems
Function Splitting
Before we start, we need to split the various functional points of the system and estimate the affected points:
Login and Register
The login registration function is the first to bear the brunt. We need users to access different domain names and record different identities when registering. So we need to perform the following processing:
Add fieldsidentity
Perform heavy judgment
Perform login verification
Data processing
This will not be discussed. Depending on the identity of the user, the data called is also different.
Registration judgment
Basis for judgment: We know that after using php artisan make:auth, email is used to log in by default, and email is judged by default in form verification. . The code is as follows:
Default form verification:
// Path:app/Http/Controllers/Auth/RegisterController.php protected function validator(array $data) { return Validator::make($data, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8', 'confirmed'], ]); }
Default login verification field
// Path:vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php public function username() { return 'email'; } // 当然可以修改验证字段(看过文档的都知道),注意:登录验证字段必须是在表里面唯一的。
Now we need to analyze our needs:
In a single user background, email judgment Heavy enough is enough, but not enough for multiple users.
Assumption: We have two domain names A and B, corresponding to two users a and b. We need to store a and b in a table. First, we determine whether a and b belong to that domain name (site). Secondly, see Is this user a duplicate?
Let’s use Laravel form verification to implement it below:
Add fields:
For the convenience of demonstration, I directly modified it on the migration file generated by make auth. Please do not modify it directly in the actual project. , but by creating a new migration file and adding fields by modifying the table structure
public function up() { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email'); // 去掉原来的unique $table->string('identity'); // 增加的字段 $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
Note: In this requirement, we do not need to unique the email and name fields in the migration file Qualified, because their uniqueness is dependent, not independent.
Simulate user registration, insert identity information
// Path: app/Http/Controllers/Auth/RegisterController.php protected function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), 'identity' => 'pcsoft', // 模拟用户注册时,插入身份字段值 ]); }
Perform heavy judgment processing
protected function validator(array $data) { return Validator::make($data, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users')->where(function ($query) { $query->where('identity', '=', 'onlinedown'); })], // 这句话的意思:按照什么条件对 users 表中的 email 去重,我们需要按照身份字段等于我们访问的域名对 email 去重, 'password' => ['required', 'string', 'min:8', 'confirmed'], ]); }
Test
Perform first registration, the database is cut off as follows:Same identity, same email test
Login verification
Overwrite credentials and pass in the authentication field
// Path:app/Http/Controllers/Auth/LoginController.php protected function credentials(Request $request) { $request->merge(['identity' => Controller::getWebPrefix()]); return $request->only($this->username(), 'password', 'identity'); }
[Related recommendations: PHP video tutorial]
The above is the detailed content of Introduction to field verification methods under Laravel multiple domain names (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

Laravel provides a comprehensive Auth framework for implementing user login functions, including: Defining user models (Eloquent model), creating login forms (Blade template engine), writing login controllers (inheriting Auth\LoginController), verifying login requests (Auth::attempt) Redirecting after login is successful (redirect) considering security factors: hash passwords, anti-CSRF protection, rate limiting and security headers. In addition, the Auth framework also provides functions such as resetting passwords, registering and verifying emails. For details, please refer to the Laravel documentation: https://laravel.com/doc

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Want to learn the Laravel framework, but suffer from no resources or economic pressure? This article provides you with free learning of Laravel, teaching you how to use resources such as online platforms, documents and community forums to lay a solid foundation for your PHP development journey from getting started to master.

In the Laravel framework version selection guide for beginners, this article dives into the version differences of Laravel, designed to assist beginners in making informed choices among many versions. We will focus on the key features of each release, compare their pros and cons, and provide useful advice to help beginners choose the most suitable version of Laravel based on their skill level and project requirements. For beginners, choosing a suitable version of Laravel is crucial because it can significantly impact their learning curve and overall development experience.

The Laravel framework has built-in methods to easily view its version number to meet the different needs of developers. This article will explore these methods, including using the Composer command line tool, accessing .env files, or obtaining version information through PHP code. These methods are essential for maintaining and managing versioning of Laravel applications.
