CodeIgniter: Passing variables via URL - alternatives to using GET -
i'm new codeigniter , have discovered difficulties using method of passing variables via url (e.g. domain.com/page.php?var1=1&var2=2).
i gather 1 approach pass variables in uri segments haven't quite figured out how yet seems create expectation of having function in controller named specific uri segment????
anyway instead of using i've decided use post adapting submit button (disguised link) variables in hidden input fields. i've created following solution seems work fine, wondering whether i'm on right track here or whether there easier way of passing variables via link within codeigniter?
i've created 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 $key => $value){ echo form_hidden($key, $value); } echo form_close(); } } ?>
with following css:
/* submit button link adapted 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 show hand when hovering */ } *:first-child+html .submit_as_link { /* hack needed ie 7 */ border-bottom: 0; text-decoration: underline; } * html .submit_as_link { /* hack needed ie 5/6 */ border-bottom: 0; text-decoration: underline; }
link created using following code in view:
<?php $link = new c_variables; $link_data=array('var1' => 1, 'var2' => 2); $link ->variables_via_link('destination_page', 'here link!', 'submit_as_link', $link_data); ?>
thanks help...
to honest, creating form perform job of hyperlinks bit of semantic no no.
codeigniter default strips $_get parameters. without enabling query strings in config, can't following:
http://my-domain.com/script/?param=1¶m2=foo
for beginner, segment based urls bit of learning curve, make sense. production example of how segment based urls work in practice stack overflow!
so if wanted copy stack overflow's question view page following url in codeigniter:
http://stackoverflow.com/questions/2728978/codeigniter-passing-variables-via-url-alternatives-to-using-get
in default controller create following method:
public function questions() { $question_id = $this->uri->segment(2); // our question_id }
the third segment (question title slug) ignored. grab following:
$question_title = $this->uri->segment(3);
more information here: http://ellislab.com/codeigniter/user-guide/libraries/uri.html
if don't idea of having name method in controller first uri segment. create custom route in routes configuration.
so, imagine create controller called questions_controller.php
, , have method called show_question_by_id()
. keep /questions/1234/some-text-here
style uri handle controller/method above, create following route:
$route['question/(:num)'] = "questions_controller/show_question_by_id/$1";
more information here: http://ellislab.com/codeigniter/user-guide/general/routing.html
if wish have infinite number of parameters in url, or don't know parameters expect e.g. mysite.com/my_page/param1/12/param2/foo/param3/bar/param4/baz/another-param/xyz-123
you can split these associative array using $this->uri->uri_to_assoc(1)
uri method following:
[array] ( 'param1' => '12' 'param2' => 'foo' 'param3' => 'bar' 'param3' => 'baz' 'another-param' => 'xyz-123' )
you handle if using $_get array. can combine approach, custom routes give practically uri , application structure like. benefit each parameter , segment has been automatically cleaned up. it's bit of learning curve, , can seem work begin with, pretty flexible , helps build structured application.
Comments
Post a Comment