【问题标题】:Redirect to the same page after switching the language in Codeigniter在 Codeigniter 中切换语言后重定向到同一页面
【发布时间】:2018-05-14 08:26:06
【问题描述】:

我的网站上有语言切换器,它工作正常。但是,它会重定向到基本 URL。

但是当我写redirect($_SERVER["HTTP_REFERER"]); 时,它没有正确重定向。当我在主页上更改新语言时,我应该保持相同的 URL,只是让网站更改语言。

我该如何解决这个问题?

这是我的控制器:

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');
class LanguageSwitcher extends CI_Controller
{
    public function __construct() {
        parent::__construct();  
        $this->load->helper('url');    
    }

    function switchLang($language = "") {

        $language = ($language != "") ? $language : "azerbaijani";
        $this->session->set_userdata('site_lang', $language);

        redirect(base_url());

    }
}

另外,我试过这个,它不适合我:

function switchLang($language = "") {

        if($this->uri->uri_string() == '') {

            $language = ($language != "") ? $language : "azerbaijani";
            $this->session->set_userdata('site_lang', $language);

            redirect(base_url());
        } else {

            $language = ($language != "") ? $language : "azerbaijani";
            $this->session->set_userdata('site_lang', $language);

            redirect($_SERVER["HTTP_REFERER"]);
        }


    }

【问题讨论】:

标签: php codeigniter


【解决方案1】:

您可以通过会话来执行此操作,但我更喜欢这种使用 uri-query 的方法:它的作用是将?my-current-url 部分添加到您调用新语言的链接中。

所以,在我的 view 中,我有一个链接(按钮等),它调用语言切换器,将当前站点 url 添加为 uri-query 之类的这个:

<?php 
  // set pathname from where we came from
     $pn=uri_string();  // the uri class is initialized automatically
?>
<a href="/LanguageSwitcher/switchLang/azerbaijani?<?=$pn?>">Azerbaijani</a>

然后在您的函数switchLang() 中处理重定向:

function switchLang($language = "") {
    // get pathname from where we came from
    $pn= parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
    $language = ($language != "") ? $language : "azerbaijani";
    $this->session->set_userdata('site_lang', $language);

    // redirect to where we came from
       redirect($pn, 'refresh');    
}

【讨论】:

  • 表示每次在url处都是?az?rus?
  • 不,只有在链接(按钮)或您用来点击的任何地方,新语言才会显示。这显然是基于您的报价:“我的网站上有语言切换器,它工作正常”?$pn 将为您提供您来自的路径名
  • 首先,它重定向到LanguageSwitcher/switchLang/russian 页面,然后重定向到我更改了语言的那个页面。正常吗?
  • 不, 部分调用控制器,将您重定向到新语言页面,它应该与您原来的方法相同,但是重定向(base_url());现在它重定向到您来自的页面,但是使用新语言...但是等待我设置了 $config['index_page'] = '';并用 .htaccess 删除索引部分,这可能会搞乱整个策略
  • 我也应该删除它吗?
【解决方案2】:

我在所有项目中所做的(对于您的要求可能不是一个简单的解决方案,但对这个和其他场景非常有用)是使用两个助手,我称之为 history()back()

History 将当前 url 保存在 session 中的数组中:

$history[] = current_url();
$ci->session->set_userdata('history', $history);

Back($num) 接收一个数字并重定向到该位置。 即:back(1) 相当于“将我发送到上次访问的页面”。

$history = $this->session->userdata('history');
$cant = count($history);
$back = $cant - $num;
redirect($history[$back]);

然后在“CI_Controller”(或您扩展控制器的任何类)的 __construct() 中调用 history() 一次以记录每个方法。

另外,您可以添加第三个控制器,我称之为 no_history()

no_history() 删除最后保存的 URL(我在不想保存的方法中使用它,例如 ajax 请求)。您也可以检查 $this->input->is_ajax_request() 但这只是一个想法。

就是这样,现在您有了访问 URL 的历史记录。 您可以随意更改,然后返回(1)。 对于将您的用户重定向到他们在登录前所做的任何事情也很有用。

哦!差点忘了:记得限制数组的大小,并且总是在添加新的 url 之前删除最旧的 url。五到十之间就可以了。

【讨论】:

    【解决方案3】:
     /*
        |--------------------------------------------------------------------------
        | Base Site URL
        |--------------------------------------------------------------------------
        |
        | URL to your CodeIgniter root. Typically this will be your base URL,
        | WITH a trailing slash:
        |
        |   http://example.com/
        |
        | WARNING: You MUST set this value!
        |
        | If it is not set, then CodeIgniter will try guess the protocol and path
        | your installation, but due to security concerns the hostname will be set
        | to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
        | The auto-detection mechanism exists only for convenience during
        | development and MUST NOT be used in production!
        |
        | If you need to allow multiple domains, remember that this file is still
        | a PHP script and you can easily do that on your own.
        |
        */
        $config['base_url'] = 'your domain';
    

    尝试在您的应用程序/配置目录中更改您的 config.php 中的 base_url,并使用您的 URL 地址完成。

    【讨论】:

    • 我不需要这个
    【解决方案4】:

    试试这个

    $url = $_SERVER['HTTP_REFERER'];
    redirect($url);
    

    【讨论】:

    • 最佳和最短的答案。完美运行
    【解决方案5】:

    希望对您有所帮助

    $this->load->library('user_agent');
    if ($this->agent->is_referral())
    {
        echo $this->agent->referrer();
    }
    

    【讨论】:

    【解决方案6】:

    尝试使用:

    redirect($uri, 'refresh');
    

    其中$uri 是您要翻译的主页的网址

    【讨论】:

    • 我应该先定义URI吗?
    • 那太好了