not able to pass array from controller to view in cakephp 3 using $this->set('data', array received from find()) - cakephp-3.0

Not able to pass array variable form controller to view below code
If, I print in controller it is showing "Undefined variable: data"
public function report(){
$results=$this->Nodals->find()->toArray();
$this->set('data',$results);
print_r($data); die();}

Try using compact like this.
public function report(){
$data=$this->Nodals->find()->toArray();
$this->set(compact('data'));
};
Now do dd in your view to check if the data variable sending to the view. I am assuming your view file is report.ctp, so in report.ctp file write this line
<?php
dd($data);
?>
I hope it will help.

$data is not defined in your code if you want to print the query result from
$this->Nodals->find()->toArray(); then you need to store this in $data variable or any other variable
$results=$this->Nodals->find()->toArray();
$this->set('data',$results);
//data will be sent to the template which wrap the $result and in template you can access $reseult by using $data
print_r($results);
die();
or
$data=$this->Nodals->find()->toArray();
$this->set('data',$data);
print_r($data);
die();

Related

How to retrive the parameters in URL + laravel

for web services , how to retrieve the data json format using Laravel, can anybody help
https://test.com/admin/users/apiLogin?Login={"email":test#test.com,"pwd":testing}
in Controller
public function apiLogin(Request $request)
{
//$data = $request->json()->all();
$data=json_decode($request['Login']);
echo $data['email'];
echo "<pre>";
print_r($data);
echo "</pre>";
echo "testsetet";
exit;
}
To get the URL parameter use this:
$login = $request->input('login'); // or $request['Login']
The goal is to turn login (which is a string) into appropriate JSON format so that you can use json_decode.
I'll break it down in steps:
Remove all \n characters:
$login = str_replace('\n', '', $login);
Remove last comma
$login = rtrim($login, ',');
Add brackets
$login = "[" . trim($login) . "]";
Turn it into a PHP array:
$json = json_decode($login, true);
The issue here is that your url parameter 'Login' isn't properly formatted json. I would revisit your front-end code persisting this data and ensure it is formatting properly. True json should look like:
{"email":"test#test.com","pwd":"testing"}
As a BIG aside, never persist passwords in the URL. It is dangerously foolish. Use a POST request to do this. On the plus side, the back-end code will be virtually the same with your json_decode() logic.

cakePHP3 pass variable from controller to view

I have a problem when passing a variable from Controller to view in CakePHP3.
File FincasController.php: I have created a public Method to make a report and be able to pass to a Template I have also created.
public function worklist()
{
$worklist = TableRegistry::get('Fincas');
$query = $worklist
->find()
->select(['id', 'prov', 'municipio', 'paraje', 'poligono', 'parcela', 'f_ult_poda' , 'f_ult_recog' ]);
foreach ($query as $worklist) {
if ( $worklist->f_ult_poda > $worklist->f_ult_recog )
debug($worklist); //it works fine
}
$this->set('finca', $this->$query); //I have tried also $this->$worklist
}
File Template\Fincas\worklist.ctp :
<?php debug($finca);?>
Thanks a lot
$query it justa a variable, it's not an attribute of your FincasControllerõ. Why are you using$this`?
simply
$this->set('finca', $query);

Yii2 get controller/action from url

How in yii2 get controller/action from url.
Attention!!! I ask about Yii2. There are answers that consern only Yii1 at this forum.
Added.
I find smth like Yii::app()->getUrlManager()->parseUrl('http://eewee.djn'));
but in yii2
Added.
I get refferer url to anather conrtoller, where i want to parse it url by yii2 and get controller/action.
That worked for me
$request = new Request(['url' => parse_url(\Yii::$app->request->referrer, PHP_URL_PATH)]);
$url = \Yii::$app->urlManager->parseRequest($request);
list($controller, $actionID) = \Yii::$app->createController($url[0]);
You receive your action in $actionID and controller name in the $controller->id. The Request object needed because such construction:
list($controller, $actionID) =
\Yii::$app->createController(parse_url(\Yii::$app->request->referrer, PHP_URL_PATH));
not works with parameters in url.
Try this!
<?php echo Yii::$app->controller->id; // controller id ?>
<?php echo Yii::$app->controller->action->id; // controller action id ?>
list($controller, $actionID) = Yii::$app->createController($url);
If you need action as Object try this
$action = $controller->createAction($actionID);

How to implode an array to show it in a textbox in yii2 ActiveForm?

Children field is an array in mongoDB:
<?= $form->field($model, 'children') ?>
The error I get is:
Array to string conversion
I need to use implode(',', $model->children) somehow, how to use it in an ActiveForm? What to do now?
What is the solution? How to turn that array into a string?
The content of the $model->children attribute is displayed when being used in a $form->field() call. If the content of the attribute is an array and you want/need it to be a string you'll have to convert the content before the field() call.
So like this, it will probably work.
<?php
$model->children = implode(',', $model->children);
echo $form->field($model, 'children');
?>
Not sure editing a list value like this (in a textfield) is the best way. You'll have to explode the string back when saving. But the code above is the solution to turn that array into a string.
As I wanted to turn it into string in every widget, grid view and so I used afterFind() function in my model in order to convert it into string. Now everything seems awesome:
public function afterFind() {
parent::afterFind();
if (is_array($this->children)) {
$this->children = implode(',', $this->children);
}
}

Json call in joomla 2.5 component

I'm making a joomla 2.5 component and i trying to set in model or controller (what's the most appropriate ?) a json response of my DB request (for later get the json with angularJS).
Here's my model (with DB response):
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.modelList' );
class MediastoreModelList extends JModelList
{
function getListQuery()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id, type, designation', 'marque', 'prix');
$query->from('produits');
return $query;
}
}
My empty controller:
<?PHP
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
class MediastoreController extends JController
{
}
My view
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.application.component.view' );
class MediastoreViewList extends JView
{
function display($tpl = null)
{
$this->items = $this->get('items');
parent::display($tpl);
}
}
and my template
<?php
defined('_JEXEC') or die('Restricted access');
JHTML::script('media/com_mediastore/js/angular.min.js');
JHTML::script('media/com_mediastore/js/app.js');
?>
<?php
echo $this->items;
?>
<div class="content">
<p>Nothing</p>
</div>
How can i do that ?
Thanks a lot,
Antoine
Bit late .....
Hi I am working on something similar.
In your views (sitepart) add an other file called view.raw.php.
You can browse to this file by appending 'format=raw' at the end of the url.
For example: if your component view page in the browser is 'http://test.com/test( IF SEF is on ) then after appending th url shoul be like this
http://test.com/test?format=raw and in case SEF is not on the use & instead of ?
You can use this URL in the angularjs for http.get service.
In view.raw.php file make sure you just echo the results rather than parent::display($tpl);.
This did work for me.
PS: I am using angualrjs too but want to use the component with many menu items and the url keeps changing so am stuck on that part.
Hope this solves your issue.
Regards,
Jai