How to hide the driver in the url with codeigniter?


I do not know how to hide the controller name and its function in the url of my website.

I have several controladores , one of them is blog , which is the controlador por defecto set in the file routes.php .

$route['default_controller'] = 'blog';

I have been testing several things, including setting the following code in the file routes.php .

$route['entradas'] = 'blog/entradas';

But I can not get it to work and it's something that brings me headlong, since I'm currently learning codeigniter, if someone can enlighten me, I'd appreciate it.

asked by Akarin 02.10.2017 в 11:08

2 answers


You can define a custom route in config / routes.php , for example:

$route['entradas'] = 'blog/entradas';

Va a

In other words more explained:

There is a file where we can generate all kinds of rules to get a URI routing different from the default. We can find that file in the directory of configuration files:


There we can define a array called $ route where we will place all the routing rules that we want to create specifically in our web application CodeIgniter .

If we open the aforementioned file we can see that there are already two routing values defined in the array $ route .

$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";

These two routing values are specific to CodeIgniter and are reserved for any other use. We can change the default routing values for those variables, but we can not create routes for our application by using them. In addition these two predetermined routes must appear before any other route that we define later in the file routes.php .

Nota: El primero de ellos, default_controller, sirve para definir el controlador por defecto que se ejecutaría en caso que no se indique ninguna ruta en la URL (en la raíz del dominio). En la configuración que viene en la instalación básica de CodeIgniter, si no se específica nada en la URL, se abre el controlador "welcome", pero ese controlador predeterminado lo podemos cambiar según nuestras necesidades. 
Al hablar de controladores ya se comentó qué era el controlador por defecto y cómo podemos cambiarlo. Ahora ya sabemos que ese cambio de controlador predeterminado en realidad no era más que la definición de una regla de enrutado en CodeIgniter.

Then, to create our own rules, it would be simply to assign new values to the associative array $ route and for that we have to pay attention to the following syntax:

$ route ['de_donde_vienes'] = 'a_donde_vas';

As you can see, in the routes we have two parts:

In the index of the associative array, ' de_donde_vienes ', we place a pattern that must match for a routing to occur. In the value assigned to that index of the array, ' a_donde_vas ', we place the path to which we are sending the execution flow of CodeIgniter . Now let's see an example of real routing:

$route['entradas'] = "empresa/tecnologia/5";

More examples Here

answered by 02.10.2017 / 16:22

In your routes file you can put the following

//ex.  lo_que_sea/#   => blog/id || blog/entrada/id
$route['(.+)'] = function ( $type = '' ){
                          $pos         = strpos($type, '/');

                          if( ! $pos){
                              $get_opt = substr_count($type, '/');
                              $opt     = explode('/', $type);

                                  case 1://only with blog/id
                                         $return = $opt[1];
                                  case 2://only with blog/entrada/id
                                         $return = 'entrada/' . $opt[2];
                              $return = '';

                          return 'blog/' . $retrun;

With this, what you do is that whatever the name is and the parameters passed by '/' will be formatted in the function and will be redirected in a transparent way to the browser to the'blog 'controller (to the'input' method) with the parameter'id ', you can put a prefix so that it does not affect you with the other pages like for example

//normal page
$route['faqs'] = 'public/home/faqs';
//blog page
$route['b-(.+)'] = //codigo previo
answered by 02.10.2017 в 16:18