I want to redirect to another action in different controller.
This is my code:
$tempData[] = $rowData;
return $this->redirect(array('customize-header/show-headers', 'arr' => $tempData));
and this is the actionShowHeader in purpose controller:
public function actionShowHeaders($arr) {
$model = new CustomizeHeader();
$items = CustomizeHeader::getAllHeader();
return $this->render('view', [
'model' => $model,
'items' => $items,
]);
}
but I got this error
Anybody know how to solve this?
The error has been solved! I just convert the array using json_encode, this is the code: $tempData = json_encode($arrayData); return Yii::$app->response->redirect(['customize-header/sorting-headers', 'tempData' => $tempData]);
Related
Im trying to get $data from profileData function from First Controller and render it into html in Second Controller. I couldnt get pass through the error "Call to a member function has() on null". How do I fix this?
Here is the function that is stated in the First Controller.
public function profileData(Request $request, $uuid){
$em = $this->getDoctrine()->getManager();
$currentUser = $this->get('security.token_storage')->getToken()->getUser();
$customerRepo = $em->getRepository('CoreEntity:SubscribedCustomer');
$subscribedCustomer = $customerRepo->findOneBy(array('customer' => $uuid, 'consultant' => $currentUser->getId()));
$customer = $subscribedCustomer->getCustomer();
$age = 'N/A';
$dob = $customer->getDateOfBirth();
if ($dob) {
$birthYear = $customer->getDateOfBirth()->format('Y');
$yearNow = date('Y');
$age = $yearNow - $birthYear;
$dob = $customer->getDateOfBirth()->format('Y-m-d');
} else {
$dob = "";
}
if ($customer->getStatus() === 1) {
$status = "Active";
} else {
$status = "Inactive";
}
$data = array(
'firstName' => $customer->getFirstName(),
'lastName' => $customer->getLastName(),
'email' => $customer->getEmail(),
'phoneNum' => $customer->getPhoneNumber(),
'gender' => $customer->getGender(),
'age' => $age,
'status' => $status,
'dob' => $dob
);
}
In the Second Controller, I want to render the data in
/**
* #Route("/client/{uuid}/basic_health/bmi", name="client_bh_bmi")
*/
public function basicHealthBmiAction($uuid)
{
$this->checkWebModuleAccess(WebModuleCode::MANAGE_CLIENT);
$profileData = $this->get('client_profile_data')->profileData();
return $this->render('AppBundle:client/health_record/basic_health:bmi.html.twig', array(
'customer' => $data
));
}
In services.yml :
services:
client_profile_data:
class: AppBundle\Controller\FirstController
Though not very common, you can also forward to another controller internally with the forward() method provided by the AbstractController class.
Instead of redirecting the user's browser, this makes an "internal" sub-request and calls the defined controller. The forward() method returns the Response object that is returned from that controller
https://symfony.com/doc/current/controller/forwarding.html
I know this question has already been asked so many times before but I am not able to find out any good example which can solve my issue.
I am using Codeigniter Rest APIs for my project with AngularJS. This is my response
{"msg":"success","data":{"test":"1","test1":"2"}}
I am using Codeigniter method to send response in this way.
$data = array();
$data['test'] = '1';
$data['test1'] = '2';
$this->response(array('msg' => 'success', 'data' => $data),200);
and this is AngularJS code
factory.create = function(){
return Restangular.all('index.php/customer/getAll').getList().then(function(response){
console.log(response);
});
}
Please help me to understand and resolve it.
Thanks
Use it as below to receive result as array :
$data = array();
$data[] = [ "label" => 'test1',"id" => 1];
$data[] = [ "label" => 'test2',"id" => 2];
$this->response(array('msg' => 'success', 'data' => $data),200);
I am working on cakephp now.Please explain the process of custom mysql query in cake php.
we have to write query by using
$this->Model->query();
and I return this to controller.In controller,i loaded the model in particular function and i called the function and set that function to view like this
$this->set('post',$this->User->get());
is it correct process?please explain the code ...
What query do you want to write this way? It is possible to write nearly all queries using the CakePHP ORM. Using the query building functions of CakePHP is the prefered way of doing it.
All data fetching an manipulation should be done in a model as well. See Separation of Concerns.
Here is a complete model method to fetch an user record based on its id OR slug.
public function view($userId, $options = []) {
$defaults = array(
'contain' => array(),
'conditions' => array(
'OR' => array(
$this->alias . '.' . $this->primaryKey => $userId,
$this->alias . '.slug' => $userId,
),
$this->alias . '.email_verified' => 1
),
);
$result = $this->find('first', Hash::merge($defaults, $options));
if (empty($result)) {
throw new NotFoundException(__d('user_tools', 'User not found!'));
}
return $result;
}
Controller:
public function view($userId = null) {
$this->set('user', $this->User->view($userId);
}
Alternative but NOT preferred mode method to fetch te data
public function view($userId, $options = []) {
$result = $this->query(/* Your query here */);
if (empty($result)) {
throw new NotFoundException(__d('user_tools', 'User not found!'));
}
return $result;
}
Your solution is correct, let me eleborate it in more detail
1st Solution
In your controller
$arrayTemp =array();
$arrayTemp = $this->ModelName->query(' Your Query String ');
$this->set('post',$arrayTemp);
2nd Solution
In your model class
function returnDate(){
return $this->query('Your Query String')
}
In Controller class
$arrayTemp = $this->ModelName->returnDate();
$this->set('post',$arrayTemp);
}
I have a search method in my WorkersController.php like following:
public function search() {
$conditions = array();
if(!empty($this->request->data)){
foreach($this->request->data['Search'] as $field => $search_condition ) {
if(!empty($search_condition))
$conditions["$field LIKE "] = "%$search_condition%";
}
}
if(!empty($conditions)){
$this->Worker->recursive = 0;
$workers = $this->Worker->find('all',array('conditions' => $conditions));
}
$this->redirect(array('action' => 'index','search' ));
}
IN the method I call redirect(), then the page goes to index.ctp, where I want to fetch $workers like this:
if($this->request->params['pass']==array('search')){
if (empty($workers)){
echo('No result found!');
}else{
foreach ($workers as $worker){
//do something
}
}
}
But I just can't fetch $workers, how can I pass it from search() to index.ctp?
Thanks a lot!
You could try and use Session for this case.
//in controller1
$this->Session->write('worker', $workers);
//in controller2
$workersData = $this->Session->read('worker');
yes you can use as per below syntax to
$this->redirect(array('controller' => 'workers', 'action' => 'index', 'pass' => 'param', 'pass1' => 'param1'));
for more detail you can use doc
I recently upgraded my app from cake 1.3.x to cake 2.x. Now I have a helper which uses model in some function. Initially syntax for loading model was (working for 1.3.x)
App::import('Model', $modelName);
$modelObject = &ClassRegistry::getObject($modelName);
$modelObject->find()
Now I changed it to following
App::uses($modelName,'Model');
$modelObject = &ClassRegistry::getObject($modelName);
$modelObject->find()
Problem is, this conversion is not working. Can anybody tell me where am I doing wrong. Thanking in advance.
PS:
Error message is:
Call to a member function find() on a non-object
working code should be
//let $modelName be User
App::import("Model", "User");
$model = new User();
$model->find("list");
I hope this will help some needy fellow
You can also load model in helper, add following method in helper class:
Step1:
public function loadModel($modelClass = null, $id = null) {
if ($modelClass === null) {
$modelClass = $this->modelClass;
}
$this->uses = ($this->uses) ? (array) $this->uses : array();
if (!in_array($modelClass, $this->uses, true)) {
$this->uses[] = $modelClass;
}
list($plugin, $modelClass) = pluginSplit($modelClass, true);
$this->{$modelClass} = ClassRegistry::init(array(
'class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id
));
if (!$this->{$modelClass}) {
throw new MissingModelException($modelClass);
}
return true;
}
Step2: call above method as you do in controller like this:
$this->loadModel(ModelName);
I hope this would resolve problem
I wanna go through helper to view data In the form of foreach
public function display()
{
$links = TableRegistry::getTableLocator()->get('category');
$category = $links->find('all')
->where(['status' => 1, 'position' => 0])
->select([
"category" => 'parent', 'name'
])
->toArray();
return $category['category'];
}