optional parameters in CodeIgniter - php

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

Related

Laravel web route variable is not returning as supposed

I have the following route :
Route::get('/web/{cat_id?}/{post_type}','WebPostsController#index')
->where('post_type', '(pictures|videos|links|companies)')
->name('post_index');
Controller file :
public function index($post_type,$post_id = null,$cat_id = null)
{
dd($post_type);
}
When access the URL /public/web/15/videos
It return '15' which is supposed to return {post_type} value (videos) while it returning the {cat_id?} value.
The variables passed in to the action have to follow the definition in the route.
That is, in your case, you expect the cat_id first, then the post_type which means that you will have to define the method as:
public function index($cat_id, $post_type) { }
Furthermore, I expect that you will have some issues with it still, cause you can not have an optional parameter and then a forced parameter. So either move the $cat_id param as a optional to the end of the route and use $post_type before (/public/web/videos/15) or make both forced.

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

adding default parameters of the function as result of other function

Hi I have question it is possible to adding default parameters of the function as result of other function? For example:
static function addParameter(){
return rand(10,100)
}
function doSomething($year=$this->addParameter()) or
function doSomething($year=class::addParameter())
I need to pass actual year and month to my function. When i pass
function($month=3, $year=2016)
it work then but i not want to write this from hand but want to use function or something to always return actual month and year.
Short answer: no, you can't.
http://php.net/manual/en/functions.arguments.php states that:
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
You can use default null value (if normally function does not take one) and check if parameter is null, then get value from function:
function testfun($testparam = null) {
if ($testparam == null) $testparam = myclass::funToReturnParam();
// Rest of function body
}
Yo can implement it like this
<?php
class Core {
static function addParameter(){
return rand(10,100);
}
}
$core = new Core();
function doSomething($rand){
echo 'this is rand '.$rand;
}
doSomething(Core::addParameter());
You can change the function defination according to your requirement.
hope it helps :)
From the PHP Manual (emphasis mine):
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
So only scalar types(boolean, int, string etc), arrays and null are allowed.
A workaround could be to check if the parameter is null and set it within the function:
function doSomething($year = null) {
if (!$year) {
$year = addParameter();
}
//actually do something
}

how can I specify the given parameter in codeigniter

I have a controller function with 3 argument.Default parameter is set for all of them
When calling this function if i have to give the 3rd parameter only how can i specify i am passing the 3rd parameter
public function manage_class($msg="",$id=0,$class_type="all")
i want to call this function only by the 3rd parameter
I think that if you pass null to the 1st and 2nd parameter, it will grab the default values
like:
manage_class(null,null,3rd_value)
Method would be to first set parameters with possible change of default value, and then strict defaults:
public function manage_class($class_type="all", $msg="", $id=0). Maybe I've got you wrong, or something.
And by the way, if you need to pass only 1 parameter, why you passing other two as arguments?
Well in that case you could define the function manage_class like i said above but then with an array:
manage_class($args = array())
{
//do something with this
echo $args['third'];
}
and then you can cal manage_class with an url like this (sitename/call_manage_class/null/null/foo
call_manage_class($first, $second, $third)
{
if($first == null) {
$first = "";
}
//etc
manage_class(array('first'=>$third
'second'=>$second
'third'=>$third));
}

How to create routes for a controller function which needs argument in codeigniter

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" // ..

Categories