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.
Related
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.
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.
I have a listing with some filters which are applied on form submit, using GET as form method. So after submit, I get a url that looks like:
/listing?filter_1=a&filter_2=&filter_3=c
Notice that filter_2 is empty. How can I avoid showing it in the URL in this case? I would only need the URL to be like this:
/listing?filter_1=a&filter_3=c
I would not mess with $_GET and I wonder what is the right way to do it with Laravel 4.
Thank you
During making of url, you need to check the value of variables whether it has empty or null or have some value. Then add those varibale into the url, so that your url will be clean.
to achive this you will be required to use Laravel - pretty URls functionality and make some changes in Route::get function to remove empty parameters while construction url.
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.
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.