尊龙凯时人生就是博

laravelÐÞ¸ÄÑéÖ¤ÌáÐÑÖÐÎÄ

laravelÊÇÒ»¿î¿ªÔ´µÄphp webÓ¦ÓóÌÐò¿ò¼Ü£¬¾ßÓдúÂ뾫Á·¡¢Ò×ÓÚÃ÷È·ºÍѧϰ¡¢³äÂúÁ¢ÒìµÄÌص㣬Òò´ËÊܵ½Ô½À´Ô½¶à¿ª·¢ÕߵĽӴý¡£ÆäÖУ¬ÑéÖ¤ÊÇlaravel¿ò¼ÜÖкÜÊÇÖ÷ÒªµÄÒ»²¿·Ö£¬Ëü×ÊÖú¿ª·¢ÕßÑéÖ¤ºÍ± £»¤Óû§Ìá½»µÄ±íµ¥Êý¾Ý¡£¿ÉÊÇ£¬laravelĬÈϵÄÑéÖ¤ÌáÐÑÐÅÏ¢¶¼ÊÇÓ¢ÎĵÄ£¬Î´±ãÓÚÖÐÎÄÕ¾µãµÄʹÓ㬽ÓÏÂÀ´£¬ÎÒÃǾÍÀ´Ì½ÌÖÒ»ÏÂÔõÑù½«laravel¿ò¼ÜµÄÑéÖ¤ÌáÐÑÐÅÏ¢Ð޸ijÉÖÐÎÄ¡£

0¡¢Ð¡Ðò

ÔÚ×îÏÈÕýʽµÄÐÞ¸ÄÊÂÇé֮ǰ£¬ÎÒÃÇÐèÒªÃ÷È·¼¸¸ö¿´·¨¡£

µÚÒ»£¬Laravel¿ò¼ÜĬÈϵÄÑéÖ¤Æ÷ÊÇIlluminateValidationValidator¡£

µÚ¶þ£¬Laravel¿ò¼ÜÖÐĬÈÏʹÓÃÁËSymfony¿ò¼ÜµÄvalidator×é¼þ¡£

µÚÈý£¬Laravel¿ò¼Ü×Ô¼ºÌṩÁ˶àÖÖÐÞ¸ÄÑéÖ¤ÌáÐÑÐÅÏ¢µÄÒªÁ죬ºÃ±ÈÐÞ¸ÄÑéÖ¤Æ÷ÖеÄmessagesÊý×顢ʹÓÃÓïÑÔ°üµÈ¡£

1¡¢ÐÞ¸ÄÑéÖ¤Æ÷ÖеÄmessagesÊý×é

ÔÚLaravel¿ò¼ÜÖУ¬ValidatorÀàÖÐÓÐÒ»¸ömessagesÊý×飬Ëü´æ´¢×ÅËùÓеÄÑéÖ¤ÌáÐÑÐÅÏ¢£¬ÔÚʹÓÃValidatorʱ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÐ޸ĸÃÊý×éµÄ·½·¨À´ÐÞ¸ÄÑéÖ¤ÌáÐÑÐÅÏ¢¡£ÏÂÃæÊÇÒ»¶ÎʾÀý´úÂ룺

$validator = Validator::make($request->all(), [
    'name' => 'required|max:255',
    'email' => 'required|email|unique:users,email',
    'password' => 'required|confirmed|min:6',
]);

$validator->messages()->add(
    'email.unique', '¸ÃÓÊÏäÒѱ»×¢²á£¬ÇëʹÓÃÆäËûÓÊÏä¡£'
);

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃValidator::makeÒªÁ콨ÉèÁËÒ»¸öÑéÖ¤Æ÷£¬²¢ÉèÖÃÁËÈý¸öÑéÖ¤¹æÔò£ºname±ØÌî¡¢³¤¶È²»Áè¼Ý255¸ö×Ö·û £»email±ØÌî¡¢±ØÐèΪÕýµ±µÄÓÊÏäµØµã¡¢ÔÚusers±íÖÐΨһ £»password±ØÌȷÈÏÃÜÂë¡¢³¤¶È²»ÉÙÓÚ6¸ö×Ö·û¡£È»ºó£¬ÎÒÃÇͨ¹ýŲÓÃ$validator->messages()->addÒªÁìÀ´Ìí¼ÓÁËÒ»¸öеÄÌáÐÑÐÅÏ¢£¬Ö¸¶¨Á˵±email×ֶεÄÑéÖ¤¹æÔòuniqueʧ°Üʱ£¬Ó¦¸ÃÌáÐÑ¡°¸ÃÓÊÏäÒѱ»×¢²á£¬ÇëʹÓÃÆäËûÓÊÏä¡£¡±ÕâÑù£¬ÎÒÃǾͿÉÒÔÎÞаµØÐÞ¸ÄÑéÖ¤ÌáÐÑÐÅÏ¢ÁË¡£

ÐèҪעÖصÄÊÇ£¬Ê¹ÓÃÕâÖÖÒªÁìÐ޸ĵÄÌáÐÑÐÅÏ¢Ö»»áÔÚ½¨ÉèÄ¿½ñÑéÖ¤Æ÷ʱÉúЧ£¬ÈôÊÇÐèÒª¶ÔËùÓеÄÑéÖ¤Æ÷¶¼ÉúЧ£¬ÔòÐèÒªÔÚÿ¸öÑéÖ¤Æ÷Öж¼Ìí¼ÓÒ»±é¡£

2¡¢Ê¹ÓÃÓïÑÔ°ü

³ýÁËÔÚValidatorÖÐÖ±½ÓÐÞ¸ÄmessagesÊý×飬Laravel»¹ÌṩÁËÁíÒ»ÖÖ±ã½ÝµÄÒªÁìÀ´ÐÞ¸ÄÑéÖ¤ÌáÐÑÐÅÏ¢£¬ÄǾÍÊÇʹÓÃÓïÑÔ°ü¡£Laravel¿ò¼ÜĬÈÏÌṩÁ˶àÖÖÓïÑÔ°ü£¬°üÀ¨Ó¢ÎÄ¡¢Î÷°àÑÀÓï¡¢·¨Óï¡¢µÂÓï¡¢ÈÕÓï¡¢ÖÐÎĵÈ¡£ÎÒÃÇ¿ÉÒÔÔÚÓïÑÔ°üÖÐÕÒµ½¶ÔÓ¦µÄÑéÖ¤ÌáÐÑÐÅÏ¢£¬²¢¾ÙÐÐÐ޸ġ£

ÔÚLaravelÖÐʹÓÃÓïÑÔ°üºÜÊǼòÆÓ£¬Ö»ÐèÒªÔÚresources/langĿ¼Ï½¨Éè¶ÔÓ¦µÄÓïÑÔ°üÎļþ¼Ð£¬¼´¿É×îÏÈÐÞ¸ÄÑéÖ¤ÌáÐÑÐÅÏ¢¡£ÀýÈ磬ÈôÊÇÎÒÃÇÐèÒª½«ÑéÖ¤ÌáÐÑÐÅÏ¢¸Ä³ÉÖÐÎÄ£¬ÔòÐèÒªÔÚresources/langĿ¼Ï½¨Éèzh-CNÎļþ¼Ð£¬²¢ÔÚ¸ÃÎļþ¼ÐϽ¨Éèvalidation.phpÎļþ£¬È»ºó½«Ð޸ĺóµÄÑéÖ¤ÌáÐÑÐÅϢдÈëµ½¸ÃÎļþÖм´¿É¡£Ê¾Àý´úÂëÈçÏ£º

<?php
// resources/lang/zh-CN/validation.php

return [
    'required' => ' :attribute Ϊ±ØÌîÏî¡£',
    'max' => [
        'numeric' => ' :attribute ²»¿É´óÓÚ :max¡£',
        'file' => ' :attribute ²»¿É´óÓÚ :max kb¡£',
        'string' => ' :attribute ²»¿ÉÁè¼Ý :max ¸ö×Ö·û¡£',
        'array' => ' :attribute ²»¿ÉÁè¼Ý :max ¸öÏî¡£',
    ],
    'email' => ' :attribute ±ØÐèΪÕýµ±µÄÓÊÏäµØµã¡£',
    'unique' => ' :attribute Òѱ£´æ£¬ÇëʹÓÃÆäËû :attribute¡£',
    'confirmed' => 'Á½´ÎÊäÈëµÄ :attribute ·×ÆçÖ¡£',
];

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃǽç˵ÁËrequired¡¢maxµÈ¶à¸öÑéÖ¤¹æÔòµÄÌáÐÑÐÅÏ¢¡£ÕâЩÐÅÏ¢»áÔÚLaravel¿ò¼ÜÖÐÓÃÓÚÑéÖ¤Óû§Ìá½»µÄ±íµ¥Êý¾Ý£¬²¢ÔÚÑé֤ʧ°Üʱ¾ÙÐÐÌáÐÑ¡£ÕâÖÖÒªÁì½ÏÁ¿ÊʺÏÔÚÈ«Õ¾¹æÄ£ÄÚÐÞ¸ÄÑéÖ¤ÌáÐÑÐÅÏ¢£¬²»±ØÔÚÿ¸öValidator¹¤¾ßÖж¼Ìí¼ÓÒ»´Î¡£

3¡¢SymfonyÑéÖ¤×é¼þµÄ·­Òë

³ýÁËÉÏÊöÁ½ÖÖÒªÁìÍ⣬SymfonyÑéÖ¤×é¼þÒ²ÌṩÁËÄÚÖ÷­ÒëµÄ¹¦Ð§£¬ÎÒÃÇ¿ÉÒÔʹÓÃÕâ¸ö¹¦Ð§½«Laravel¿ò¼ÜÖеÄÑéÖ¤ÌáÐÑÐÅÏ¢·­Òë³ÉÖÐÎÄ¡£

ҪʹÓÃSymfonyÑéÖ¤×é¼þµÄ·­Ò빦Ч£¬ÎÒÃÇÊ×ÏÈÐèҪװÖÃsymfony/translation×é¼þ£¬ÒªÁìÈçÏ£º

composer require symfony/translation

µÇ¼ºó¸´ÖÆ

È»ºó£¬ÎÒÃÇÐèÒªÔÚLaravel¿ò¼ÜÖÐÉèÖ÷­ÒëÆ÷£¬ÔÚAppServiceProviderÀàµÄbootÒªÁìÖÐÌí¼ÓÒÔÏ´úÂ룺

use IlluminateSupportFacadesLang;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app->singleton('translator', function($app){
            $loader = new SymfonyComponentTranslationLoaderArrayLoader;
            $translator = new SymfonyComponentTranslationTranslator('zh');
            $translator->addLoader('array', $loader);
            $translator->addResource('array', require __DIR__ . '/../vendor/symfony/validator/Resources/translations/validators.zh.xlf', 'zh');
            $loader->load($this->getTranslatorMessages());

            return new IlluminateTranslationTranslator($translator);
        });
    }

    public function getTranslatorMessages()
    {
        $messages = [
            'required' => ':attribute Ϊ±ØÌîÏî¡£',
            'max' => [
                'numeric' => ':attribute ²»¿É´óÓÚ :max¡£',
            ],
            'email' => ':attribute ±ØÐèΪÕýµ±µÄÓÊÏäµØµã¡£',
            'unique' => ':attribute Òѱ£´æ£¬ÇëʹÓÃÆäËû :attribute¡£',
            'confirmed' => 'Á½´ÎÊäÈëµÄ :attribute ·×ÆçÖ¡£',
        ];

        return $messages;
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÔÚAppServiceProviderÖÐʹÓÃÁËSymfonyÑéÖ¤×é¼þÌṩµÄ·­Ò빦Ч¡£ÆäÖУ¬ÎÒÃÇÉèÖÃÁË·­ÒëÆ÷µÄÓïÑÔΪÖÐÎÄ£¬²¢¼ÓÔØÁËSymfonyÑéÖ¤×é¼þ×Ô´øµÄ·­ÒëÎļþvalidators.zh.xlf¡£ÔÚgetTranslatorMessagesÒªÁìÖУ¬ÎÒÃǽç˵ÁËÐèÒª·­ÒëµÄÑéÖ¤ÌáÐÑÐÅÏ¢¡£ÕâÑù£¬µ±Laravel¿ò¼ÜÑéÖ¤±íµ¥Êý¾Ýʱ£¬Symfony·­Òë×é¼þ¾Í»á×Ô¶¯½«Ó¢ÎĵÄÑéÖ¤ÌáÐÑÐÅÏ¢·­Òë³ÉÖÐÎÄ¡£

ÐèҪעÖصÄÊÇ£¬ÕâÖÖÒªÁì½ÏÁ¿Æ¶À§£¬ÐèҪװÖÃеÄ×é¼þ£¬²¢ÐÞ¸ÄLaravel¿ò¼ÜµÄServiceProviderÀà¡£

4¡¢×ܽá

ÒÔÉϾÍÊÇÐÞ¸ÄLaravel¿ò¼ÜÑéÖ¤ÌáÐÑÐÅÏ¢µÄÒªÁ죬°üÀ¨ÔÚValidatorÖÐÖ±½ÓÐÞ¸ÄmessagesÊý×顢ʹÓÃÓïÑÔ°ü¡¢SymfonyÑéÖ¤×é¼þµÄ·­ÒëµÈÒªÁì¡£²î±ðµÄÒªÁìÊÊÓÃÓÚ²î±ðµÄ³¡¾°£¬¿ÉÒÔƾ֤×Ô¼ºµÄÐèÒª¾ÙÐÐÑ¡Ôñ¡£

ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇͨ³ £»áÍŽáʹÓÃÉÏÊö¶àÖÖÒªÁ죬ÀýÈçÔÚValidatorÖÐÌí¼Ó×Ô½ç˵µÄÑéÖ¤ÌáÐÑÐÅÏ¢£¬²¢ÔÚÓïÑÔ°üÖоÙÐÐÁýÕֺ͵÷½â£¬ÕâÑù¼È¿ÉÒÔ°ü¹ÜϸÄ廯µÄ¿ØÖÆ£¬ÓÖ¿ÉÒÔÀû±ãµØ¾ÙÐÐÈ«Õ¾µÄÖÎÀíºÍά»¤¡£

ÒÔÉϾÍÊÇlaravelÐÞ¸ÄÑéÖ¤ÌáÐÑÖÐÎĵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ