Write a Laravel Chinese verification extension package

📦 Laravel verifies Chinese, mobile phone number and ID number

use

identification explain
chs Verify that the value of a field can only be Chinese characters
chsAlpha Verify that the value of a field can only be Chinese characters and letters
chsAlphaNum Verify that the value of a field can only be Chinese characters, letters and numbers
chsDash Verify that the value of a field can only be Chinese characters, letters, numbers and underscores_ And dash-
mobile Verify phone
idCard ID

Installation tools

Install super brother first package-builder

Switch to a laravel directory to create PKG CD Pkg execution:

establish

package-builder build laravel-validate-ext

The output is as follows

Enter src to create laravevalidateextprovider PHP, as follows

<?php


namespace Liaosp\LaravelValidateExt;
use Illuminate\Support\ServiceProvider;

class LaravelValidateExtProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //todo writes logic here
        die('helloworld');
    }
}

Package loaded into laravel

Enter the package composer JSON editing

Add extension:

    "extra": {
        "laravel": {
            "providers": [
                "Liaosp\\LaravelValidateExt\\LaravelValidateExtProvider"
            ]
        }
    },

Now you need to add this provider class to your laravel project and import it through composer

In the laravel project, composer Add in JSON

    "repositories": [
        {
            "type": "path",
            "url": "pkg\\laravel-validate-ext"
        }
    ]

Execution:

composer require liaosp/laravel-validate-ext dev-master

Writing logic

If there are any errors in this step, such as naming errors, you need to remove composer remove and install it

How do I test whether this package has been imported into the laravel framework? We just provided a class interrupt

    public function boot()
    {
         die('helloworld');
    }

Because the boot initialization is performed first when laravel is loaded

php artisan test

You can see 'hello world'

Here, you will master the development of laravel composer package. I have uploaded the specific implementation to github. If you are interested, you can have a look.
github.com/liaoshengping/laravel-v...

Run

        $v = Validator::make($request->all(), [
            'name' => "idCard"
        ]);

        if ($v->fails()) {
            return $v->errors()->first();
        }

There is no problem with the output. That's it.

Write test

Generally speaking, this is OK, but a qualified package still needs to write tests

New test file:

pkg\laravel-validate-ext\tests\ValidateTest.php

Write the test extension just written, such as:

    /**
     * Verify that the value of a field can only be Chinese characters, letters and numbers
     */
    public function testChsAlphaNum()
    {
        $data = ['name' => 'Small L'];

        $v = \Illuminate\Support\Facades\Validator::make($data, ['name' => 'chsAlphaNum']);

        $this->assertTrue(!$v->fails());

        $data = ['name' => 'Small L.'];

        $v = \Illuminate\Support\Facades\Validator::make($data, ['name' => 'chsAlphaNum']);

        $this->assertFalse(!$v->fails());
    }

The specific test was uploaded to github.com/liaoshengping/laravel-v...

Laravel performs the test

php artisan test pkg/laravel-validate-ext


You can see that the tests just written have passed, indicating that the probability is no problem. Now upload it to packgist for use by other small partners.

Students, in general, it's best to write test files when Pr, so that the author can execute it and merge it without problems

How to Pr an open source composer project

Upload to packgist

Let's make a label
Create a warehouse in github: laravel-validate-ext Send the bag to the warehouse here

git tag v1.0.0
git push origin --tags

Sign in packagist.org/

Enter the github address and submit it, and it is finished


packagist.org/packages/liaosp/lara...

Keywords: Laravel

Added by Steven_belfast on Fri, 17 Dec 2021 12:40:52 +0200