I am passing an id containing / eg: 171/CR/EOW1/14 in the link.
It is showing correctly, but in the controller function it is taking only the first letters before the slash. eg: 171
How do I solve this problem?
Your question is incredibly vague. But for the purposes of this, I'll assume that you want to pass the whole string 171/CR/EOW1/14. Not parts of the string as different params.
you are using an un-escaped slash. So codeigniters' routing thinks the parts of the url after the 171 are more parameters in the route string.
if you want to pass a url, use urlencode() and then urldecode() to handle the slashes in the string you want to pass.
Or use addslashes().
addslahes()
urlencode()
You can use uri_segment, which should help.
http://example.com/index.php/controller/action/1stsegment/2ndsegment
it will return
$this->uri->segment(1); // controller
$this->uri->segment(2); // action
$this->uri->segment(3); // 1stsegment
$this->uri->segment(4); // 2ndsegment
Passing URI Segments to your methods in codeigniter visit codeIgniter docs
If your URI contains more than two segments they will be passed to your method as parameters.
For example, let’s say you have a URI like this:
example.com/index.php/products/shoes/sandals/123
Your method will be passed URI segments 3 and 4 (“sandals” and “123”):
<?php
class Products extends CI_Controller {
public function shoes($sandals, $id)
{
echo $sandals;
echo $id;
}
}
In PHP 5.6 you can retrieve as a variable argument list which can be specified with the ... (spread) operator
function do_something($first, ...$all_the_others)
{
var_dump($first);
var_dump($all_the_others);
}
or if you are using a lesser version you have to specify separate arguments variables
function do_something($first, $second, $third)
{
var_dump($first);
var_dump($second);
var_dump($third);
}
EDIT:
You can route the url to this function like
$route['products/(:any)'] = 'catalog/do_something';
Please check the documentation for more details about url routing
You can you use a question mark like:
?first=171&second=CR
For more information, see e.g. http://html.net/tutorials/php/lesson10.php
Related
If I have a URL like https://example.com/controller/action?customer-id=7414 how do I get customer-id in my action parameters? Since a dash is not allowed in variables names I cannot do the following!
public function actionContact($customer-id) { //syntax error! :)
// ...
}
Documentation is usually excellent but on this exact point it's just silent. How do I solve this?
When yii\web\Controller calls action it only binds parameters which names match exactly. Dash cannot be used in variable name in PHP so there is no way it will ever match like that.
If you really want to have param in URL as customer-id=XXX then easiest thing you can do, is skip param in action method definition and get it during action itself:
public function actionContact() {
$customerId = $this->request->get('customer-id');
// or
$customerId = \Yii::$app->request->get('customer-id');
// ...
}
how to remove the mark on the picture below? this url to create blog details. I use this php codeigniter language
here's the picture:
If you supposed to remove the string from URL, this may help you.
Make the URL as an array by parsing it with parse_url().
Extract the portion of your string and then decompose it with parse_str().
Remove the parameter with the use of unset.
Now build the your URL with http_build_query().
This will give you the URL which you supposed to do.
Other way:
If you want to go with that URL by optimizing it, use routes functionality of CodeIgniter. Check this below.
Path: application/config/routes.php/
$route['your_url_string'] = 'blog/detail';
add like this
In application/config/ruotes.php
$route['(.*)'] = 'YourController/YourMethod'; // example 'blog/detail'
In Your Controller - I guess Blog is a controller and detail is the method
class Blog extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function detail(){
$id = $this->uri->segment(1); // return 8
$slug = $this->uri->segment(2); // return 77-anniversary
}
}
In View
Title
I've created a filter method for filtering the products list. This is my URL:
localhost/myshop/products/filter?category=shirts&color=blue&page=1
But I want to show this way:
localhost/myshop/products/shirts/blue/1
How can I achieve it?
Assuming that Products::filter() is responsible for handling the request, you can rewrite the method to accept parameters in its signature. So, if the current logic is something like this:
class Products extends CI_Controller
{
public function filter()
{
// Retrieve data from GET params
$page = $this->input->get('page');
$color = $this->input->get('color');
$category = $this->input->get('category');
// Do the filtering with $category, $color and $page...
}
}
You can simply refactor it to accept parameters through URL segments:
public function filter($category, $color, $page)
{
// Do the filtering with $category, $color and $page...
}
With this in place, your current URL is:
localhost/myshop/products/filter/shirts/blue/1
We need to get rid of that extra filter/ and we're done, right? Quoting from the docs:
Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
example.com/class/method/param1/param2
In some instances, however, you may want to remap this relationship so that a different class/method can be called instead of the one corresponding to the URL.
OK, so we need to remap the current route. You have a few options:
First, is to update your application/config/routes.php file with a new entry:
$route['products/(:any)'] = 'products/filter/$1';
It says that if a URL starts with products/, remap it to the filter method of products class.
Here you can use wildcards and regex patterns to be even more precise about the type of parameters your method accepts.
Another option is that you might want to implement a _remap() method in your controller in order to do the route remapping for you.
in routes.php file, you can write following line
$route['products/(:any)/(:any)/(:num)'] = 'products/filter/$1/$2/$3';
and function will be like following
public function filter($category, $color, $page)
{
echo $category.'<br>';
echo $color.'<br>';
echo $page.'<br>';
}
I have URL like this: http://localhost/sitename/some-post-title/code=24639204963309423
Now I have one findUser function in my controller file
public function findUser() {
// I have tried with $_GET['code']
}
and I am trying to get code variable value inside this function. I have tried with $_GET['code'] but did not worked.
Any Idea how to get value inside controller function?
Thanks.
Are you trying to get a path segment variable or a GET variable? It looks like you're going for a bit of both.
Natively in CI, you can use $this->input->get if you update your url to look more like
http://localhost/sitename/some-post-title/?code=24639204963309423
(Note the question mark).
Alternatively, you can modify your URL to look like this
http://localhost/sitename/some-post-title/code/24639204963309423
And then use URI segments like so
$data = $this->uri->uri_to_assoc();
$code = $data['code'];
If you do not want to change your URL, you will have to break that string up manually like so
$data = $this->uri->segment(3);
$data = explode($data, '=');
$code = $data[1];
I would argue the second option is the most SEO-friendly and pretty solution. But each of these should be functionally identical.
If your URI contains more then two segments they will be passed to your function as parameters.
For example, lets say you have a URI like this:
example.com/index.php/products/shoes/sandals/123
Your function will be passed URI segments 3 and 4 ("sandals" and "123"):
<?php
class Products extends CI_Controller {
public function shoes($sandals, $id)
{
echo $sandals;
echo $id;
}
}
?>
If you are using GET to get parameters, you can do like this:
$this->input->get('get_parameter_name');
Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
example.com/class/function/id/
More details for Controllers find here and for GET find here
So I have a page:
http://www.mysite.com/controller/function
The function is defined in the controller as:
function ()
{
//some stuff here
}
However it is possible to resolve the URL:
http://www.mysite.com/controller/function/blablabla
i.e. "blablabla" can be passed to the function and forms an additional URI segment, but still brings up the same page. I have the same issue with a number of controllers / functions - how do I prevent parameters being passed to the function (or appearing as a URI segment)?
I've been working with Codeigniter and PHP for around 6 months very part time, so forgive me if the answer is obvious but my searches haven't been fruitful on this.
My goal is optimised SEO - unsure whether better to redirect the page with the extra URI segment to the correct page or to the 404 page.
You can't prevent that without changing how CI handles URI parsing.
You could force a redirect like so:
function my_happy_function($redirect=null) {
if($redirect) {
redirect('/controller/my_happy_function/');
}
}
That would strip out any variables that are given in the URI, at the cost of a page redirect.
Sounds like you want a generic catch all for pages. You can do this using routes.
For example:
$route['my_happy_function(/:any)*'] = "my_happy_function";
then in your my_happy_function index method you check the URI segments there...
public function index()
{
$something = $this->uri->segment(1);
$something_else = $this->uri->segment(2);
// etc
}
this way all calls to my_happy_function get pushed to the index method...
wait, did I understand your question correctly? If I missed the point let me know and I can update.