Skip to main content

Posts

Showing posts from February, 2013

CakePHP: Calling function from other controller

Import Controller of the function you want to use.

Once you imported  the controller you can call any function of this controller.

<?php//Import controller  App::import('Controller','SmsOutgoings');class ReportsController extends AppController {publicfunctionadd(){$message="Notification: New report submitted!";//Instantiation$SmsOutgoings=new SmsOutgoingsController;//Call a method from SmsOutgoingsControllerwith parameter$SmsOutgoings->notify_user($user_id,$message);}}?> Other way is by using the requestAction() function
<?phpclass ReportsController extends AppController {publicfunctionadd(){$message="Notification: New report submitted!";//Call a method from SmsOutgoingsController with parameter$this->requestAction('/sms_outgoings/notify_user',array($user_id,$message))

CakePHP : Updating the session user data after edit

<?php
//users controller

if ($this->User->save($this->request->data)) {
if($this->request->data['User']['id'] == AuthComponent::User('id')){ //if current logged in user  update user session data

                  $this->Session->write('Auth.User.first_name', $this->request->data['User']['first_name']); //updating first_name only

                  $this->Session->write('Auth.User.username', $this->request->data['User']['username']); //updating username only

                  $this->Session->write('Auth.User', array_merge(AuthComponent::User(), $this->request->data['User']) );   //updating all user session data

                }
                $this->Session->setFlash('The user has been saved');
                $this->Session->setFlash('', 'default', array(), 'success');
                $this->redirect(arra…