Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
prefix url
  • I would like too add a prefix (fr ou it) to my url to know the language on my link, I want to do it on the route.php file but I don't know how to do it
    exemple
    [url=http://localhost/fuelphp/intranet/public/it/welcome/]http://localhost/fuelphp/intranet/public/it/welcome/[/url]
    [url=http://localhost/fuelphp/intranet/public/fr/welcome/]http://localhost/fuelphp/intranet/public/fr/welcome/[/url]
    can someone help me?
    thanks you
  • i will try to get ride of the 'public' part of the url I have installed the uri class and changed it like:
    public function detect_language()
    {
    if ( ! count($this->segments))
    {
    /*ajout code */
    if ($_SERVER!='')
    {
    if ($_SERVER=="fr-fr")
    {
    Config::set('language',Config::get('language'));
    }
    if ($_SERVER=="it-it")
    {
    Config::set('language',Config::get('language_fallback'));
    }
    }
    else
    {
    Config::set('language',Config::get('language'));
    }
    /*ajout code */ return false;
    } $first = $this->segments[0];
    $locales = Config::get('locales'); if(array_key_exists($first, $locales))
    {
    array_shift($this->segments);
    $this->uri = implode('/', $this->segments); Config::set('language', $first);
    Config::set('locale', $locales[$first]); }
    } }
    ?> So I can detect the browser language can you explain to me "If so, if you alter the URI there and strip the language code, the router will never see the 'fr', because you stripped it...", you mean that with this class the routers looks to all segment expect 'segments[0]' of the url? I'm looking for 3 days now, how to do a bilingual website, who detect the language of the browser to choose a language on the first page (index) and for the other page I was thinking to put the language on the url. I don't know if it is the best solution and I don' t arrive to it!
  • I arrived to do what i wanted with the help of http://fuelphp.com/forums/topics/view/1064/10#7683, now i just need to fond how to get ride of the public segment of the url. Thank for your help and patience!
  • I have extended the URI class for that purpose, and overloaded the constructor:
    public function __construct($uri = null)
    {
     parent::__construct($uri);
    
     if (isset($this->segments[0]) and in_array($this->segments[0], \Config::get('supported_languages')))
     {
      \Config::set('language', $this->segments[0]);
      array_shift($this->segments);
      $this->uri = implode('/', $this->segments);
     }
    }
    

    supported_languages is a key in my config/config.php, and defines an array with supported language codes.
  • thanks for your answer, I'm a beginner and I don't really understand what you told me, can you please explain it with simple word or an example. thanks a lot
  • There is a more detailed discussion about this subject here: http://fuelphp.com/forums/topics/view/1064#7544
  • thanks it helped. I did something more simple like in my bootstrap :
    if ($_SERVER!='')
    {
    if ($_SERVER=="fr-fr") {Config::set('language',Config::get('language'));}
    if ($_SERVER=="it-it") {Config::set('language',Config::get('language_fallback'));}
    }
    else
    {Config::set('language',Config::get('language'));} now in my welcome controller I want to know in which language I am, can I do a variable on my bootstrap and reach it in my welcome controller? if yes do you know how? for the others links I just use the class uri::sergment to know the language. thanks
  • You store it in the config, so get it from there:
    $current_lang = \Config::get('language');
    
  • yes but I don't want the language from the config, but the one I define in my bootstrap, In my code I check if the browser is italian or french and I set the language in french (language) or italian (fallback_language)
  • You should not define the language in your bootstrap. Do not try to reinvent the wheel. 'language' and 'language_fallback' are both config items (defined in config/config.php), you should use those.
    Define your fallback and default language there, and have your URI class extension update 'language' in your detection code (like in the example posted).
  • ok i use the uri class, but this work only if you have your language on your url, for the first page of the website i want to detect the browser language, so where do I put that in the bootstrap ?
  • i finally put my browser detection in the uri class, I suppose it is the place it has to be
  • I want a link like"fr/histoire" my route.php
    <?php
    //$user_language = Cookie::get('language', ''); return array(
    '_root_' => 'welcome/index', // The default route
    '_404_' => 'welcome/404', // The main 404 route 'fr' => 'welcome/index',
    'it' => 'welcome/index', 'histoire' => 'welcome/histoire',
    'storia' => 'welcome/histoire', ); My controller welcome
    public function action_index()
    { $langue_requise=Config::get('language');
    Lang::load('welcome') ;
    $this->template->L = $langue_requise; $this->template->title = Lang::line('Titre');
    $data = Model_Post::find('all');
    $this->template->content = View::forge('welcome/index', $data);
    } public function action_histoire()
    {
    $langue_requise=Config::get('language');
    Lang::load('welcome') ;
    $this->template->L = $langue_requise;
    $this->template->title = Lang::line('Titre');
    $this->template->content = View::forge('welcome/histoire');
    } I use a file temple.php for my views when I click on my link "fr/histoire" on my home page all the link css, img, js are false because my page is under the folder fr, and this folder doesn't exist I just need it to know in which langue I am, I think I have to modify my routes.php file, so i can have a link going to the right controller with "fr" before the controller in my link.
    Can someone help me
    thank you
  • I don't think you understand how routes work. Routes are a mapping from the URL to the internal controller path. Routes have the form 'controller/method/parameters' or 'module/controller/method/parameters'. This means you have to specify the full URL you want to map:
    'fr/histoire' => 'welcome/histoire',
    'it/storia' => 'welcome/histoire',
    
  • i put
    'fr/histoire' => 'welcome/histoire',
    'it/storia' => 'welcome/histoire',
    on my routers.php and on my fuel/app/views/template.php my link a href=" fr/histoire" on my welcome controller
    public function action_histoire()
    {
    $langue_requise=Config::get('language');
    Lang::load('welcome') ;
    $this->template->L = $langue_requise;
    $this->template->title = Lang::line('Titre');
    $this->template->content = View::forge('welcome/histoire');
    } and I have the following error Huh? We can't find that! The controller generating this page is found at APPPATH/classes/controller/welcome.php. This view is located at APPPATH/views/welcome/404.php. can you help me
    thank you
  • please someone can tell how should look the link I put on my template .php ( fuel/app/views/template.php )
    i put a href=" fr/histoire" and I have an error, I can reach my page at [url=http://localhost/fuelphp/intranet/public/welcome/histoire,]http://localhost/fuelphp/intranet/public/welcome/histoire,[/url] but It missing the langue segment, and don't understand correctly how the routes woks, I just need a segment on my url to indict the language and have this segment part on my url
    thanks in advance for your help
  • That would indicate that your rewriting works as it should. So [url=http://yourwebsite/fr/histoire]http://yourwebsite/fr/histoire[/url] should route to 'welcome/histoire', there doesn't seem to be anything wrong with the routes...
  • if my routes are good, how it comes i goes to en error page? where can my error then?
    fuel find my page at [url=http://localhost/fuelphp/intranet/public/welcome/histoire]http://localhost/fuelphp/intranet/public/welcome/histoire[/url] and at [url=http://localhost/fuelphp/intranet/public/fr/histoire]http://localhost/fuelphp/intranet/public/fr/histoire[/url] it goes to en error page"page not found" my link in my template php is 'fr/histoire' and my route ''fr/histoire' => 'welcome/histoire' I'm really missing this error, if it is not the routes or the link what can it be? thanks again to help me
  • I haven't got a clue what goes wrong, I don't know your entire setup. The fact that you have 'fuelphp/intranet/public' in your links means it's is not a standard setup. It is most likely a rewrite issue. If 'welcome/histoire' loads the welcome controller and calls the action_histoire() method, Fuel is working. On the other hand I think I saw you posting in that thread about extending the URI class to support language codes in the URL. Did you install that by any chance? If so, if you alter the URI there and strip the language code, the router will never see the 'fr', because you stripped it...

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion