Search code examples
codeignitercodeigniter-url

naming a codeigniter function as class


I have a codeigniter function which I would like to name as "class" as follows:

public function class($class_id = NULL)
    {
        ...

This will result in a url looking something like domain.com/mydir/class/1

I'm guessing "class" is reserved though.

Is there any way I can achieve this?


Solution

  • CodeIgniter's routing can help you achieve this. You can't name a function class as it is a reserved word.

    For example, if you renamed the function in your question to get_class in a controller called welcome, then you could use the following route in application/config/routes.php

    $route['mydir/class/(:num)'] = "welcome/get_class/$1";
    

    This would route a URL such as domain.com/mydir/class/1, (assuming the root of the application is domain.com), with the final parameter being a number, to the welcome controller and the get_class function, passing 1 as the parameter.