Cakephp Archives | Codeexecute

Get current full url in php

$currentPage =  'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
If you want to check that ssl is enable or not then use below code
$currentPage =  (isset($_SERVER['HTTPS']) ? "https" : "http") . "://". $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];


Force SSL redirection in cakephp

Step 1. use Security component.

 public $components = array('Security');

Step 2. use the below code to post data.

//Use the below code in parent controller's beforeFilter action
$this->Security->unlockedActions = array('action_1','action_2'); // unlock actions which you want to call in ajax without ssl 
$this->Security->blackHoleCallback = 'forceSSL'; //call forceSSL action to redirect http:// uri to https:// 

// Defination of forceSSL function 
public function forceSSL() {
			return $this->redirect('https://' . env('SERVER_NAME') . $this->here);


Populate Select box dynamically by ajax in Cakephp

To populate a select box by cakephp ajax functionality.
Step 1.
In the end of your view (.ctp)file.This is your javascript code in cake.

$this->Js->request(array('controller'=>'ControllerName','action'=>'ajax_call'), array(
'async' => true,
'method' => 'post',
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true))

Step 2.
In your controller file create function with that returns options string

public $helpers = array('Js'); //load Js helper
public function getBySubject(){
		$this->autoRender=false; //prevent to load default layout
//your code

step 3.
In the end or your layout file after your loaded scripts

if (class_exists('JsHelper') && method_exists($this->Js, 'writeBuffer')) echo $this->Js->writeBuffer();
// will write your cached scripts