2

I have already setup project having it's url like below.

index.php?c=controllerName&m=methodName

And parameter in above url like below.

index.php?c=controllerName&m=methodName&selDeg=manager

I want to use url like below for my newely created modules.

admin/Items/1

But want to use first type url as it is for previousely developed modules.

Is it possible to use url with index.php for old modules and without index.php for new modules.

5
  • Dont think that can be done. That is configured in your config.php globally to your app, not just for some parts of it. Commented Dec 14, 2018 at 12:18
  • @marcogmonteiro Can it is possible using htaccess to define that all previous url can work with index.php and new without index.php? Commented Dec 14, 2018 at 12:20
  • 2
    Have a look at this Use both segments and query strings Commented Dec 14, 2018 at 12:40
  • @Yogendrasinh yeah probably but not 100% sure on this. Commented Dec 14, 2018 at 14:44
  • @SherifSalah I have done my config file and added below lines. $config['allow_get_array'] = TRUE; $config['enable_query_strings'] = TRUE; $config['controller_trigger'] = 'c'; $config['function_trigger'] = 'm'; $config['directory_trigger'] = 'd'; Also change $config['uri_protocol']= 'AUTO'; To Other value and PATH_INFO is take effect but still i am not getting the result. The config not allow to work with both type of url at same time. Either it allows old url or new url. Commented Dec 15, 2018 at 5:03

1 Answer 1

1
+50

You can prepare your config like this:

$config['base_url'] = 'your_base_url';
$config['index_page'] = ''; // empty string
$config['enable_query_strings'] = FALSE; // keep it to default FALSE, you can use it even without enabling it here

and your .htaccess like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

I think that is it, now you can use both segments and query_strings like this:

http://localhost/codeigniter/?c=welcome&m=index
http://localhost/codeigniter/welcome/index
http://localhost/codeigniter/index.php/welcome/index
http://localhost/codeigniter/index.php?c=welcome&m=index
http://localhost/codeigniter/index.php/?c=welcome&m=index

I've already tested both in my environment and it works just fine.


Edit: in my codeigniter template i've extended my core router and slightly changed the _set_routing() method like this:

Of course it is a bad practice to change the core system, so you need to extend the core router file and create MY_Router.php under application/core and make it extend CI_Router like this:

class MY_Router extends CI_Router
{
protected function _set_routing()
{
    if (file_exists(APPPATH.'config/routes.php'))
    {
        include(APPPATH.'config/routes.php');
    }

    if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
    {
        include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
    }

    if (isset($route) && is_array($route))
    {
        isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
        isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
        unset($route['default_controller'], $route['translate_uri_dashes']);
        $this->routes = $route;
    }

    $_c = trim($this->config->item('controller_trigger'));
    if ( ! empty($_GET[$_c]))
    {
        $this->uri->filter_uri($_GET[$_c]);
        $this->set_class($_GET[$_c]);

        $_f = trim($this->config->item('function_trigger'));
        if ( ! empty($_GET[$_f]))
        {
            $this->uri->filter_uri($_GET[$_f]);
            $this->set_method($_GET[$_f]);
        }

        $this->uri->rsegments = array(
            1 => $this->class,
            2 => $this->method
        );
    }
    else
    {
        if ($this->uri->uri_string !== '')
        {
            $this->_parse_routes();
        }
        else
        {
            $this->_set_default_controller();
        }
    }
}
}

Now you you have the best of both worlds, you will keep your segments with its goodies untouched but will always look for get c=&m=, i hope its clear enough.

We kept everything to default and made it check for a get request with c and m pretty like what enable_query_strings does but without enabling it and messing anything up to keep working with segments as it is..

Sign up to request clarification or add additional context in comments.

8 Comments

Can you tell me which version you are using? I have set up new codeigniter 3.1.9 and did the changes that you mentioned as above but still i am not able to use both the URL in my setup.
I'm using codeigniter 3.1.9 too, and I've added all the urls I've used and all of them worked fine out of the box with the mentioned configurations.
Lemme check if i have extra configurations in my ci_template
Okay. With this configuration for me it runs only clean URL like codeigniter/welcome/index but when i pass url like codeigniter/?c=welcome&m=index it always going to the default route controller. I have change my default route as $route['default_controller'] = 'test';. so when i pass url with c and m then it always call my test controller's index method.
Look at the edits, now it works just fine but keep $config['enable_query_strings'] = FALSE; @Yogendrasinh
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.