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
Related
I have the following route in my routes.web.php. Looks like this...
Route::get('/spielerAuswahl/{somevar}', 'SpielplanController#getHeimGast');
In the variable {somevar} I have for example Nr1=111&Nr2=222. The route works fine in a get to SpielplanController...
public function getHeimGast(){
$var = $somevar;
return view('test')->with('variableControllerSomevar', $var);
}
In this function I want to put the Nr1=111&Nr2=222 in the $var and make then an easy output of this variable in the view. How to get that?
Though the answer come late, but you can put Nr1=111&Nr2=222 as uri with proper encoding. For javascript, encodeURIComponent can be used to escape certain characters to form a valid URI:
encodeURIComponent('Nr1=111&Nr2=222');
// output: Nr1%3D111%26Nr2%3D222
To request server, using this url:
http://example.com/spielerAuswahl/Nr1%3D111%26Nr2%3D222
The controller function then receives correct form of variable as follows:
public function getHeimGast ($somevar) {
$somevar // = Nr1=111&Nr2=222
}
Only URI segments can be map as route variables in Laravel, no query string variables.
You can simply fetch those like these:
public function getHeimGast(){
$vars = request()->all();
return view('test', $vars);
}
In your test blade, you can use those query vars, eg( Nr1=111&Nr2=222 ) as:
$Nr1, $Nr2 ... and so on...
NOTE: given a query like this: /spielerAuswahl?Nr1=111&Nr2=222
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!");
}
}
I want to know how can I create routes for a controller function which needs an argument to be passed.
Like if I create a function like this:
function abc($arg)
{
return $arg;
}
Then how can I specify a route for the same function? My argument will be containing a mixture of alphabets and numbers
Add this to your APP_PATH/application/config/routes.php
$route['controller_name/abc/([A-Za-z0-9])+'] = "controller_name/abc/$1";
for more details http://codeigniter.com/user_guide/general/routing.html
use the uri class and try reading the segments passed to your method
in the controller method below you pass the url segments such as
controller / function / argument1 / argument2 / argument3 / 4 / 5 / 6 / ..
// controller
public function myfunction(){
var_dump($this->url->segment(1)); // route/abc -> abc
var_dump($this->url->segment(2)); // route/abc/123 -> 123
}
if the function take fixed length of parameters then something like
public function myfunction($arg1,$arg2){
// ..
}
you can work the route mapping to make this work,
$route["myfunction/(any)"= "remap" // ..
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;
}
}
I'm trying to write a function in a CodeIgniter controller which can take optional parameters. However, I always get Missing Argument warnings. I'm not trying to suppress the warnings - I'm trying to declare the parameters as optional (maybe they could be empty strings if they don't exist, or something).
What is it I'm missing?
Thanks
Mala
public function my_optional_test($not_optional_param, $optional_param = NULL)
{ $this->stuff(); }
have you tried this?
For example, let’s say you have a URI like this:
example.com/index.php/mycontroller/myfunction/hello/world
example.com/index.php/mycontroller/myfunction/hello
Your method will be passed URI segments 3 and 4 (“hello” and “world”):
class MyController extends CI_Controller {
public function myFunction($notOptional, $optional = NULL)
{
echo $notOptional; // will return 'hello'.
echo $optional; // will return 'world' using the 1st URI and 'NULL' using the 2nd.
}
}
Reference: https://codeigniter.com/user_guide/general/controllers.html