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.
Related
Below, I have the variable entryName available to use to insert data into my small-test blade view by using {{!! entryName !!}}.
#include('views.small-test', [
'entryName' => $data['entryName'],
])
But how can I append a static string to the front of it? Would using . work to concatenate it like below?
#include('views.small-test', [
'entryName' => "Welcome".$data['entryName'],
])
Yes, it works.
According to the laravel docs:
Even though the included view will inherit all data available in the parent view, you may also pass an array of extra data to the included view:
#include('view.name', ['some' => 'data'])
You can also format the entryName on server side and send the variable to the view, or, within your small-test view, show the message like this:
Welcome, {!! $entryName !!}
Hope it helps.
I do not know how to use this in order to always have GET parameters
Route is setup like
Route::controller('/test', TestController::class);
If I specify the getIndex function, it adds GET parameters
>>> action('TestController#getIndex', ['type' => '123'])
=> "http://localhost/test?type=123"
But if I specify anything but the getIndex function it does not add the parameters as GET variables
>>> action('TestController#getSuccess', ['type' => '123'])
=> "http://localhost/test/success/123"
Take a look at the output of php artisan routes --path=/test
Some of the routes (like the one mapped to getSuccess) have parameters and Laravel attempts to match the given parameter array to the route parameters.
Once all the query parameters are accounted for, any excess elements are converted into a query string and appended to the final URL.
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
I decided to move the logic with YII on Yii2 and a problem with the routing entry.
//yii1
'image/<id:\d+>*' => array('image/get'),
But it doesn't work on YII2
//yii2
'image/<id:\d+>*' => 'image/get',
How to write the route to get function takes a variable number of arguments?
Try
'image' => 'image/get',
And in the ImageController get the params the normal way
$id = \Yii::$app->request->post('id');
I have defined
'my-account' => 'my-account/index',
The link /my-account?id=5&foo=bar works just fine.
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);