Send parameter to controller in Yii - php

I am working on admin panel in yii and want to update record so when so i want to send record id on href click so url become
http://localhost/firstapp/backend/vehicle/edit/1
but its not working it gives error as "Unable to resolve the request "backend/vehicle"." I am trying this from long time please help

You can pass variable by get method, like "/site/index?asd=xyz&pqr=qwe" and this can be access by $_GET.
If you want to pass variable in codeigniter style, i.e. by using '/', You can use name parameter in url manager. You can get more info here : http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-named-parameters

You should provide more information about your question for better help.
However, suppose you have www.example.com/controller/action url. if you want to pass some variable to action, first you need to define a rule in url manager in config/main.php like this:
"controller/action/<variable1>/<variable2>"=>"controller/action"
Now if the url was "controller/action/test1/test2", in your action the value of $_GET['variable1'] will equal to "test1" and $_GET['variable12'] will equal to "test2" .
Note that user-defined url manager rules must be BEFORE the yii default url manager rules.

Related

How to get the URL paramters inside view using codeigniter

Given this link:
localhost/abc/review?coupon=bskgnlsdgkj
How do I get the coupon value inside view?
I tried $_GET['coupon], but it didn't work.
You can use $this->input->get('coupon'); to get the value of url parameter.
But, I suggest you to get the parameter value with $this->input->get('coupon',TRUE); to applying XSS Security directly.
For information can be found on this link.

Change URL in CakePHP

I have a Page in Cakephp that looks like
www.example.com/posts/next_posts
i.e. I have app/View/posts/next_posts.ctp
but I have Categories1, Categories2, Categories3,.... in Database.
How do I change the URL so that I assign a Variable of Database and it looks like
www.example.com/categories1/next_posts
www.example.com/categories2/next_posts
www.example.com/categories3/next_posts
By using the router. Read this chapter:
http://book.cakephp.org/3.0/en/development/routing.html
It will map a URL to a controller and action and passes arguments based on the URL. There are examples on that page as well if you're looking for that.

Yii - how to make url as readable?

I am creating a realestate website, I want my url to be readable,
For example my site url is http://sitename/property/3, (3 represents property id)
I want to change it like this link
My question is how they are using "Residential-Apartment-Flat-in-Park-View-City-Gurgaon-4-BHK-for-Sale-spid-H13544257" this part in url.
Of course they are using id and all. How can I replace "property" with something like "residential-property-in-india" in my url and it should be changing dynamically according to search.
I hope it is clear what I want to do...
Everything is in the Yii guide
For example the folling urls config in the url manager component
array(
'posts'=>'post/list',
'post/<id:\d+>'=>'post/read',
'post/<year:\d{4}>/<title>'=>'post/read',
)
Will generate link like http://example.com/post/2004/My-title-that-can-be-long
You can tweak it to match your need,if I were you I'll read the Yii guide very carfully
At the last of rules array place this rule
'<page>'=>'someControlles/someAction',
And you will get in someControlles/someAction parameter. $page = 'any_text'. Another way to create your own UrlRule.

GET url in Codeigniter

I am using codeigniter for my project. To get the uri segments I know I can use
$this->uri->segment();
but my case is a bit different
My url looks like
localhost/mediabox/home/box/21
but once I go to this url a popup form apears in which the user provide a key to access this page and I validate the key using ajax method which is inside my home controller validate_key function
when I echo the url it gives me localhost/home/validate_key
while calling valiate_key of the home controller how can I get the 21 from the url wrritten in the url bar?
Any ideas?
Thanks
The problem:
It's not a bug, it's a natural behavior.
Consider the following:
you request the validate_key function from the server by typing the URL in your address bar. current_url() returns localhost/blabla/validate_key. No AJAX involved.
requesting validate_key with AJAX. The same PHP code will be executed.
the current_url() will change to localhost/blabla/validate_key
even though your browser's address bar is showing localhost/blabla/box/21.
So, what does this means? It means the Codeigniter base_url() doesn't care about your address bar, it cares about the function it is in, whether it was called via ajax or normal request.
so as long this function is being executed, the URL is pointing to it.
The solution:
My favorite solution to such a case, is to simply create a hidden input.
Simply, when a user requests the box function. you're showing him a popup form. so add a hidden_input field, give it a name and a value of 21(depends).
For example(you should tailor this to your specific needs):
Add this to your form in the view that get displayed by the box function:
form_hidden("number", $this->uri->segment(3));;
Now these data will be sent to your validate_key function. How do we access it? It's simple!
function validate_key(){
$this->input->post("number");//returns 21 or whatever in the URL.
//OR if the form sends GET request
$this->input->get("number");//return 21 or whatever in the URL.
/*
*Or , you can do the following it's considered much safer when you're ONLY
*expecting numbers, since this function(intval) will get the integer value of
*the uri segment which might be a destructive string, so if it's a string
*this function will simply return 0.
*/
$number = intval($this->input->post("number"));//returns 21 or whatever in the URL.
//Or if it it GET request:
$number = intval($this->input->get("number"));//returns 21 or whatever in the URL.
}
It looks like you've used .htaccess to remove the index.php part of the url. So, when you navigate to localhost/mediabox/home/box/21 you're passing the value 21 to the function named box in the controller named home
If you want to keep that value within the validate_key function, just pass it through when calling it:
function box($param)
{
//$param = 21
$this->validate_key($param);
}
Its better and suggetsed to use hidden field and post the value when it is needed.

How to pass value to popup window using codeigniter?

I know that you can use anchor_popup() to open up a new window in codeigniter, but what i cant figure out is how to pass values using that.
Here is the code i am using,
$attr = array('width'=>'800','height'=>'700');
echo anchor_popup('friends/'.$uid.'/'.$suid.'','add as friend',$attr);
Now i get a popup window which links to friends/34/34 where friends is a controller and rest are values which i want to give to my controller. but iam getting an error 404,
404 Page Not Found
The page you requested was not found.
I am using uri segment to grab the values.
Could any tell me what i am doing wrong?
the link friends/34/34/ is looking for a controller called "friends" and a method called "34", hence why it can't be found.
If your friends controller has no other methods, then change the link to this:
echo anchor_popup('friends/index/'.$uid.'/'.$suid,'add as friend',$attr);
else, add the appropriate method.

Categories