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
Related
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
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 have a problem with Laravel 5, and to be precise, I can't find the solution for it.
In C# (ASP.NET MVC) it's easy to solve.
For example, I have these routes (I'll just type the route content, and the function header, for the sake of simplicity)
/{category}/Page{page}
/Page{page}
/{category}
The function is defined inside Product controller.
function header looks like this:
public function list($page = 1, $category = null)
the problem is, whenever I enter just one argument, it doesn't send the value for the parameter by the name I set in the route, but rather, it pushes values by function parameter order.
So, when I open /Page1, it works properly, value of 1 is sent to $page variable,
but when I access /Golf(made up on the spot), it also sends the value to the $page variable.
Any possible idea how to avoid this, or do I really need to make different functions to handle these cases?
In C#, it properly sends the value, and keeps the default value for undefined parameter.
Hope you have an answer for me.
Thank you in advance and have a nice day :)
So, as you've seen the parameters are passed to the function in order, not by name.
To achieve what you want, you can access these route parameters from within your function by type hinting the request object to it like this:
class ProductController extends Controller
{
function list(Request $request){ # <----------- don't pass the params, just the request object
$page = $request->route('page'); # <--- Then access by name
$category = $request->route('category');
dd("Page: $page | Category: $category");
}
}
Then of course you would set all 3 of your routes to hit that same controller method:
Route::get('/{category}/Page{page}', 'ProductController#list');
Route::get('/Page{page}', 'ProductController#list');
Route::get('/{category}', 'ProductController#list');
Hope this helps..!
if you want to get the parameters in your controller, you can use this:
public function list() {
$params = $this->getRouter()->getCurrentRoute()->parameters();
}
for /aaa/Page3, the $params would be array(category => 'aaa', page => '3')
for /Page3, the $params would be array(page => '3')
for /aaa, the $params would be array(category => 'aaa')
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 am using the latest version of Yii and trying to basically collate the URL parameters.
For instance if my URL was as follows:
site.com/events/199/111
What is the function to grab the first parameter e.g '199' and similarly again to say grab the 2nd parameter e.g '111'.
I remember CodeIgniter has $this->url->segment(1), I basically need the same functionality to Yii :)
On somthing like -
$this->createUrl('blah/blahx',array('id'=>2));
You can get your parameter using -
$x = CHttpRequest::getParam('id'); //outputs x=2
OR $x = Yii::app()->getRequest()->getQuery('id'); //x=2 again
you can try getParam method
$id = Yii::app()->request->getParam('id');
You should read this :
http://www.yiiframework.com/doc/guide/1.1/en/topics.url
http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action-parameter-binding
About your problem, you should simply create a corresponding url rule, e.g. :
'events/<param1>/<param2>'=>'events/view',
And then in your controller :
public function actionView($param1, $param2)
{
// you can now use $param1 and $param2
}
You just need uri component.
Here is the solution , you can use uri component like this ; Yii::app()->uri->segment(2);
for details follow url
http://www.hasandemir.com/how-to-get-contoller-action-segments-like-codeigniter/