Redirect with array parameter Cakephp - php

I'm working on a Cakephp project, I have one controller that made some algorithms and save information to an array. I need to call another method on that same controller and send the array as a parameter. I need to do this using redirect, just like this:
return $this->redirect(['controller' => 'Identities', 'action' => 'myMethod', $array]);
If I don't use redirect, I will not have the view of the myMethod, and I need it, but I need to have the array information on that view, something like doing $this->set to pass the array to a view.
However, I get this error:
rawurlencode() expects parameter 1 to be string, array given
How can I send the array as a parameter?

You can use query string like this.
return $this->redirect(['controller' => 'Identities', 'action' => 'myMethod', '?' => ['param1' => 'val1', 'param2' => 'val2']]);
You can provide routed elements or query string parameters.
Source

Related

Yii2 - how to get all view params passed from controller?

How can I get all params that the controller passed on to the view?
My controller says:
return $this->render('update', [
'model' => $model,
'varA' => 3,
'varB' => 8
]);
Within the view var_dump($this->params) just returns an empty array instead of the 3 items. How can I get the entire array that was passed on?
The reason I want to do this is to pass on the params to another view with renderPartial() and I want to avoid specifying each parameter manually...
You should simply use $_params_ :
The parameters (name-value pairs) that will be extracted and made available in the view file.
e.g. :
$this->renderPartial('view', $_params_);
Read more about how php view files are rendered.

Is there a way to create a ZF2 console route with repeat parameters

I'm trying to set up a console route that can accept multiple email addresses.
Basically what I'm wanting is a route that accepts something like:
php public/index.php run-report --email=first#example.com --email=second#example.com
I've tried:
run-report [--email=]
But that will only accept a single address. Once you put in a second --email, it fails to match the route. I can hack it by passing in a comma separated string of email addresses, but I'm looking for a way that will result in an array of values, so that I don't have to parse the parameter myself.
From looking at the source code for the simple Console router (ie. Zend\Mvc\Router\Console\Simple), it doesn't appear that this is available out of the box. The console parameter matching is only written to match unique keys in the route.
However - you could try to use the 'catchall' route type instead.
So for example, use this as your console route:
'test' => array(
'type' => 'catchall',
'options' => array(
'route' => 'test', //this isn't actually necessary
'defaults' => array(
'controller' => 'Console\Controller\Index',
'action' => 'test'
)
)
)
Then you can pass as many --email values as you want, you just have to validate them in the controller.
So running this:
php index.php test --email=test#testing.com --email=test2#testing.com
Can be interpreted in the controller:
print_r( $this->getRequest()->getParams()->toArray() );
Array
(
[0] => test
[1] => --email=test#testing.com
[2] => --email=test2#testing.com
[controller] => Console\Controller\Index
[action] => test
)
This isn't exactly ideal as you can also get the same input by executing this (ie. passing email instead of test as the route) - because it's the catchall:
php index.php email --email=test#testing.com --email=test2#testing.com
So you'll have to validate the params directly in the controller as well.

Codeigniter retrieve rewritten URL parameters

I'm using Codeigniter and my page URL is here.
and I want to send this URL to the controller "adverts" and method "search" with a category parameter "cat" and id "824". To do so i use the routes like this:
$route['^en/hotels$'] = "adverts/search/cat/824";
My problem now is that i dont know how to retrieve the rewritten parameters "cat" and id "824".
When i use $this->uri->segment_array() i have the initial request URL:
array (size=2)
1 => string 'en' (length=2)
2 => string 'hotels' (length=16)
Please someone can explain me how to retrieve my parameters?
Thanks again.
you should declare a method like:
function search($cat,$id){
}
CodeIgniter will automatically bind URL parameters to method parameters.

Pass dynamic value in URL with CakePHP

I have the following method that performs an Ajax request passing some dynamic value obtained from a select input.
It works fine, but the dynamic value is passed as parameter in the URL, something like states/listCities/?big_string_of_serialized_parameter .
$this->Js->event(
'change',
$this->Js->request(
array( #url
'controller' => 'states',
'action' => 'listCities'),
array( # ajax options that generates the serialized parameter
'update' => '#DealerCityId',
'data' => '$("#DealerStateId").serialize()',
'dataExpression' => true
)
)
);
I'm trying to do this in a more friendly URL way, something like states/listCities/2.
It's possible in CakePHP to generate a friendly URL like this with dynamic value from a input or is only possible passing the dynamic values as parameters?
As far as I understand it's not possible. You could pass an ID as third parameter in the URL array, but as the ID is not known at template generation time, it's not applicable in this situation. If you want to use JsHelper, you'll have to stick with the JavaScript code generation it provides.
As an alternative, you could write your own Helper: Derive it from JsHelper and override the request() method to suit your needs. You can probably take the source code of the original source code to get a head start and only modify the way the data parameter is used in code generation.

How to get parameters from the URL to the model in cakePHP?

let's say i have:
http://some-domain/application/controller/action/parameter
This is somehow working in cakePHP. Now I want to now what exactly 'parameter' is. But inside the Model. How to get to this information?
I have to say that there is a formular including a 'Next' Button and I want to validate the input inside of the Model in beforeValidate(). But I have to know on which page the user was at the time of clicking the submit button. This page is 'parameter'.
Router::getParams() is available from everywhere and gives
[plugin] =>
[controller] => leads
[action] => step1
[named] => Array()
[pass] => Array()
[url] => Array(
[ext] => html
[url] => someurl/post-1
)
http://api.cakephp.org/2.3/class-Router.html#_getParams
There are two type of parameter in CakePHP, you have passed parameters and named parameters. A passed parameter is as shown in your example and will be passed as part of the url.
http://example.com/controller/action/passed_param
echo $this->params['passed'][0] // 'passed_param'
http://example.com/controller/action/name:param
echo $this->params['named']['name'] // 'param'
I would recommend getting the parameters in your controller and calling model methods with them passed through.
Such as
$this->Model->find('all', array('conditions'=>array('id'=>$this->params['passed'][0])));
As to how it's working, you will want to have a look at your routes file. In your app/config/routes.php you will find all the routing and which parts are passed.
The standard cake url format is usually as follows, as you'll see in the routes.
array('controller'=>'MyController', 'action'=>'MyAction', 'MyParam');
I can't seem to find a specific page in the book on Params, but have a google around for guides.
Model (in MVC design pattern) shouldn't have direct access to any external variables. The proper way is to pass that variable as a parameter from Controller or View:
$myModelObj->doSth($getParameter);

Categories