Tuesday, November 17, 2015

Call and use helper in controller Cakephp

Somebody said that there would have many ways to call Helper to use in view , but inspire I would show you how to implement it in Controller.php

class YourContrller extends Objects{
  pubic function yourfunction()
  {
  App::import('Helper', 'String'); 
  $string = new StringHelper(new View());
 
  $string->yourFunctionHelper();
 }
}


StringHelper.php

class StringHelper extends AppHelper{
  
   public function yourfunction()
   {
     .....
   }
} 

Saturday, November 14, 2015

Check and Remove Flash Session with Cakephp


Controller.php
 public function contact()
    {
        if($this->Session->check('Message.message_send_success')) {
            $this->Session->delete('Message.message_send_success');
        }
        if ($this->request->is('post')) {
            $author = $this->viewVars['get_author'];

            if (!$author) {
                return $this->Session->setFlash(
                        'Both fields are required!',
                        'default',
                        array('class' => 'redTxt'),
                        'message_send_error');
            } else {
                $sender_name = ucfirst($author[0]['User']['username']);
                $email       = $author[0]['User']['email'];
                $user_id     = $author[0]['User']['id'];
                $this->loadModel('User');
                    $title = $this->request->data['User']['title'];
                    $body  = $this->request->data['User']['body'];
                    if (!$title && !$body) {
                        return $this->Session->setFlash(
                            'Both fields are required!',
                            'default',
                            array('class' => 'redTxt'),
                            'message_send_error');
                    }
                    $Email = new CakeEmail();
                    $Email->from(
                        array($email => $title))
                        ->to($email)
                        ->subject($title)
                        ->emailFormat('html')
                        ->send('From '.h($sender_name).' '.h($body));
                    $this->Session->setFlash(
                        'The message has been send!',
                        'default',
                        array('class' => 'alert alert-success'),
                        'message_send_success');
                }
        }
        $this->set('title', 'Contact Us');
    }
}
view.php
Session->check('Message.message_send_success')): ?> 

THANK FOR YOUR CONTACT US!

your message has been send to us successfully.
Go back to home

Monday, November 9, 2015

Get properties from beforefilter in Cakephp to other function



That is just a trick which most of the developers are still wondering that is there a ways to get some properties from beforefilter in Cakephp to use in other function ? so here is the ways to do.

AppController.php
public function beforeFilter()
{
$user_id = ($this->Auth->user('id') != '' ? $this->Auth->user('id'): $author[0]['User']['id']);
$this->User->recursive = -1;
$get_author = $this->User->find('all',
     array(
        'conditions' => array('User.id' => $user_id)
     ));
$this->set('get_author', $get_author);
}
FavoritesController.php
public function get_vaforite(){
   pr( $this->viewVars['get_author']); 
}
Result :
Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 563c9401-445c-4d94-9151-5edb0a79370f
                    [username] => samphors bb
                    [password] => 3076ca74262cf933d1bbb4ee97474255f9aa8d9a
                    [facebook] => 
                    [email] => samphors.info@gmail.com
                    [address] => March dela deja
                    [mobile] => 1234567890
                    [birthdate] => 2015-11-10
                    [country] => Cambodia
                    [partner_country] => Italy
                    [height] => 6
                    [foot_size] => 3
                    [partner] => Dora vanndya
                    [partner_birthdate] => 2015-11-18
                    [partner_height] => 2
                    [partner_foot_size] => 5
                    [role] => 
                    [token] => 
                    [created] => 2015-11-06 20:50:25
                    [modified] => 2015-11-07 00:20:07
                )

        )

)