How to include a class from outside of cakephp folder - cakephp

Please see my current file structure
CakePHP
- bin
- config
- src
- vendor
- webroot
RowPHP
- push.php
I want to import/include Push class into my cakephp2 application which stands at push.php file outside of cakephp
Which I have tried
require_once( ROOT . DS . '..' . DS . 'RowPHP'. DS . 'push.php');
$pushOb = new Push();
it include successfully but when I try to create a object it through error
Fatal error: Class 'App\Controller\Push' not found
Question: How to import/include this class into my cakephp application ?

You need to ensure PHP can find the class in the correct namespace using new \Push() (note the backslash before the class name):-
require_once( ROOT . DS . '..' . DS . 'RowPHP'. DS . 'push.php');
$pushOb = new \Push();

Related

Cakephp class not found

Hi i am using cakephp 2 . i am including external php folder in app/webroot
and in my controller used following to include the file .
include_once(ROOT . DS . 'app' . DS . 'webroot'.DS . 'socio'.DS . 'index.php');
The file is getting included but i am getting error Cannot redeclare class App .
i know i have declared App class twice . Since it is external folder having many dependencies i cant change class name . Please help me to find solution
Can you use something like this, WWW_ROOT is a Core Definition Constant and gives full path to the webroot.
include_once(WWW_ROOT . 'socio'.DS . 'index.php');
https://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html

How to implement dompdf in cakephp 3?

I just making website that can make html file into pdf using cakephp 3 as php framework.
this is my code in layout as default.ctp for pdf view that i want to convert
<?php
require_once(APP . 'Vendor' . DS . 'dompdf' . DS . 'dompdf_config.inc.php');
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->set_paper = 'A4';
$dompdf->load_html(utf8_decode($content_for_layout), Configure::read('App.encoding'));
$dompdf->render();
echo $dompdf->output();
when i try to run it, it some error like this
Error: Class 'Configure' not found
File C:\xampp\htdocs\MyProject\src\Template\Layout\pdf\default.ctp
Line: 6
is my syntax for calling dompdf is not right?
It is not dompdf error.
It is because of this Configure::read('App.encoding').
Write this on top of your file and your code should work.
use Cake\Core\Configure;

cakephp3 load library inside a method

Just migrated a system to the new cake3.
However now due to namespaces, struggling to dynamically load libraries inside methods.
I have a controller
with this in :
require_once(APP . 'Lib' . DS . 'Channel' . DS . 'Channel1.php');
require_once(APP . 'Lib' . DS . 'Channel' . DS . 'Channel2.php');
require_once(APP . 'Lib' . DS . 'Channel' . DS . 'Channel3.php');
require_once(APP . 'Lib' . DS . 'Channel' . DS . 'Channel4.php');
use Channel\Channel1;
use Channel\Channel2;
use Channel\Channel3;
However I don't want to load all the libraries unless I need them. Any suggestions for a good solution?
If you put the files in src/Channel instead ofsrc/Lib/Channel then the autoloader will find automatically your files when you do
use App\Channel\Channel1;
It will require that you set the namespace of the class to App\Channel
You can also tell composer how to autoload your custom namespace. In your composer .json's autoload section, under psr-4, do:
"Channel\\" : "./src/Lib/Channel"
And finally execute composer dumpautoload

Getting error while hosting a cakephp app

I am getting an error while hosting a cakephp app:
Fatal error: Can't find application core file. Please create /home/home/a1808794/a1808794/home/a1808794/app/Config/core.php, and make sure it is readable by PHP. in /home/a1808794/lib/Cake/Core/Configure.php on line 78
Failed opening '/home/home/a1808794/a1808794/home/a1808794/app/Config/core.php' for inclusion (include_path='/home/a1808794/lib.:/usr/lib/php:/usr/local/lib/php') in /home/a1808794/lib/Cake/Core/Configure.php on line 77
The index.php
if (!defined('ROOT')) {
//define('ROOT', dirname(dirname(dirname(__FILE__))));
define('ROOT', dirname(dirname(dirname(__FILE__))).DS.'home'.DS.'a1808794');
}
/**
* The actual directory name for the "app".
*
*/
if (!defined('APP_DIR')) {
define('APP_DIR', basename(dirname(dirname(__FILE__))).DS.'home'.DS.'a1808794'. DS .'app');
}
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
if (function_exists('ini_set')) {
/*ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));*/
ini_set('include_path','home'. DS . 'a1808794'. DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
}
I'm not sure why you think it was necessary to modify the index.php file and replce this:
define('ROOT', dirname(dirname(dirname(__FILE__))));
With this:
define('ROOT', dirname(dirname(dirname(__FILE__))).DS.'home'.DS.'a1808794');
And replace this:
ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
With this:
ini_set('include_path','home'. DS . 'a1808794'. DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
And set the app dir to:
define('APP_DIR', basename(dirname(dirname(__FILE__))).DS.'home'.DS.'a1808794'. DS .'app');
Because that is breaking the includes. Your app is now trying to include /home/home/a1808794/a1808794/home/a1808794/app/Config/core.php which is obvisouly not a valid path. Restoring the original index.php should work just fine. If it doesn't, please state what the actual problem was that made you modify the index.php in the first place. The index.php should work out of the box and I have not had to modify it ever.

Fatal error: Class 'Configure' not found in ..\cakephp\2.3\lib\Cake\bootstrap.php

Error message:
Fatal error: Class 'Configure' not found in C:\wamp\cakephp\2.3\lib\Cake\bootstrap.php on line 163
I have seen other answers on this issue (like this one), all hinting at ensuring I am not missing any files, but I have added all files and folders from a fresh download of CakePHP v.2.3 via Github.
Can anyone shed any light on why I am still getting an error like this?
Key lines of code shown below:
webroot/index.php
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
define('CAKE_CORE_INCLUDE_PATH', '../../../cakephp/2.3/lib');
}
if (!defined('CORE_PATH')) {
if (function_exists('ini_set') && ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS . PATH_SEPARATOR . ini_get('include_path'))) {
[...]
define('CORE_PATH', null);
} else {
[...]
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
}
}
lib/Cake/bootstrap.php
App::uses('Configure', 'Core');
Configure::bootstrap(isset($boot) ? $boot : true);
You seem to have an old version of index.php
Use the current one available at github.
It contains
if (!defined('CAKE_CORE_INCLUDE_PATH')) {}
instead of your
if (!defined('CORE_PATH')) {}
Where is DocumentRoot pointing to ?
If you load (e.g.) /index.php then your include_path could be incorrect and one directory too deep.
Use instead:
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
define('CAKE_CORE_INCLUDE_PATH', '../../cakephp/2.3/lib');
}

Resources