Cake PHP redirect with parameters in url - php

I have a page that I want to redirect to that requires parameters in the URL:
http://www.example.com/myController/myAction/param1:val1/param2:val2
I know that there is a CakePHP redirect function for redirecting that works as follows:
$this->redirect(array("controller" => "myController",
"action" => "myAction",
$data_can_be_passed_here),
$status, $exit);
How do I add the parameters that I want as part of the url using the above function?
I would think that there might be another element that I could add to array so that I can pass along param1:val1 and param2:val2.
Any help would be greatly appreciated!

I do not know why I was not able to find this in the CakePHP documentation, but I did finally figure out the solution. I am posting it here in case anyone else has the same problem. (If someone knows where this is in the documentation please post it as well, thanks!)
To redirect to the URL:
http://www.example.com/myController/myAction/param1:val1/param2:val2
You can use:
$this->redirect(array("controller" => "myController",
"action" => "myAction",
"param1" => "val1",
"param2" => "val2",
$data_can_be_passed_here),
$status,
$exit);
Hope it helps!

If you need to redirect with exactly get parameters, then pass '?' index to $url array argument:
$this->redirect(
array(
"controller" => "myController",
"action" => "myAction",
"?" => array(
"param1" => "val1",
"param2" => "val2"
),
$data_can_be_passed_here
),
$status,
$exit
);
It redirects to /myController/muAction/...?param1=val1&param2=val2
This is true at least in CakePHP 1.3

Instead, you can use this format also
<?php
$this->redirect('/controller/action/par1:par1/par2:par2/');
?>
<?php
$this->redirect('/controller/action/id/10/name/hello/');
?>

I usually do something like this:$this->redirect(['action' => 'view', $id, 'admins' => true]);
Hope it will help you.

In CakePHP 4.2 it seems that "param" => "val1" doesnt work anymore.
This is the way you go:
return $this->redirect(['controller'=>'mycontroller','action' => 'myview', 'myparameter']);

Related

How to redirect if a method is missing CakePHP?

I have a trouble when I'm using a wrong method with CakePHP and since new versions of it, things have changed. I didn't found how to redirect a wrong page to the default page each time it's a wrong URL.
Use:
$this->redirect(
array(
"controller" => "Controller",
"action" => "user",
"?" => array(
"username" => "USER123",
"usertype" => "Test"
) ),
$status,
$exit
);

Phalcon PhP - how to use named routes inside a controller

I'm having trouble finding how to get the Urls from named routes inside a Phalcon PhP controller. This is my route:
$router->add(
'/admin/application/{formUrl:[A-Za-z0-9\-]+}/{id:[A-Za-z0-9\-]+}/detail',
[
'controller' => 'AdminApplication',
'action' => 'detail'
]
)->setName("application-details");
I want to get just the Url, example: domain.com/admin/application/form-test/10/detail . With the code below I can get the html to create a link, the same result of the link_to.
$url = $this->tag->linkTo(
array(
array(
'for' => 'application-details',
'formUrl' => $form->url,
'id' => $id
),
'Show'
)
);
The result I want is just the Url. I'm inside a controller action. I know it must be really simple, I just can't find an example. Can you help me?
Thanks for any help!
You should use the URL helper. Example:
$url = $this->url->get(
[
'for' => 'application-details',
'formUrl' => $form->url,
'id' => $id,
],
[
'q' => 'test1',
'qq' => 'test2',
]
);
You can pass second array for query string params if needed.
According to your route definition, the above should output something like:
/admin/application/form-url/25/detail?q=test1&qq=test2
More info of Generating URIs in the docs.

Exploding array for url

I'm developing a site for analyzing a store's data.
I need the url part of my array to look like this:
array(
'url' => 'http://some.website.com:8080/SASStoredProcess/do?_username=user-123',
'_password' => 'passwd',
'_program' => '/Utilisateurs/DARTIES3-2012/Mon dossier/analyse_dc',
'annee' => '2012',
'ind' => 'V',
'_action' => 'execute'
);
I currently have this and am struggling to convert it to the desired format:
array(
'url' => 'url=http://some.website.com:8080/SASStoredProcess/do?_username=user-123&_password=passwd&_program=%2FUtilisateurs%2FDARTIES3-2012%2FMon+dossier%2Fanalyse_dc&annee=2012&ind=V&_action=execute',
'otherKey' => 'otherValue'
);
Please can somebody help me to convert the URL in the second code block to look like the first code block? Thanks in advance.
So this will extract the url in the form you want, as $url:
$myArray = array(
'url' => 'url=http://some.website.com:8080/SASStoredProcess/do?_username=user-123&_password=passwd&_program=%2FUtilisateurs%2FDARTIES3-2012%2FMon+dossier%2Fanalyse_dc&annee=2012&ind=V&_action=execute',
'otherKey' => 'otherValue'
);
parse_str($myArray['url']);
echo $url;
You will need to decide where it needs to go next and how you get it there.
You might want to use: parse_url() and parse_str() over your $array['url']

Passing parameters via redirect

Is it possible to pass parameters via redirect? I tried a lot of options, but nothing seems to work. My latest approach is:
return $this->redirect(array('Users::helloworld', 'args' => array('id' => 'myId')));
then I created a route:
Router::connect('/Users/helloworld?id={:id}', array('controller' => 'Users', 'action' => 'helloworld'));
but all I get is users/helloworld/myId
args is part of the routes and will be converted into an URL using the very generic route (not the one you created and don't require)
To get a query string, simply use the ? key:
return $this->redirect(array(
'Users::helloworld',
'?' => array('id' => $myId)
));
// will use the route:
// /{:controller}/{:action}/{:args}
// and generate
// /users/helloworld?id=$myId
The test for that: https://github.com/UnionOfRAD/lithium/blob/master/tests/cases/net/http/RouteTest.php#L374-405
Instead of defining a separate route to pass arguments, you could just do the following.
Lets say you want to pass 2 arguments: $arg1 & $arg2 to the helloworld action of your Users controller:
return $this->redirect(array(
'Users::helloworld',
'args' => array(
$arg1,
$arg2
)
));

Pass Element value to $ajax->link in cakephp

I need to pass the value of an element to an $ajax->link without using a form/submit structure. (because I have a dynamically set number of clickable links through which I am triggering the action)
I used to do this in Ruby using the Prototype javascript function $F like this:
<%= link_to_remote "#{item.to_s}",
:url => { :action => :add_mpc },
:with => "'category=' + $F('mpc_category')" -%>
But this does not seem to work in Cakephp:
<?php echo $ajax->link(substr($vehicle['vehicles']['year'], -2),
array('action' => 'add_mpc', 'category' => '$F("mpc_category")'),
array('update' => 'results', 'position' => 'top')); ?>
PHP sees $F as a variable instead of a call to javascript. I'm not too familiar with Javascript, but is there another way to pass the value of the 'mpc_category' input element to the controller through this link? I have been looking for a couple days and can't find anyone dealing with this specific issue. Thanks for any assistance.
Edit: fixed syntax in php statement.
haven't really used cake, but I have used rails. The js part should be entirely a string. Probably something like this:
<?php echo $ajax->link(substr($vehicle['vehicles']['year'], -2),
array('action' => 'add_mpc', 'category' => "$F('mpc_category')"),
array('update' => 'results', 'position' => 'top')); ?>
I'm assuming that it tacks "$key=$value" onto the params in the ajax link. Also note you were missing a comma at the end of that second line.
After working on this for a couple days now, the best I have come up with is this:
<?php echo $ajax->link($year,
array( 'action' => 'add_row',
'category' => $category,
'product' => $product.$newpart,
array( 'update' => $summary['summary']['id']." ".$vehicle['vehicles'],
'with' => "$('app_select').serialize()")); ?>
the 'with' => "$('app_select').serialize()" being the part that grabs the values out of the form without having to submit the form.
I only needed one of the form elements, however, so this is not ideal as it passes the entire serialized form to the controller.
I would still like to be able to do this with any element, regardless of it is in a form or not, but this method doesn't seem to do that. Perhaps someone more familiar with prototype could shed some light on that.

Categories