How to route 2 parameters to a controller? - codeigniter

This seems really basic but i can't get the hang of it.
I'm trying to send more then one parameter to a method in the controller, like this :
http://localhost/ci/index.php/subjects/3/state
This is the routings i've tried :
$route['subjects/(:num)'] = 'subjects/view/$1';
$route['subjects/(:num)/{:any}'] = 'subjects/view/$1/$2';
the method accepted 2 paremeters :
public function view($slug, $id = null){
}
but i seem to get a 404. How can i get this to work? i need the view method to always accept 1 parameter and optional other parameters.
NOTE : I am including the url helper.

you have problem with your route brackets just change it from {} to () brackets will work
from
$route['subjects/(:num)/{:any}'] = 'subjects/view/$1/$2';
to
$route['subjects/(:num)/(:any)'] = 'subjects/view/$1/$2';

Always maintain your routing rules
like
$route['subjects/(:num)/(:any)/(:any)/(:any)'] = 'subjects/view/$1/$2/$3/$4';
$route['subjects/(:num)/(:any)/(:any)'] = 'subjects/view/$1/$2/$3';
$route['subjects/(:num)/(:any)'] = 'subjects/view/$1/$2';
always follow this pattern for routing
if you add like this
$route['subjects/(:num)/(:any)'] = 'subjects/view/$1/$2';
$route['subjects/(:num)/(:any)/(:any)/(:any)'] = 'subjects/view/$1/$2/$3/$4';
$route['subjects/(:num)/(:any)/(:any)'] = 'subjects/view/$1/$2/$3';
then always first condition will be true every time.
also refer this link --> codeigniter routing rules

I once tried this URI pattern
$route['(:any)'] = 'welcome/list1/$1';
$route['(:any)/(:num)'] = 'welcome/list1/$1/$2';
$route['(:any)/(:any)'] = 'welcome/list2/$1/$2';
$route['(:any)/(:any)/(:num)'] = 'welcome/list2/$1/$2/$3';
$route['(:any)/(:any)/(:any)'] = 'welcome/list3/$1/$2/$3';
but it didnt worked ... so I replaced it with regular expression
([a-z 0-9 -]+) replaced (:any)
and
([0-9]+) replaced (:num)
so it became
$route['([a-z 0-9 -]+)'] = 'welcome/list1/$1';
$route['([a-z 0-9 -]+)/([0-9]+)'] = 'welcome/list1/$1/$2';
$route['([a-z 0-9 -]+)/([a-z 0-9 -]+)'] = 'welcome/list2/$1/$2';
$route['([a-z 0-9 -]+)/([a-z 0-9 -]+)/([0-9]+)'] = 'welcome/list2/$1/$2/$3';
$route['([a-z 0-9 -]+)/([a-z 0-9 -]+)/([a-z 0-9 -]+)'] = 'welcome/list3/$1/$2/$3';
And it worked for me :)
In order to access the variables in your controllers, you can assign any parameter in the function.
class Welcome extends CI_Controller {
public function list($first, $second)
{
var_dump($first);
var_dump($second);
}
}

Related

Create title slug + random string Laravel 7

I would like to create a slug that combines the title + a random string in Laravel.
I tried this, but nothing, in the second case it does nothing but combine the character string with the title.
Str::slug(request('title'), '-', Str::random());
or
Str::slug(request('title'), Str::random());
I would like something like this:
this-is-an-example-title-Jfij4jio4523q234
Double-check the method signature from https://laravel.com/docs/7.x/helpers#method-str-slug (and deeper at https://github.com/laravel/framework/blob/7.x/src/Illuminate/Support/Str.php#L552)
To be explicit, the second parameter of the method is the character used to replace whitespace, and the third parameter refers to the locale to use when generating the slug. That means you need your string to be fully composed before passing it to the method.
Assuming you want your slug joined by a - then something like this is what you want:
$value = request('title') . ' ' . Str::random();
$slug = Str::slug($value); // optionally Str::slug($value, '-'); to explicitly define the join
Str::Slug() has been defined as:
slug(string $title, string $separator = '-', string $language = 'en')
The third param can be seen as language with default value: en.
Str::slug($request->input('title').Str::random(40), '-');
I hope this helps.

Route in Codeigniter to hide Class Method

Currently I have something like this
$route['ourworks/client'] = "ourworks/ourwork_item/client";
$route['ourworks/portfolio'] = "ourworks/ourwork_item/portfolio";
$route['ourworks/casestudy'] = "ourworks/ourwork_item/casestudy";
Is there anyway that I could make these 3 lines a a dynamic line?
Thank you
Should work:
$route['ourworks/([a-z]+)'] = "ourworks/ourwork_item/$1";
To allow the other permitted chars use this:
$route['ourworks/(:any)'] = "ourworks/ourwork_item/$1";

Format Output of Placeholder

I am creating a dynamic list of placeholders, some of the values held in these place holders are decimal numbers that are supposed to represent money.
What I'm wondering is if there is a way I can format them to display as such?
Something like [[+MoneyField:formatmoney]]
I see http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-(output-modifiers) but I do not see a way to do this here.
You most definitely can, under the header "Creating a Custom Output Modifier" on the link you posted it's described how you can place a snippet name as a output modifier. This snippet will recieve the [[+MoneyField]] value in a variable called $input.
So you'd have to create this custom snippet which could be as simple as
return '$'.number_format($input);
Another version of doing this is calling the snippet directly instead of as an output modifier like so:
[[your_custom_money_format_snippet ? input=`[[+MoneyField]]`]]
I'm not sure if theres any difference between the two in this case. Obviously you can pass any value into the number format snippet when calling it as a snippet instead of an output modifier. And i'm sure theres a microsecond of performance difference in the two but i'm afraid i don't know which one would win. ;)
Update:
Actually found the exact example you want to implement on this link;
http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-%28output-modifiers%29/custom-output-filter-examples
Snippet:
<?php
$number = floatval($input);
$optionsXpld = #explode('&', $options);
$optionsArray = array();
foreach ($optionsXpld as $xpld) {
$params = #explode('=', $xpld);
array_walk($params, create_function('&$v', '$v = trim($v);'));
if (isset($params[1])) {
$optionsArray[$params[0]] = $params[1];
} else {
$optionsArray[$params[0]] = '';
}
}
$decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : null;
$dec_point = isset($optionsArray['dec_point']) ? $optionsArray['dec_point'] : null;
$thousands_sep = isset($optionsArray['thousands_sep']) ? $optionsArray['thousands_sep'] : null;
$output = number_format($number, $decimals, $dec_point, $thousands_sep);
return $output;
Used as output modifier:
[[+price:numberformat=`&decimals=2&dec_point=,&thousands_sep=.`]]

Codeigniter problem with routes!

I am trying to do this route trick:
$route['cp/roles/:num'] = "cp/roles/index/:num";
but it doesn't work :(
please help me!!
advanced thanks .
According to the documentation on URI Routing:
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
“A URL with "product" as the first segment, and a number in the second will be remapped to the "catalog" class and the "product_lookup_by_id" method passing in the match as a variable to the function.”
So, for your particular instance, you would do the following:
$route['cp/roles/(:num)'] = "cp/roles/index/$1";
You could try
$route['cp/roles/:num'] = "cp/roles";
and then instead of passing a variable in your function you use
$this->uri->segment(3);
or the number that correspond to the segment.

Codeigniter: URI segments

How do I create an if statement saying something like this?
Basically, how do you use the URI class to determine if there is a value in any segment?
$segment = value_of_any_segment;
if($segment == 1{
do stuff
}
I know this is pretty elementary, but I don't totally understand the URI class...
Your question is a little unclear to me, but I'll try to help. Are you wondering how to determine if a particular segment exists or if it contains a specific value?
As you are probably aware, you can use the URI class to access the specific URI segments. Using yoursite.com/blog/article/123 as an example, blog is the 1st segment, article is the 2nd segment, and 123 is the 3rd segment. You access each using $this->uri->segment(n)
You then can construct if statements like this:
// if segment 2 exists ("articles" in the above example), do stuff
if ($this->uri->segment(2)) {
// do stuff
}
// if segment 3 ("123" in the above example) is equal to some value, do stuff
if ($this->uri->segment(3) == $myValue) {
// do stuff
}
Hope that helps! Let me know if not and I can elaborate or provide additional information.
Edit:
If you need to determine if a particular string appears in any segment of the URI, you can do something like this:
// get the entire URI (using our example above, this is "/blog/article/123")
$myURI = $this->uri->uri_string()
// the string we want to check the URI for
$myString = "article";
// use strpos() to search the entire URI for $myString
// also, notice we're using the "!==" operator here; see note below
if (strpos($myURI, $myString) !== FALSE) {
// "article" exists in the URI
} else {
// "article" does not exist in the URI
}
A note regarding strpos() (from the PHP documentation):
This function may return Boolean
FALSE, but may also return a
non-Boolean value which evaluates to
FALSE, such as 0 or "". Please read
the section on Booleans for more
information. Use the === operator for
testing the return value of this
function.
I hope my edit helps. Let me know if I can elaborate.

Resources