Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter: Passing variables via URL - alternatives to using GET

I'm new to CodeIgniter and have just discovered the difficulties using the GET method of passing variables via the URL (e.g. domain.com/page.php?var1=1&var2=2).

I gather that one approach is to pass the variables in the URI segments but haven't quite figured out how to do that yet as it seems to create the expectation of having a function in the controller named as the specific URI segment????

Anyway Instead of using GET I've decided to use POST by adapting a submit button (disguised as a link) with the variables in hidden input fields. I've created the following solution which seems to work fine, but am wondering whether I'm on the right track here or whether there is an easier way of passing variables via a link within CodeIgniter?

I've created the following class in application/libraries/

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_variables {

function variables_via_link($action, $link_text, $style, $link_data) {
    $attributes = array('style' => 'margin:0; padding:0; display: inline;');
    echo form_open($action, $attributes);
    $attributes = array('class' => $style, 'name' => 'link');
    echo form_submit($attributes, $link_text);
    foreach ($link_data as $key => $value){
        echo form_hidden($key, $value);
    }
    echo form_close();
 }
}
?>

With the following CSS:

/* 
SUBMIT BUTTON AS LINK
adapted from thread: http://forums.digitalpoint.com/showthread.php?t=403667
Cross browser support (apparently).
*/
.submit_as_link {
background: transparent;
border-top: 0;
border-right: 0;
border-bottom: 1px solid #00F;
border-left: 0;
color: #00F;
display: inline;
margin: 0;
padding: 0;
    cursor: hand /* Added to show hand when hovering */
}

*:first-child+html .submit_as_link {  /* hack needed for IE 7 */
border-bottom: 0;
text-decoration: underline;
}

* html .submit_as_link {    /* hack needed for IE 5/6 */
border-bottom: 0;
text-decoration: underline;
}

Link then created using the following code in the VIEW:

<?php
$link = new C_variables;
$link_data=array('var1' => 1, 'var2' => 2);
$link ->variables_via_link('destination_page', 'here is a link!', 
'submit_as_link', $link_data);
?>

Thanks for your help...

like image 588
John Durrant Avatar asked Apr 28 '10 11:04

John Durrant


People also ask

How can we pass value to url in Codeigniter 4?

In CodeIgniter 4 application, there are several types of placeholder available to pass value to URL. Parameters as like of type Integer value, String value, Hashed value, Alpna numeric etc. Let’s take an example to see How can we pass and access value to controller? Open Routes.php file from /app/Config folder. Here, we have configured few routes.

What is the use of custom routes in CodeIgniter?

Routes are responsible for responding to URL requests. Routing matches the URL to the pre-defined routes. If no route match is found then, CodeIgniter throws a page not found an exception. Routes in CodeIgniter are defined using the below formula: Controller -is mapped to the controller name that should respond to the URL.

What happens if no CodeIgniter route match is found?

If no CodeIgniter Route match is found then, CodeIgniter throws a page not found an exception. CI Routing is responsible for responding to URL requests. Routing matches the URL to the pre-defined routes. Controllers glue the models and views together.

What is the difference between method and controller in CodeIgniter?

Controller -is mapped to the controller name that should respond to the URL. Method – is mapped to the method in the controller that should respond to the URI request. Parameter – this section is optional. In this CodeIgniter Routes tutorial, you will learn: What are CodeIgniter Routes?


1 Answers

To be honest, creating a form to perform the job of hyperlinks is a bit of a semantic no no.

Codeigniter by default completely strips any $_GET parameters. So without enabling query strings in the config, you can't do the following:

http://my-domain.com/script/?param=1&param2=foo

For a beginner, segment based URLs are a bit of a learning curve, but soon make sense. A good production example of how segment based URLs work in practice is Stack Overflow!

So if you wanted to copy Stack Overflow's question view page with the following URL in codeigniter:

eg; http://stackoverflow.com/questions/2728978/codeigniter-passing-variables-via-url-alternatives-to-using-get

In your default controller create the following method:

public function questions()
{
  $question_id = $this->uri->segment(2);

  // now do something with our question_id
}

The third segment (question title slug) is effectively ignored. But you could grab it with the following:

$question_title = $this->uri->segment(3);

More information here.

If you don't like the idea of having to name a method in your controller with the first URI segment. You could create a custom route in your routes configuration.

So, imagine you create a controller called questions_controller.php, and have a method called show_question_by_id(). To keep the /questions/1234/some-text-here style URI but handle it with the controller/method above, you could create the following route:

$route['question/(:num)'] = "questions_controller/show_question_by_id/$1";

More information here.

If you wish to have an infinite number of parameters in your URL, or don't know what parameters to expect e.g. mysite.com/my_page/param1/12/param2/foo/param3/bar/param4/baz/another-param/xyz-123

You can split these into an associative array using the $this->uri->uri_to_assoc(1) URI method to get the following:

  [array]
(
    'param1' => '12'
    'param2' => 'foo'
    'param3' => 'bar'
    'param3' => 'baz'
    'another-param' => 'xyz-123'
)

You could then handle this exactly as if you were using the $_GET array. You can then combine this approach, with custom routes to give you practically any URI and application structure you like. You also get the benefit that each parameter and segment has been automatically cleaned up. It's a bit of a learning curve, and can seem like extra work to begin with, but is actually pretty flexible and helps you to build a well structured application.

like image 122
rbaker86 Avatar answered Oct 11 '22 19:10

rbaker86