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/
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
In my codeigniter project i want to use two character with wildcard any
$route['(school*)/(:any)'] = 'Schools/static_school/$1/$2';
this is not working.
I want to create a url like domain.com/schoolTesting/urlofschool
similarly another record can be
domain.com/schoolWorking/urlofschool
Hope this will help you :
$route['school(:num)/(:any)'] = 'Schools/url-of-school/$1';
Access url should be like this :
www.domain.com/school1613001/town-south-school
Your Schools controller's method should be like this :
public function town_south_school()
{
echo "string";
}
I need some information about parameter passing in the phalcon framework
example URL : http://localhost/project/vehicle/new/vinno/1323123
vehicleController{
newAction {
//get the value of vinno
// want to send parameter to view
}
}
Try dumping the dispatcher parameters.
print_r($this->dispatcher->getParams());
The above will give you the complete parameters array according to your router definition. More info about dispatcher in the docs.
To get a specific parameter value you can use getParam() method.
print_r($this->dispatcher->getParam('your_param_name'));
Use route parameter. And use $this->view->setVar
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
I am using codeigniter re-route to clean up some urls.
I am aware that I can do
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
But in some cases I have to add some extra parameters to the redirect url so that I get them as a param to the method. for example
$route['product_unique_and_rare'] = "catalog/product_lookup_by_id/{HERE I WANT SOME ADDITIONAL EXTRA PARAM}";
How to do this so that I get the value in the param of the method rather than the value in uri->resegment
you can try this
$route['product_unique_and_rare/(:num)'] = "catalog/product_lookup_by_id/$1";
Get the param in product_lookup_id like this
function product_lookup_id($product_id){
/*$product_id will be the passed parameter*/
}
So, if someone goes for http://domain.com/product_unique_and_rare/23, $product_id will get the value 23.
You can hard-code the parameter too, but I believe you aren't looking for that.