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
 

How to Remove Mailed-by Header in CakePHP?

Mailed-by header remove in cakephp:

  1. Through Email component:
    $this->Email->additionalParams = '-fnoreply@yourdomain.com';

     

  2. Through CakeEmail Class:
    $Email = new CakeEmail(array('eol'=>PHP_EOL,'additionalParameters' =>'-fnoreply@yourdomain.com'));
Share on Google+Share on FacebookTweet about this on TwitterEmail this to someone
 
 

How to add google recaptcha on login form in cakephp

(1)First register your domain and create site key and secret key here

(2)Open file app/Config/bootstrap.php and add following setting

<?php
	Configure::write('google_recatpcha_settings', array(
    'site_key'=>'your-domain-site-key',
    'secret_key'=>'your-domain-secret-key'
));
?>

(3)add following script in your layout file app/View/Layouts/default.ctp

<script src='https://www.google.com/recaptcha/api.js'></script>

(4)Open app/View/Users/login.ctp and add following html inside login form

<div class="g-recaptcha" data-sitekey="<?php echo Configure::read('google_recatpcha_settings.site_key'); ?>"></div>

(5)add following function in app/Controller/AppController.php

<?php
 public function verifyRecatpcha($aData)
 {
	if(!$aData)
	{
		return true;
	} 
	$recaptcha_secret = Configure::read('google_recatpcha_settings.secret_key');
	$url = "https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$aData['g-recaptcha-response'];	
	$response = json_decode(@file_get_contents($url));			
				
	if($response->success == true)
	{
		return true;
	}
	else
	{
		return false;
		
	}	
 }	 
?>

(6)Open file app/Controller/UsersController.php and add following code in login function after post values

<?php
if(!$this->verifyRecatpcha($this->request->data))
{
	$this->Session->setFlash(__('Captcha incorrect.'),'default',array('class' => ''));
	return $this->redirect('login');
}
?>
Share on Google+Share on FacebookTweet about this on TwitterEmail this to someone
 

How to create profile url (http://sitename/username/) in cakephp

(1)Create a profile controller app/Controller/ProfileController.php and write following code

<?php
App::uses('AppController', 'Controller');

class ProfileController extends AppController {


	public function index() {
		
		 $username = $this->params['username']; 
	 
		 
	}
}
?>

(2)create a view file app/View/profile/index.ctp

(3)Open file app/Config/routes.php and add following line

<?php
	Router::connect('/:username', array('controller' => 'profile', 'action'=> 'index')); 
?>
Share on Google+Share on FacebookTweet about this on TwitterEmail this to someone