Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/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 - Fatal编程技术网

PHP检查Url并打印默认参数

PHP检查Url并打印默认参数,php,Php,我有多语言系统的Url结构,如下所示: 对于默认语言,即:en(不在url中显示/检测短语言) localhost/cms localhost/cms/article/slug/etc. 还有另一种语言,比如: localhost/cms/fr localhost/cms/fr/article/slug/etc. 我的想法是: 为我的应用定义可用语言列表 定义我在以下情况下使用的默认语言:, A.没有提供任何语言(localhost/cms/users), B提供的语言未知(localhost/

我有多语言系统的Url结构,如下所示:

对于默认语言,即:
en
(不在url中显示/检测短语言)

localhost/cms

localhost/cms/article/slug/etc.

还有另一种语言,比如:

localhost/cms/fr

localhost/cms/fr/article/slug/etc.

我的想法是:

  • 为我的应用定义可用语言列表
  • 定义我在以下情况下使用的默认语言:, A.没有提供任何语言(
    localhost/cms/users
    ), B提供的语言未知(
    localhost/cms/fr/users
  • 在我的代码中,获取当前语言(例如:
    en
    )和routableURI, 没有任何语言的URI(例如:cms/用户)
  • 我有以下代码:

    function urlss(){
    
        $uri = $_SERVER['REQUEST_URI'];
    
        $languages = ['en', 'fr', 'es', 'de'];
    
        // this is my default language
        $defaultLang = 'en';
    
        // $currentLang is the language I'll use to show the contents
        $currentLang = $defaultLang;
    
        $uri = ltrim(rawurldecode($uri), '/');
        $parts = explode('/', $uri);
    
        if( ! empty($parts) && in_array(strtolower($parts[0]), $languages)) {
            $currentLang = array_shift($parts);
        }
    
        $routableUri = implode('/', $parts);
    
    return $routableUri;
    
    } 
    
    我的代码输出默认语言:(假输出)不工作

    选中:
    localhost/cms/
    localhost/cms/users/

    输出为:
    cms/
    cms/users/

    另一种语言的输出:(true输出)工作为true

    检查:
    localhost/cms/fr/
    输出为
    cms/fr/
    localhost/cms/fr/users/
    输出为:
    cms/fr/users/

    我如何打印我的默认语言,而不在url中添加/检测这样的短语言名称


    Check:
    localhost/cms
    输出为:
    cms/en
    或Check
    localhost/cms/users/
    输出为:
    cms/en/users/

    可能将它们存储在Cookie中?@BARNI:Belike,但在这种情况下,我添加到路由类中以获得路由url。