Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP语言切换器需要在URL中更新_Php_Url_Language Switching - Fatal编程技术网

PHP语言切换器需要在URL中更新

PHP语言切换器需要在URL中更新,php,url,language-switching,Php,Url,Language Switching,我有一个在语言之间切换的选项,它工作得非常好-问题是,url保持不变,我需要使其动态,以便在特定语言选择中共享链接。你知道我该如何实施吗 LanguageSwitcher.php <?php session_start(); if($_GET['la']){ $_SESSION['la'] = $_GET['la']; header('Location:'.$_SERVER['PHP_SELF']); exit(); } switch($_SESSION['la'

我有一个在语言之间切换的选项,它工作得非常好-问题是,url保持不变,我需要使其动态,以便在特定语言选择中共享链接。你知道我该如何实施吗

LanguageSwitcher.php

<?php
session_start();
if($_GET['la']){
    $_SESSION['la'] = $_GET['la'];
    header('Location:'.$_SERVER['PHP_SELF']);
    exit();
}

switch($_SESSION['la']){
     case "en":
        require('lang/en.php');
    break;
    case "dk":
        require('lang/dk.php');
    break;
    default:
        require('lang/en.php');
    }
?>

下拉列表:

<li><a class="dropdown-item" href="index.php?la=en"><img class="ml-3" src="assets/images/flag_uk.png" alt="<?=$lang['lang-en'];?>" title="<?=$lang['lang-en'];?>" style="width: 25px;"/><span class="ml-3"><?=$lang['lang-en'];?></span></a></li>
<li><a class="dropdown-item" href="index.php?la=dk"><img class="ml-3" src="assets/images/flag_dk.png" alt="<?=$lang['lang-dk'];?>" title="<?=$lang['lang-dk'];?>" style="width: 25px;"/><span class="ml-3"><?=$lang['lang-dk'];?></span></a></li>

  • 主要要求是不要在会话中存储语言,而是始终在URL中提供语言,这是一项小工作,因为您需要更新所有页面上的所有URL

    有两种方式浮现在脑海中,其中一种取决于个人偏好

    使用
    获取
    /
    请求

    • 导致URL的格式:
      https://example.com/subpage?la=en
    • 简单,几乎不需要管理工作
    更新所有页面上的所有URL以包括以下语言:

    https://example.com/subpage?la=<?= htmlspecialchars($_GET['la']) ?>
    
    https://example.com/en/subpage
    
    lang/en.php

    <?php
    switch ($_GET['la']) {
        default:
            // no break ('en' is default)
        case 'en':
            require('lang/en.php');
            break;
        case 'dk':
            require('lang/dk.php');
            break;
    }
    
    <!-- some beautiful HTML content here -->
    Visit us at https://example.com/subpage?la=<?= htmlspecialchars($_GET['la']) ?>
    <!-- another beautiful HTML content here -->
    
    <?php
    use Slim\Factory\AppFactory;
    
    require __DIR__ . '/../vendor/autoload.php';
    
    $app = AppFactory::create();
    
    $app->any('/en[/{path:.*}]', function ($request, $response, array $args) {
        // you can also directly put code here,
        // use the $request and $response objects
        // or many other helper classes and methods
        require 'lang/en.php';
    });
    
    $app->any('/dk[/{path:.*}]', function ($request, $response, array $args) {
        // you can also directly put code here,
        // use the $request and $response objects
        // or many other helper classes and methods
        require 'lang/dk.php';
    });
    
    $app->run();
    
    <!-- some beautiful HTML content here -->
    Visit us at https://example.com/en/subpage
    <!-- another beautiful HTML content here -->
    
    例如:

    index.php

    <?php
    switch ($_GET['la']) {
        default:
            // no break ('en' is default)
        case 'en':
            require('lang/en.php');
            break;
        case 'dk':
            require('lang/dk.php');
            break;
    }
    
    <!-- some beautiful HTML content here -->
    Visit us at https://example.com/subpage?la=<?= htmlspecialchars($_GET['la']) ?>
    <!-- another beautiful HTML content here -->
    
    <?php
    use Slim\Factory\AppFactory;
    
    require __DIR__ . '/../vendor/autoload.php';
    
    $app = AppFactory::create();
    
    $app->any('/en[/{path:.*}]', function ($request, $response, array $args) {
        // you can also directly put code here,
        // use the $request and $response objects
        // or many other helper classes and methods
        require 'lang/en.php';
    });
    
    $app->any('/dk[/{path:.*}]', function ($request, $response, array $args) {
        // you can also directly put code here,
        // use the $request and $response objects
        // or many other helper classes and methods
        require 'lang/dk.php';
    });
    
    $app->run();
    
    <!-- some beautiful HTML content here -->
    Visit us at https://example.com/en/subpage
    <!-- another beautiful HTML content here -->
    

    谢谢你的回答@Marcello,我一定会深入调查。欢迎回来询问任何问题。祝你好运