Online Forum To Support Coders With Technical Assistance - Read Write Execute

Call Controller function from webroot file in Cakephp 3

Ankit Agrawal
Ankit Agrawal
Published on September 8, 2016 · 1 min read

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-

Cakephp 3.4

<?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');

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

?>

Cakephp 3.8

<?php 
namespace App\Controller;
use App\Controller\AppController;
use Cake\Core\Configure;
require '../vendor/autoload.php';
use Cake\Routing\Router;

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');
 
$app = new AppController();
$responseofappfunction = $app->yourAppControllerfunctionName();
print_r( $responseofappfunction ); 
?>
Was this article helpful?
Ankit Agrawal

Ankit Agrawal

Author & Senior Engineer at CodeExecute

Writing practical engineering guides, debugging real-world bottlenecks, and creating free tools for developer productivity.

← Previous Article Add two background color using css Next Article → Create Custom Helper and call from view in cakephp 3.x