如何在CodeIgniter中将Cookie语言值追加到URL并实现跳转
Let's break down how to implement your required functionality using CodeIgniter, building on your existing code.
1. Add Language Detection & Redirect Logic
First, we need to check for the language cookie on page load, and if it doesn't exist, fall back to the Accept-Language header to determine the user's preferred language. Add this logic to your home controller's index method (or a base controller constructor if you want it site-wide):
public function index() { // Check if language cookie exists $lang_cookie = $this->input->cookie('language'); if ($lang_cookie) { // Map cookie value to URL path $lang_map = [ 'english' => 'en', 'deutch' => 'de' // Add more language mappings here as needed ]; $target_lang = isset($lang_map[$lang_cookie]) ? $lang_map[$lang_cookie] : 'en'; // Default to English redirect(base_url($target_lang)); } else { // Fallback to Accept-Language header $accept_lang = $this->input->server('HTTP_ACCEPT_LANGUAGE'); if ($accept_lang) { // Parse the Accept-Language header (simplified example) $langs = explode(',', $accept_lang); $preferred_lang = strtolower(substr($langs[0], 0, 2)); // Get first 2 chars (e.g., 'en', 'de') // Map short code to your language folder names $code_map = [ 'en' => 'english', 'de' => 'deutch' // Add more mappings here ]; $target_folder = isset($code_map[$preferred_lang]) ? $code_map[$preferred_lang] : 'english'; // Default // Set the cookie for future visits $cookie = array( 'name' => 'language', 'value' => $target_folder, 'expire' => '31536000', ); $this->input->set_cookie($cookie); // Redirect to the corresponding language URL redirect(base_url($preferred_lang)); } else { // No cookie or Accept-Language, redirect to default redirect(base_url('en')); } } // Rest of your index method code... }
2. Update the Language Controller
Your existing language controller sets the cookie but redirects back to the referrer. We need to adjust it to redirect to the correct language URL instead:
public function language($lang = false) { $folder = 'application/language/'; $languagefiles = scandir($folder); if (in_array($lang, $languagefiles)) { $cookie = array( 'name' => 'language', 'value' => $lang, 'expire' => '31536000', ); $this->input->set_cookie($cookie); // Map the language folder to URL path $lang_map = [ 'english' => 'en', 'deutch' => 'de' ]; $target_path = isset($lang_map[$lang]) ? $lang_map[$lang] : 'en'; redirect(base_url($target_path)); } else { // Invalid language, redirect to default redirect(base_url('en')); } }
3. Adjust the View Code (Minor Tweaks)
Your view code is mostly good, but we can clean up the image source logic for readability:
<h5>Translate this page:</h5> <div class="lang-holder"> <a href="#" class="topbar-menu-toggle" data-toggle="button"> <?php // Get current language from cookie or default $current_lang = $this->input->cookie('language') ?: $this->settings_lib->item('site.default_language'); ?> <img src="<?php echo base_url('assets/img/lang/' . $current_lang . '.png'); ?>"> </a> </div> <div id="topbar-dropmenu" class="animated animated-short slideInDown"> <div class="topbar-menu row"> <?php if ($handle = opendir('application/language/')): ?> <?php while (false !== ($entry = readdir($handle))): ?> <?php if ($entry != "." && $entry != ".."): ?> <div class="col-xs-2 col-sm-1"> <a href="<?php echo base_url('home/language/' . $entry); ?>"> <img class="h30" src="<?php echo base_url('assets/img/lang/' . $entry . '.png'); ?>"> <span class="fs14 hidden-xs"><?php echo ucfirst($entry); ?></span> </a> </div> <?php endif; ?> <?php endwhile; ?> <?php closedir($handle); ?> <?php endif; ?> </div> </div>
Key Notes
- Language Mapping: Make sure the
lang_maparrays in both controllers match your cookie values to URL paths (e.g.,english→en,deutch→de). Add more entries as you support additional languages. - Accept-Language Parsing: The example uses a simplified parsing of the header. For more robust handling, you could parse the quality values (e.g.,
en-US;q=0.8) if needed. - Default Language: Always set a default language (like English) to fall back on when no cookie or Accept-Language header is available.
内容的提问来源于stack exchange,提问作者Bunty Kumar Sharma
相关产品推荐
相关产品推荐

