How to get CodeIgniter to accept a query string url?

According to the CI docs, CodeIgniter uses a segmented approach , for example:

example.com/my/group 

If I want to find a specific group (id = 5), I can visit

 example.com/my/group/5 

And in the controller define

 function group($id='') { ... } 

Now I want to use the traditional approach, which CI calls the URL of the query string. Example:

 example.com/my/group?id=5 

If I go directly to this URL, I get a 404 page . So how can I enable this?

+4
source share
9 answers

For reliable use of query strings, I found that you need to do 3 things

  • In application/config/config.php set $config['enable_query_strings'] = true;
  • Again, in application/config/config.php set $config['uri_protocol'] = "PATH_INFO";
  • Change your .htaccess to remove? (if present) in the rewrite rule

I use the following

 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] 
+9
source
 //Add this method to your (base) controller : protected function getQueryStringParams() { parse_str($_SERVER['QUERY_STRING'], $params); return $params; } // Example : instagram callback action public function callback() { $params = $this->getQueryStringParams(); $code = !empty($params['code']) ? $params['code'] : ''; if (!empty($code)) { $auth_response = $this->instagram_api->authorize($code); // .... } // .... handle error } 
+3
source

It may help some people; put this in your controller constructor to re-populate the controller-based $ _GET behind the controller (for example, if you are integrating a third-party library that relies on $ _GET - for example, most PHP OAuth libraries).

 parse_str(str_replace($_SERVER['QUERY_STRING'],'',$_SERVER['REQUEST_URI']),$_GET); 
+1
source

Html:

 <a href="?accept=1" class="btn btn-sm btn-success">Accept</a> 

Controller function

 if ($this->input->get('accept')!='') { $id = $this->input->get('accept', TRUE ); $this->camprequests->accept($id); redirect('controllername/functionname'); } 

Model function

 public function accept($id) { $data = array('status'=>'1'); $this->db->where('id','1'); if($this->db->update('tablename',$data)) { $this->session->set_flashdata("accpeted","<div class='col-sm-12 alert alert-success'>Accpeted successfully.</div>"); } else { $this->session->set_flashdata("accpeted","<div class='col-sm-12 alert alert-success'>Error..</div>"); } } 
+1
source

Change application/config.php in the line:

 $config['enable_query_strings'] = FALSE; 

Make it true. There are other details that you should pay attention to. See here .

0
source

After setting $config['enable_query_strings'] = TRUE; in config.php you can use a segment-based approach in conjunction with query strings , but only if you use two or more variables (separated by the "&" character) in the query string , for example:

 example.com/my/group?id=5&var=something 

See this answer for more information.

0
source

CodeIgniter additionally supports this feature, which can be included in your application / config.php file. If you open your configuration file, you will see the following items:

 enter code here $config['enable_query_strings'] = FALSE; 

$ config ['controller_trigger'] = 'c'; $ config ['function_trigger'] = 'm';

If you change the enable_query_strings to TRUE, this function will become active.

0
source

You can change the URI PROTOCOL in config file to

  $config['uri_protocol'] = "ORIG_PATH_INFO"; 

and

  $config['enable_query_strings'] = FALSE; 

It will take query strings and resolve your urls. Worked for me :)

0
source

It is truly verified and verified.

It works with any method you like; giving you the freedom to mix match query string and / segment (unlike previous answers)

which you want to use:

 example.com/my/group/?id=5 

(pay attention to trailing / earlier?). OR

  example.com/my/group/5 

(depending on your url template definitions in the router file). OR EVEN

 example.com/index.php/?my/group/?id=5 

(although it looks uncomfortable)

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

and in the codigniter config / config.php file set

 $config['uri_protocol'] = 'AUTO'; $config['enable_query_strings'] = TRUE; 
0
source

Source: https://habr.com/ru/post/1310694/


All Articles