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->validatePost=false;
$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:// 
$this->Security->requireSecure();

// Defination of forceSSL function 
public function forceSSL() {
		if(json_decode($_SERVER['HTTP_CF_VISITOR'])->scheme=="http"){
			return $this->redirect('https://' . env('SERVER_NAME') . $this->here);
		}

	}
Share on Google+Share on FacebookTweet about this on TwitterEmail this to someone
  

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.

<?php
$this->Js->get('#select1')->event('click', 
$this->Js->request(array('controller'=>'ControllerName','action'=>'ajax_call'), array(
'update'=>'#select2',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'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
$option;		
//your code
		printf($option);
		}

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

<?php
if (class_exists('JsHelper') && method_exists($this->Js, 'writeBuffer')) echo $this->Js->writeBuffer();
// will write your cached scripts
?>
Share on Google+Share on FacebookTweet about this on TwitterEmail this to someone
  

Create Custom Helper and call from view in cakephp 3.x

To create a custom helper in cakephp first create a file in Helper folder and Helper classes should be suffixed with ‘Helper’.

example- (YourProject/src/view/Helper/CustomHelper.php)

CustomHelper.php is our new helper with Helper suffix.

Now in CustomHelper.php you can create function like this example

<?php
namespace App\View\Helper;

use Cake\View\Helper;

class CustomHelper extends Helper {


	
	public function yourhelperfunction()
	{		
		//your function code here
	}
}
?>

How to call custom helper function in view file

To call custom helper function from view file use

  $getresponse = $this->Custom->yourhelperfunction();
Share on Google+Share on FacebookTweet about this on TwitterEmail this to someone
  

Call Controller function from webroot file in Cakephp 3

In our example we call AppController function from webroot file. you can call any controller function from webroot file.

To Call Controller function from webroot file in Cakephp create a new file on webroot and paste this code-

<?php
namespace App\Controller;
use App\Controller\AppController;

if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}

require_once(dirname(dirname(__FILE__)).DS.'config'.DS.'bootstrap.php');
require_once(dirname(dirname(__FILE__)).DS.'src'.DS.'Controller'.DS.'AppController.php');

$user = new AppController();
$responseofappfunction = $user->yourAppControllerfunctionName();

?>
Share on Google+Share on FacebookTweet about this on TwitterEmail this to someone
  
Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.