×ðÁú¿­Ê±¹ÙÍøµÇ¼

ThinkPHP6ÖÐÔõÑùʵÏÖ¶àÓïÑÔÍøÕ¾ £¿

thinkphp6ÊÇÒ»¿îÓÅÒìµÄphp¿ª·¢¿ò¼Ü £¬ËüÌṩÁ˺ÜÊÇÓÅÒìµÄ´úÂëÖÎÀíºÍ¿ÉÀ©Õ¹ÐÔ ¡£ÔÚÏÖʵ¿ª·¢ÖÐ £¬Ëæ×ÅÈ«Çò»¯µÄÇ÷ÊÆ £¬Ô½À´Ô½¶àµÄÍøÕ¾ÐèÒªÌṩ¶àÓïÑÔÖ§³Ö ¡£ÄÇôÔÚthinkphp6ÖÐ £¬ÔõÑùʵÏÖ¶àÓïÑÔÍøÕ¾ÄØ £¿±¾ÎĽ«´ÓÒÔÏÂËĸö·½Ãæ¾ÙÐнâ˵ ¡£

Ò»¡¢ÉèÖÃÎļþÖнç˵¶àÓïÑÔ±äÁ¿

ÔÚThinkPHP6ÖÐ £¬ºÜÊÇÍƼöͨ¹ýÉèÖÃÎļþµÄ·½·¨À´½ç˵¶àÓïÑÔ±äÁ¿ ¡£Ê×ÏÈÎÒÃÇÐèÒªÔÚconfigĿ¼Ï½¨ÉèÒ»¸ölang.phpÎļþ £¬È»ºóÔÚÆäÖнç˵¶àÓïÑÔÊý×é £¬ÀýÈ磺

<?php
return [
    'welcome' => '½Ó´ý',
    'hello' => 'ÄãºÃ',
    'bye' => 'ÔÙ¼û',
    ...
];

µÇ¼ºó¸´ÖÆ

½Ó×ÅÔÚ¿ØÖÆÆ÷ÖÐͨ¹ýlangÖúÊÖº¯ÊýÀ´»ñÈ¡ÕâЩ¶àÓïÑÔ±äÁ¿ £¬ÀýÈ磺

echo lang('welcome');

µÇ¼ºó¸´ÖÆ

ÕâÑù £¬µ±ÄãµÄÍøÕ¾ÐèÒªÖ§³Ö²î±ðµÄÓïÑÔʱ £¬Ö»ÐèÒªÐÞ¸Älang.phpÎļþÖжÔÓ¦µÄ¶àÓïÑÔ±äÁ¿Öµ¼´¿É ¡£

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»

¶þ¡¢Ê¹ÓÃÖÐÑë¼þÀ´Æ¾Ö¤ÓïÑÔÉèÖÃSession

ΪÁËÄܹ»Çл»²î±ðµÄÓïÑÔ £¬ÎÒÃÇÐèÒªÔÚÍøÕ¾ÖÐÉèÖÃÒ»¸öÓïÑÔSession ¡£Õâ¸öÓïÑÔSession¿ÉÒÔƾ֤ǰ̨´«À´µÄ²ÎÊý»òÕßä¯ÀÀÆ÷ÉèÖÃÀ´¾ÙÐÐ×Ô¶¯Ê¶±ð ¡£

ÔÚThinkPHP6ÖÐ £¬ÎÒÃÇ¿ÉÒÔͨ¹ýÖÐÑë¼þÀ´ÊµÏÖÕâ¸ö¹¦Ð§ ¡£ÔÚapp/middlewareĿ¼Ï½¨ÉèÒ»¸öLanguage.phpµÄÖÐÑë¼þÎļþ £¬´úÂëÈçÏ£º

<?php
namespace appmiddleware;

use thinkacadeSession;
use thinkRequest;

class Language
{
    public function handle(Request $request, Closure $next)
    {
        $lang = $request->param('lang');
        if(!in_array($lang, ['zh-cn', 'en-us'])){
            $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        }
        Session::set('lang', $lang);
        return $next($request);
    }
}

µÇ¼ºó¸´ÖÆ

ÆäÖÐ £¬ÅжÏת´ïµÄÓïÑÔ²ÎÊýÊÇ·ñÕýµ± £¬ÈôÊDz»¶Ô¹æÔòʹÓÃä¯ÀÀÆ÷ÉèÖÃÖеÄÓïÑÔ²ÎÊý ¡£

ÔÚapp/middleware.phpÖÐÒýÈë¸ÃÖÐÑë¼þ £¬²¢ÔÚÐèÒªÖ§³Ö¶àÓïÑԵĿØÖÆÆ÷ÖÐʹÓà £¬ÀýÈ磺

<?php
namespace appcontroller;

use thinkacadeSession;

class Index
{
    public function index()
    {
        $lang = Session::get('lang');
        return lang('welcome');
    }
}

µÇ¼ºó¸´ÖÆ

Èý¡¢Ê¹ÓöàÓïÑÔ·ÓÉ

Õë¶ÔһЩÐèÒªÖ§³Ö¶àÓïÑÔ·ÓɵÄÐèÇó £¬ThinkPHP6ÖÐÌṩÁ˺ÜÊÇÀû±ãµÄÒªÁì ¡£ÀýÈç £¬ÎÒÃÇ¿ÉÒÔ½ç˵ÒÔÏÂÁ½¸ö·ÓÉ£º

Route::get(':lang/index', 'index/index');
Route::get(':lang/about', 'index/about');

µÇ¼ºó¸´ÖÆ

ÕâÑùÎÒÃÇ»á¼ûhttps://example.com/zh-cn/indexºÍhttps://example.com/zh-cn/aboutʱ¾Í»á½øÈë¶ÔÓ¦µÄ¿ØÖÆÆ÷ÖÐ £¬²¢ÇÒǰ̨¾Í²»ÐèÒªµ¥¶Àת´ïÓïÑÔ²ÎÊýÁË ¡£

ËÄ¡¢Ê¹ÓÃÄ£°å±êÇ©À´Êä³ö¶àÓïÑÔÄÚÈÝ

×îºó £¬ÎÒÃÇÐèÒªÔÚǰ̨¶Ô¶àÓïÑÔ±äÁ¿¾ÙÐÐÊä³ö ¡£ÕâʱÎÒÃDZã¿ÉÒÔʹÓÃThinkPHP6ÌṩµÄ¡°Ä£°å±êÇ©¡±¹¦Ð§ £¬ÀýÈ磺

{: lang('welcome') }

µÇ¼ºó¸´ÖÆ

ËäÈ» £¬¸üÍƼöʹÓÃÏÂÃæÕâÖÖ·½·¨£º

{lang name="welcome"}

µÇ¼ºó¸´ÖÆ

ÕâÑù¼ÈÄܹ»Êä³ö¶ÔÓ¦µÄ¶àÓïÑÔ±äÁ¿ £¬Ò²Äܹ»Ìí¼ÓһЩĬÈÏÖµºÍ²ÎÊý ¡£

×ܽá

ÒÔÉϾÍÊÇÔÚThinkPHP6ÖÐʵÏÖ¶àÓïÑÔÍøÕ¾µÄÒªÁì ¡£Í¨¹ý½ç˵¶àÓïÑÔ±äÁ¿¡¢Ê¹ÓÃÖÐÑë¼þÀ´ÉèÖÃSession¡¢Ê¹ÓöàÓïÑÔ·ÓɺÍÄ£°å±êÇ©¶Ô¶àÓïÑÔÄÚÈݾÙÐÐÊä³ö £¬ÎÒÃǾͿÉÒÔÇáËɵؽ¨ÉèÒ»¸öÍêÕûµÄ¶àÓïÑÔÍøÕ¾ ¡£ËäÈ» £¬ÔÚÏÖʵ¿ª·¢Öл¹ÐèҪ˼Á¿Ò»Ð©Ï¸½ÚÎÊÌâ £¬µ«ÒÔÉÏÒªÁìΪThinkPHP6ÖоÙÐжàÓïÑÔ¿ª·¢ÌṩÁ˺ÜÊǺõĻù´¡ ¡£

ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑùʵÏÖ¶àÓïÑÔÍøÕ¾ £¿µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

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

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

QR code
ÍøÕ¾µØͼ