Welcome Guest, Not a member yet? Register   Sign In
How do I use base_url() in my case?
#1

[eluser]KeyStroke[/eluser]
Hi,

My site uses two languages: English and Arabic. The language is sent through the first segment (e.g.: example.com/index.php/en and example.com/index.php/ar), then the routes file would re-route those requests to example.com/index.php/.

My question is, how would I use base_url() in my case since I have a different base url for each language? I need to make it flexible somehow.


Appreciate your help.
#2

[eluser]frenzal[/eluser]
I modified my url helper (MY_url_helper.php - don't overwrite ci core) so base_url() would give me example.com and allow for parameter like site_url(), site_url i use for the url of the subsite i'm on like example.com/en/

I would give you code but the way I did it was kinda dirty Smile Maybe you get the site_url() function to always include the first uri segment if it's 2 letters or something..
#3

[eluser]KeyStroke[/eluser]
You just reminded me that I'm able to modify helpers' code.

Check this out:
When I re-code it to this:
Code:
function base_url()

    {

        $CI =& get_instance();

        $url = $CI->config->slash_item('base_url') . $CI->uri->segment(1);

        return $url;

    }
I get my old base_url, as if CI is completley ignoring my modification.

but when I do this:
Code:
function base_url()

    {

        $CI =& get_instance();

        $url = $CI->config->slash_item('base_url') . $CI->uri->segment(1);

        return '';

    }

It will work just how I want it!

What is wrong with this thing?
#4

[eluser]Lone[/eluser]
If I were you i would use more of the session options available to you in CI to set the language for the site rather then depending on the URL as the first segment is usually considered to be your controller.

Have some kind of drop down at the top that users can switch the language which just switches their 'language' session variable.

But if you doing a different URL so that people can hotlink to a page and it is in the right language then it certainly becomes a different problem. A quick way that I can think of doing it is below (untested) - im certain there could be a neater approach but this might give you some good ideas..

Set the following in routes.php

Code:
$route['en/(.*)'] = "language/en/$1";
$route['ar/(.*)'] = "language/ar/$1";

Then create a 'language.php' controller like below

Code:
class Language extends Controller {
                        
    function __construct() {
        parent::Controller();
    }

    function en() {
        $this->session->set_userdata('language', 'en');
        redirect($this->uri->segment(1).'/'.$this->uri->segment(2));
    }
    
    function ar() {
        $this->session->set_userdata('language', 'ar');
        redirect($this->uri->segment(1).'/'.$this->uri->segment(2));
    }
    
}
#5

[eluser]KeyStroke[/eluser]
Thanks Lone, but the problem is I already have a lot of coding done, and changing the structure and controllers to implement your method would take a plenty of time.

That's why I'm asking how I solve my issue.

The modifcation I've posted works pretty well when I return an empty string, but I'd like to know why. Logically, it shouldn't work. But it does, which scares me.
#6

[eluser]Lone[/eluser]
I can't see how it is working either - can you post an example piece of code where you are using it along with the full url_helper replacement?
#7

[eluser]KeyStroke[/eluser]
usage example:
Code:
<a href="&lt;?=base_url()?&gt;post/new_post">Post New</a>

and the replacement for base_url() (if that's what you mean) is in my previous post.
#8

[eluser]wiredesignz[/eluser]
Check out the URI Language Identifier extension, it may help. See my signature. Good Luck.
#9

[eluser]xwero[/eluser]
I find it strange you use base_url that only gets http://site.com/. In your examples you have example.com/index.php/en. This means you have to use site_url. With the site url it's possible to add the segments you want you can add them as a string or as an array. If you add that to the view you don't need to hack. A quick and dirty example
Code:
// controller
function page()
{
    $view['segment'] = $this->uri->segement(1);
    $this->load->view('view',$view);
}
// view
<a href="&lt;?=site_url($segment)?&gt;/post/new_post">Post New</a>
You could even do
Code:
<a href="&lt;?=site_url($this->uri->segement(1))?&gt;/post/new_post">Post New</a>
If you always have a language segment




Theme © iAndrew 2016 - Forum software by © MyBB