Step 4.1 – Create the admin area for a multilanguage site in CodeIgniter

Created at: February 6, 2015; Last update: September 14, 2015

(created at: February 6, 2015; last update: September 14, 2015)
Before creating a multilanguage site we need to make sure we have the administration area of the languages. So, in the dashboard we will create a controller named languages, that will take care of administering the languages.

The table

First of all let’s create a table with the languages.

Some explaining:

  • language_name will be the language name;
  • language_directory will be the language directory that will be found inside the application/language directory
  • slug will be the URL slug for the language;
  • language_code will be the language code that will appear in the html page.

The controller

Now let’s create the Languages.php file inside application/controllers/admin/ directory:

The model

We will create a Language_model.php inside application/models directory:

The workings

The index() method of languages controller

This method will display all the languages that have been added

The index() method requests a get_all() method from the Language_model. So let’s go to our model and create the method:

The application/views/admin/languages/index_view.php:

The create() method of languages controller

The create() method of language_model:

The application/views/admin/languages/create_view.php:

The update() method of the languages controller:

…the get_by_id() method of the language_model

…and the update() method of the language_model:

…and the application/views/admin/languages/edit_view.php:

The delete() method of the languages controller:

And the delete() method of language_model:

Cool. The easy part is done. After this we will have to see how we can pass the language slug to our controllers when someone is accessing a link.

13 thoughts on “Step 4.1 – Create the admin area for a multilanguage site in CodeIgniter

  1. Federico

    Obviously (but better to ask…who knows? 🙂 ), language_model.php inside application/models should be Language_model.php (capitalize just the first L), right?
    Great and useful tutorial, of course.

  2. Kay

    I think you forgot to mention the codes included with this function

    public function get_by_id()

    When you try to edit a language in the dashboard, an error is raised: “The ID for the language doesn’t exist”
    In your Languages Controller inside the update function, you have a condition where the cuirrent language is equal or not to the ID of the language that has to be updated. If the condition is met, you will be redirected to the edit view of the language, otherwise, you cannot alter that language.

    Good tutorial by the way. This is new and updated and I’m learning so much with this. I was wondering If you can make a tutorial about migrations in CI 3

    1. avenirer Post author

      Wow… a lot of bugs in my tutorials. Thank you for taking the time to notice the errors and report them to me. Just did a new update. As thank you, I will create a tutorial about migrations just for you. 🙂

  3. Stenio

    this line seems to be wrong…

    echo form_input(‘language_directory’,set_value(‘language_directory’,$language->directory),’class=”form-control”‘);

    echo form_input(‘language_directory’,set_value(‘language_directory’,$language->language_directory),’class=”form-control”‘);


  4. Franco

    Thanks for this very detailed tutorial. It works just fine. Anyway when I try to post the empty form (just to trigger the validation) I am still getting the error messages in English while I have the default language set to Dutch.
    I just did this in the Admin_controller (as you do in the Public_Controller, which works just finein the public area):

    $language = $this->data[‘current_lang’];
    $idiom = $language[‘language_directory’];

    But with any success.
    Do you have any tip for me? I will appreciate it.

      1. Franco

        Thanks for your reply. I have the translation already installed. I just want to show the validations according to the current language.

        1. avenirer Post author

          Try this before setting the validation rules… don’t know if it works… $this->config->set_item(‘language’, ‘dutch’);

          1. Franco

            Hi avenirer,
            This works! Of course you really don’t want to hard code the current language. So I need to get the current language in the controller.

            In my Admin controller I have done:

            $language = $this->data[‘current_lang’];
            $idiom = $language[‘language_directory’];
            echo $idiom;

            If I echo the $idiom I get as expected ‘dutch’.

            In the Groups controller I do:

            $this->config->set_item(‘language’, $idiom);

            But I got ‘undefined variable’ $idiom.

            Thanks for your help.

          2. avenirer Post author

            If you look in a previous tutorial you will see that I passed the language in a session variable in MY_Controller, which is extended by Admin_Controller. So you can get that language from the session. Also, regarding your $idiom problem, of course you cannot access it, because you defined it in another class. Search Google for “accessing variables properties from classes php” (or something in these lines)

  5. Franco

    Thanks for your help. I have chosen to set:

    $this->config->set_item(‘language’, $idiom);

    directly in the Admin controller so I don’t have to set this in all my controllers or models.
    It looks that this works just fine (for me).

    I am coming from Laravel, so I am fighting a little bit with Codeigniter.

    Thanks again for your help and for this tutorial (the best out there so far).


Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.