Importing class in a nested folder - php

I am using CakePHP 2.0 and trying to import a class file in my app\Lib folder. Using App::Import('Lib','MyClass') works fine if the MyClass.php file is directly under app\Lib folder.
But my directory structure is more organized, its more like app\Lib\mypackage\folder. I tried couple of approaches
I including this path in bootstrap.php by using App::build.
I used App::Import('Lib','MyClass',array('file' => '/path/to/app/Lib/mypackage/folder/MyClass.php')
Both the approaches didn't seem to help. How to fix this?

App::import('Lib', 'package/folder/MyClass');

Related

Yii 1.1.16 : components folder is not accessible all over the application in Ubuntu

The error I'm getting is:
include(widgetHomeMenu.php): failed to open stream: No such file or directory
same for all other widget views I'm rendering on other pages.
The file is in my components directory. I guess the directory is not accessible because the file exists there. Plus I've given permission to all the files in /var/www.
Also in my config.php file, there is:
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
),
So, I guess the config is loading components as well.
Not sure where is the problem exactly. No problems with case sensitivity, its correct and the filename is correct as well. The application is working flawlessly on several Windows PC using xampp.
Will be thankful for any help.
I found the problem. It was ultimately related to case sensitivity even though I mentioned above that there was no such problem.
I was loading the widget like this:
<?php $this->widget('widgetDashboardMenu');?>
where I thought that widgetDashboardMenu was the name of the view that was about to render (my bad). This was actually the controller inside components named WidgetDashboardMenu which then loaded widgetDashboardMenu.
So, changing w to W solved the problem for me.
<?php $this->widget('WidgetDashboardMenu');?>

Can't import Vendor class into Component in CakePHP 2.5.1

I've been looking at all the related questions of this topic and none of the solutions provided (usually App::import() ) have worked for me, maybe because I have a different configuration, which is the following:
I have a regular cake installation which loads components from an external folder (so outside this installation). That works perfectly, even for the component I'm trying to use now (it works fine until I try to load the Vendor class). This Vendor class I want to have it outside the Cake installation as well (same as with the components). This is how this installation looks:
[root]
.......[shared_resources]
......................................[CakePHP]
........................................................[Components]
..............................................................................MyCustomComponent.php
........................................................[Vendor]
....................................................................[MyVendor]
......................................................................................MyVendor.php
......[MySite]
................... [cakephp typical folder structure]
In my site's bootstrap.php file I have App::build(array('Controller/Component' => array(dirname(ROOT) . '/shared_resources/CakePHP/Component/'))); in order to be able to load that component in any controller, which works fine, any component I put in that folder loads and works without issues.
Now, I'm trying to load the MyVendor class in the MyCustom component, but I can't get it to work, no matter what I try I keep getting class not found errors when trying to instantiate it.
I've tried using php's and Cake's require(), import(), App::import() and App::uses() with all possible combinations of paths (absolute and relative) without any success, puttin them before the declaration of the component class and inside the method that actually uses the vendor class. The last one I've tried is App::import('Vendor', '/absolute/path/to/shared_resources/Vendor/MyVendor/MyVendor.php'); for example.
I've also tried using App::build(array( 'Vendor' => array(dirname(ROOT) . '/shared_resources/CakePHP/Vendor/'))); in the bootstrap file, like with the components.
I don't know what else to try, any help would be much appreciated!!!
Oh, I've check with PHP that the file Vendor class file exists in that path too.
According to your folder structure,
To access your MyVendor.php, you should write like this
App::import('Vendor', 'MyVendor', array('file' => 'MyVendor/MyVendor.php'));
For more information, read http://book.cakephp.org/2.0/en/core-utility-libraries/app.html

Yii, importing library does not work

i'm trying to implement Identicon library to my site but i get the error that class is not found.
i tried with Yii::import('application.vendor.*'); so i put the library in vendor folder, but it does not work.
i also tried adding the library to the component controller but still gives me the error.
and i tried making the import in the view where the code will be
<?php
Yii::import('application.vendor.*');
$identicon = new Identicon;
$identicon->displayImage('test');
?>
and yet it tells me the error that this class is not found. i just copied the src folder from the zip to vendors and components. how can i import this library?
if your folder structure is like
-- root
-- protected
|---- vendors
|---- myfolder
|---- MyClass.php
you can import it like this
Yii::import('appplication.vendors.myfolder.MyClass');
since yiis auto load is based on file name, if class Identicon was defined in MyClass it wont get loaded because it has different file name, so in this case you have to go with:
Yii::import('appplication.vendors.myfolder.*' , true);
i finally imported Identicon library by editing every file from the library and store it into components/Identicon.
it seems like the use of namespaces that each file had on the code wasn't allowing Yii to import, so it works by deleting the namespaces and the use command on every file, then import it in the config file.
'import'=>array(
'application.models.*',
'application.components.*',
'application.components.Identicon.*'
),
Note: All files found in generator must be in the same folder that Identicon.php is place.
so you can use the library almost like the readme from Identicon says
$Identicon = new Identicon;
$identicon->displayImage('foo'); //Displays the image.

Trouble importing file with CakePHP

I'm trying to import phpQuery to one of my controllers but I'm getting some errors. It is saved in my Vendors folder and plugins folder.
I've tried adding following to my ItemsController and none have worked
include_once(__DIR__.'/vendors/phpQuery.php');
App::uses('phpQuery', 'Vendor');
App::import('phpQuery', 'Vendor');
All of which result in either an error of not finding the method or
Error: The application is trying to load a file from the phpQuery plugin
I even tried placing it in the plugins folder and I'm having now luck.
Importing Vendor files (especially if they do not stick to regular conventions, like one class per file), sometimes requires some variants of the 'normal' App::import()
This section in the documentation describes those variants: Loading Vendor Files
I tested this one, and this worked without problems;
App::import('Vendor', 'phpQuery');
(loads 'app/Vendor/phpQuery.php')

CodeIgniter - extend native library in multiple-site setup

I want to use a central CI setup for multiple sites. The way I handle this is I created a package called MPACK and added it to autoload in the config file of each site.
Folder Structure:
/main
/system (CI 2 System folder)
/MPACK
/site1
/application
site2
/application
Inside this MPACK I have share libraries, models, helpers, etc.
However, I would like to have an extended MY_Form_Validation that would be common to ALL sites. Adding the class file to /MPACK/libraries fails. Adding it to /site1/application works fine, as expected.
Is there any way to do this extending inside MPACK?
Thank you for your time.
Please try this:
// Placed your MY_Form_validation.php under MPACK/libraries
$this->load->add_package_path('/path/to/MPACK');
$this->load->library('form_validation');
You can get more information from CodeIgniter User Guide - Loader Class. :)
You can also autoload your package in /application/config/autoload.php : $autoload['packages'] = array('/path/to/MPACK');
EDIT: turn out that the above solution doesn't work, because Loader always look for APPPATH & BASEPATH first, and I not sure modifying this core class won't break something. Here is another solution in theory:
You should have your MPACK form validation lib, and sites' form validation lib should be symlinks to the MPACK one:
/site1/application/MY_Form_validation.php -> /MPACK/libraries/MY_Form_validation.php
If you just use everything from MPACK, nothing specifically for /site1 or /site2, just make a folder link:
/site1/application/libraries/ -> /MPACK/application/libraries/
Hope this help =)
You can read more here: http://codeigniter.com/wiki/Multiple_Applications_via_Symlinks/

Categories