在Codeigniter

时间:2017-11-30 11:54:58

标签: php codeigniter

我的网站上有语言切换器,工作正常。但是,它会重定向到基本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"]);
        }


    }

6 个答案:

答案 0 :(得分:1)

您可以使用会话执行此操作,但我更喜欢使用uri查询的方法:它的作用是在您调用新语言的链接中添加?my-current-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');    
}

答案 1 :(得分:0)

尝试使用:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    arr = new ArrayList<>();

    if (getArguments() != null) {
        String[] data = getArguments().getStringArray("cat_data");
        cat_id = data[0];
        cat_name = data[1];
    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    if (view == null) {
        view = inflater.inflate(R.layout.fragment_sub_category, container, false);

        progress = view.findViewById(R.id.progress);
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView = view.findViewById(R.id.recycler);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);

        subCatAdapter = new SubCatAdapter(arr, getActivity());
        recyclerView.setAdapter(subCatAdapter);

        subCatAdapter.setOnClickListener(this);

        doThis(); //request data from server
    }

    return view;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putString("cat_id", cat_id);
    outState.putString("cat_name", cat_name);

    super.onSaveInstanceState(outState);
}

 @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (savedInstanceState != null) {
        cat_id = savedInstanceState.getString("cat_id");
        cat_name = savedInstanceState.getString("cat_name");
    }
}

其中redirect($uri, 'refresh'); 是您要翻译的主页的网址

答案 2 :(得分:0)

希望这可以帮助您

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

答案 3 :(得分:0)

试试这个

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

答案 4 :(得分:0)

 /*
    |--------------------------------------------------------------------------
    | 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';

尝试使用您的URL地址完成更改application / config目录下config.php中的base_url。

答案 5 :(得分:0)

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

历史记录会在会话中将当前网址保存在数组中:

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

后退($ num)收到一个数字并重定向到该位置。 即:返回(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-&gt; input-&gt; is_ajax_request(),但这只是一个想法。

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

哦!几乎忘了:记住要限制数组的大小,并在添加新URL之前始终删除最旧的URL。五到十分之间没问题。