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
Related
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();
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;
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
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.
I am getting the error Fatal error: Cannot redeclare config() (previously declared in.../basics.php:58, in live server. It works fine in my local server but when i uploaded the site to live server, i got the fatal error.
I checked if the config() was declared multiple times but it's only declared once in basics.php file.
The naming conventions are also followed, as it is working fine in local server. It only displays such error in live site.
Please suggest solution for this.
Help on this will be much appreciated.
set $uses to array() and $autoRender to false as follows will solve this problem
class IndexController extends AppController
{
public $uses = array();
public $autoRender = false;
public function index()
{
echo 'test';
}
}
I had this error running Apache, and it ended up being related to my .htaccess files being ignored - I had been reconfiguring Apache, and had accidentally set AllowOverride to None in my config file, which was somehow causing this error.
So, in brief, in either your http.conf or one of your site configs, make sure the AllowOverride in your relevant Directory section is set to All (or something other than None), here's mine:
<Directory /var/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
This can happen if "basics.php" was included multiple times. You can prevent it by using include_once/require_once instead of include/require.
But this doesn't explain why it's working on your local webserver.
Just in case somebody happens to search for this.
I had the same error on a windows 2003 server with cakephp 1.3.11 installed. In my case it was because I had a typo in one class association declaration.
I had defined a Client class with a hasOne association to a Account class. There I had wrongly typed the className property to Client which created a loop and resulted in the Cannot redeclare config() error in cakePHP.
Check your PHP version. that errors happens when you use PHP version 5.4 which is more stricter than 5.3
open your cake/bootstrap.php and make changes to make sure every file loaded once with function 'require_once'. The code will be like this:
require_once CORE_PATH . 'cake' . DS . 'basics.php';
require CORE_PATH . 'cake' . DS . 'config' . DS . 'paths.php';
require_once LIBS . 'object.php';
require_once LIBS . 'inflector.php';
require_once LIBS . 'configure.php';
require_once LIBS . 'set.php';
require_once LIBS . 'cache.php';
Configure::getInstance();
require_once CAKE . 'dispatcher.php';