How to make pretty URLs using route? - php

I want pretty url. I access these records using url below. Last segment is index id to access data using it.
http://www.example.com/order/thank_you/344
How can I convert it into
http://www.example.com/order/thank_you
How to implement and also I want last segment in page to access the data using it.

routes.php
$route['order/thank_you/(:num)'] = 'thanks/thank_you/$1';
The left hand side is the URL accessed by the users. And the right hand side is where your request going to.
For example, the request example.com/order/thank_you/344 is going to the function thank_you() of the Controller thanks.
$1 is the argument. If have more than one, just /$1/$2/.
The Controller
public function friends_profile ($id = NULL) {
echo "This is the ID I want to thank to " .$id;
}
The function arguments can be passed directly by the route. $id is the argument passed by the URI.
By the way, check out the Codeigniter URI Routing Guide to get more information.

If you want the url not to contain the id portion you could keep it in a session var and retrieve it at the thank_you method.

Related

Send parameter to controller in Yii

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.

How to generate a new URL using current_url in codeigniter

I am trying to use the current_url() function in codeigniter but not sure how to manipulate this data. I understand I can echo out the current_url() but how do I take each of the segments and use to generate a new URL?
I am trying to take pieces of the current page and use to generate a new URL with adding in new variable data.
I have never worked with current_url(); before, so not sure what to do. Lets say my user is on a filters page and has 2 variables to filter their results (neighborhood and category). If the user clicks on a neighborhood, how would I structure the hyperlink to pull the current url variable for the category and then insert the variable for neighborhood?
My URLs look something like:
mydomain.com/ny/find/$neighborhood/$category
Breakdown:
ny = controller
find = function
param1 = $neighborhood
param2 = $category
Any help would be much appreciated...
You can breakdown your URL by means of this:
For ny: echo $this->uri->segment(1);
For find: echo $this->uri->segment(2);
For param1: echo $this->uri->segment(3);
For param2: echo $this->uri->segment(4);
<?php //check if there is a value in uri segment 3 (param1)
if($this->uri->segment(3)){
//if there is a value there then include it in the link ?>
Link text
<?php //otherwise just go to the link for param1
}else{ ?>
Link text
<?php } ?>
This should work though depending on the exact logic needed you may have to tweak the links and also the logic in the else statements. Depending on how you want to link you may also have to add an elseif($this->uri->segment(4)) and use different logic if the user is on a page with 4 url segments.
current_url() returns a string for the actual page loaded. You can create any url you want using the segments.
Let´s say you have this current url:
mydomain.com/ny/find/$neighborhood/$category
if you want add something to this url and create a link to the user, you can make a string:
$url = base_url().$this->uri->segment(1).'/'.$this->uri->segment(2).'/newParamYouAdd/'.$this->uri->segment(4);
And then:
New url
Let me know if is this what you want.

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.

Zend_framework - place data in $_POST when using _redirector helper in the controller

I an redirection (in some cases) from a controller to the error controller, action 'not-logged-in' using the redirector helper. My problem is the following: I want to pass an argument in the $_POST array (an URL from where the redirection happened) so the user will be able to return to that page after performing a login.
How can i place data in the $_POST array while using redirect helper?
Thank you ahead.
When you use the redirector with an internal redirect (ie. goToRoute) the paramters are passed along with it. Thus if you add your refferrer to the the request before you actually redirect:
// Assuming $request is a Zend_Controller_Request
$request->setParam('ref', $referrer);
// then use the redirector
then that variable will be passed along with the request upon redirect. So then you would need to check for/grab that variable from the request in the action youve redirected to and then set it as a hidden field in the form. Then when your form posts to your login action you can check again for a ref variable and on successful login redirect to that location.
Now if i were you i would not actually use the referral as the url but a serialized or json encoded array of the previous request's parameters. that way you can use goToRoute in this second instance as well.
Ofcourse if the redirection came form some sort of post action that contained sensitive data you wouldnt want to do this. In that case you would want to use the session as has been previously suggested.
Above all the best advice i can give is to look at the code of Zend_Controller_Router_Rewrite and Zend_Controller_Action_Helper_Redirector.
Not possible without some socket or Curl jiggery pokery.
Why not try using $_SESSION array in the same way?
Does it really matter if the user can see the redirection url in the address bar? i doubt they will care and i see it a few times on some top sites.
Passing control to the login page just feels more like a _forward than a _redirect, like it all belongs under the one action. Especially since you're coming right back.
_forward($action, $controller = null, $module = null, array $params = null)
Then, you can pass your originating location in $params as you'd like.
I'm pretty sure that you can't send POST when redirecting a person to another page. But maybe you can, and if so, I hope somebody proves me wrong here.
I'm not sure how you'd do what you want using Zend Framework, but I would suggest two ways how to do it in general. You can either send a GET variable, or use a session variable to store a back-URL.

CodeIgniter: Directing to functions with a URL segment

I'm working on a survey system for my company and I have it setup in the system so that there are two ways to take the survey.
1) New Survey Taker no prior information
2)Survey was already sent out and a session was created.
In case one I would like my URL to look like:
mydomain.com/SurveySystem/index.php/survey/$surveyID
($surveyID being a integer of the survey to take)
The second case would where we create a link with for the test taker. I would like the URL to look like this:
mydomain.com/SurveySystem/index.php/survey/$surveySessionID/$guestID
In my Survey class I have it setup as the following:
function index(){
$segments = $this->uri->total_segments();
if($segments == 1){
echo "no surveyID set";
return;
}
if($segments == 2){
$this->take_survey($this->uri->segment(2));
}
if($segments == 3){
$this->survey_session($this->uri->segment(3), $this->uri->segment(4));
}
}
When no information is passed the it echos just fine.
But if I try to put a integer where the surveyID is it thinks i'm loading up a method in the controller.
Thank you for the help!
Use URI routing to override the default controller/function/arguments mapping.
Example: in your application/config/routes.php:
$route['survey/:num'] = "survey/take_suvey";
Bonus: You can also remove the index.php/ part, see Removing the index.php file.
My question is, with a URL that unfriendly, why do you care what it looks like at all? It is not semantic from a user perspective. Yes, you should be removing the index.php. Coupling the URL rewriting, the whole thing should be reduced from:
mydomain.com/SurveySystem/index.php/survey/????
to
mydomain.com/survey/
And your CI class methods can be reduced to "take," or "submit," and a subsequent "review."
Sessions should be managed using cookies or CI's session class. If you need to track state in the URI, combine your survey-specific "session" and the "guestID" into one segment with base64 encoding.
Lastly, using a route as suggested, there will be no way for your app to know what survey ID to load. It would need to capture the ":num" and feed it to take_survey:
$route['survey/(:num)'] = "survey/take_survey/$1";
If you take numeric ids as the first segment after /survey, you need another route placed after that one to handle the case where that segment is a session ID:
$route['survey/(:num)/(:num)'] = "survey/session_manager/$1/$2";
Where the $1 and $2 are session ID and Guest ID respectively. Personally, I would say this is bad form. The semantic meaning of your segments break down: it becomes difficult to determine what that first numeric segment means (is it a survey ID or session ID), unless you can always guarantee that these routes are in place.

Categories