How does posting query parameters in CodeIgniter work? - php

I have this format on links:
blah/link/11
where blah is the controller and link is a function inside it. But now I want to send a number in the querystring. In normal non-MVC way I would have done like this:
page.php?id=11
So what should I do for getting the eleven in my link function?

class Blah extends Controller {
function link( $id ) {
// $id == 11
}
}
reachable via URL blah/link/11

There may be other ways to go about this, but it looks like CodeIgniter has a URI Class that will allow you to retrieve specific segments of your URI. So something like
$id = $this->uri->segment(3); //from a controller, I assume
should get you what you want.
It also looks like CodeIgniter will take additional URI parameters and pass them through as parameters to your action function.
#http://example.com/index.php/products/shoes/sandals/123
class Products extends Controller {
function shoes($sandals, $id)
{
echo $sandals;
echo $id;
}
}

Related

fetching parameter in a link php codeigniter

Say I have this url
http://localhost/newtkt/index.php/welcome/gettkt/PfIfiETYXzUpYRJf6RPvyncN4PgdN3
and a controller function
public function gettkt()
{
//mydata
}
how can i retrieve the parameter PfIfiETYXzUpYRJf6RPvyncN4PgdN3 that comes with the url and make it a variable in my controller function gettkt() so that i can use the parameter within the function like gettkt(thisparameter). I hope I've structured my question in the best way possible. thank you.
You can do two things
Method 01
public function gettkt($parm)
{
echo $parm; # Prints PfIfiETYXzUpYRJf6RPvyncN4PgdN3
}
Method 02
Use URI Class in CodeIgniter
$parm = $this->uri->segment(n); # Number point your parameter
$parm = $this->uri->segment(3); # Prints PfIfiETYXzUpYRJf6RPvyncN4PgdN3
You can use
$this->uri->segment('3'); //where 3 is for 3rd param
for more information visit URI

How to fix CodeIgniter routes

I am new in framework please help to solve this. I am working in CodeIgniter. I want to have clean URLs like this: example.com/index.php/6/title
index.php/6 is a post ID and title is a Page title.
I want to get data by post ID.
Controller function:
public function watch(){
echo id;
}
How to get this clean URL using routes. I am trying this route but it's not working:
$route['(:num)/(:any)'] = 'front_controller/watch/';
Try this for routing to watch() function in Front_controller with 2 parameters id and title
$route['(:num)/(:any)'] = 'front_controller/watch/$1/$2';
Also you should accept 2 paramets in your controllers function too as :
function watch($id,$title)
{
echo $id;
echo $title;
}
Let me know in case of any queries.

How to send data/variable from controller to other controller in codeigniter without using session?

How could I send data/variable from controller to other controller in codeigniter without using session?
I know how to send data from controller to other controller using session but I don't want to use it as it's giving me problem. If I use session, data I need to send will be used in many pages.
Example of the Data from controller 1 is $id (which I want to use on the other controller), but in the controller 2. If I open many pages, I only get the same data which is not what I expected. In controller 2, if I'm in page 1 I need $id = 1 while if I'm in page 2 I need the $id = 2. Any help with me appreciated. Thanks in advance.
GET -- It's your choice ?
if yes then.. use URI Class
http://example.com/index.php/news/local/1
segment(1) = "news"
segment(2) = "local"
segment(3) = "1" <-- your $id
Controller
$id = $this->uri->segment(3);
test.php Controller File :
Class Test {
function demo() {
echo "Hello";
}
}
test1.php Controller File :
Class Test1 {
function demo2() {
require('test.php');
$test = new Test();
$test->demo();
}
}
But using require like this in codeigniter is not a good idea...

Passing parameters in CodeIgniter

I have been banging my head against this problem for about an hour now. I have looked high and low but nothing is working for me. This should be simple and I am sure it is.
I am trying to pass some parameters in CodeIgniter to a URL and nothing seems to be working. Here is my controller:
class Form_controller extends CI_Controller {
public function change($key = NULL) {
if (is_null($key)) {
redirect("reset");
} else {
echo "Hello world!";
}
}
}
Here is my route:
$route['change/(:any)'] = "form_controller/change/$1";
Every time I visit /index.php/change/hello I get the string "Hello world!" but when I visit /index.php/change I got a 404 not found.
What I am trying to do is pass a parameter to my controller for the purposes of checking the DB for a specific key and then acting upon it. If the key does not exist in the DB then I need to redirect them somewhere else.
Any thoughts on this?
Never mind, I figured it out. I ended up making two different routes to handle them, like so:
$route['change'] = "form_controller/change";
$route['change/(:any)'] = "form_controller/change/$1";
And the function in the controller looks like this now:
public function change($key = NULL) {
if (is_null($key)) {
redirect("reset");
} else if ($this->form_model->checkKey($key)) {
$this->load->view("templates/gateway_header");
$this->load->view("forms/change");
$this->load->view("templates/gateway_footer");
} else {
redirect("reset");
}
}
If anyone has a better solution, I am all ears. This worked for me though.
This might help you out
public function change() {
$key = $this->uri->segment(3);
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
This allows you to grab the segment easily using the CI url helper
index.php/change/(3RD Segment) this part will go into that variable $key.
This may not be what you are looking for but it is very useful if you are trying to pass two variables or more because you can just grab the segment from the url and store it into the variable

CodeIgniter Restserver doesn't work with specific URL's

I am using this Restserver in combination with CodeIgniter.
It seems to work pretty well except when I use URL's like these;
mydomain.com/api/example/1234/
wherein 1234 is the ID I'm requesting.
Code like this doesn't seem to work:
class Example extends REST_Controller {
public function index_get() {
print($this->get("example"));
}
}
It doesn't seem to matter whether it is a GET or POST request. There must be a way I can just retrieve the ID from the URL..
The URL's segments should equal to these:
api = Controller
example = Resource
Parameters must be in key-value pairs:
id = Key
1234 = Value
It seems that your 1234 is treaded as a key, but with no value for it. Try to change your URL to the following: mydomain.com/api/example/id/1234/, which would translate to: mydomain.com/controller/resource/key/value/
There is also a very detailed tutorial here: http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/
EDIT:
Since your controller is in a sub-folder, your URL's segments should be constructed like this:
api/example = Controller
user = Resource // basically the method name + the http method e.g. user_get(), or user_post(). I just made 'user' up, for your app can be whatever it is that people will access via your api
Parameters must be provided as key-value pairs:
id = Key
1234 = Value
So then your URL would look like this: mydomain.com/api/example/user/id/1234/
in the case of a GET REQUEST you just define the parameters in the header of the function like this:
public function index_get($param) {
print($param);
}
if you want to make it optional then:
public function index_get($param="") {
if ($param=="") {
print("No Parameters!");
} else {
print($param);
}
}
If I want to send "POST" parameters, I just create a POST METHOD and receive them as such...
public function index_post() {
$params = $this->post();
if (isset($param['id'])) {
print($param['id']);
} else {
print("No Parameters!");
}
}

Categories