Yii, importing library does not work - php

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.

Related

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

CodeIgniter Modular Extensions & the i18n library

I have been using the CodeIgniter i18n library by Jérôme Jaglale (http://maestric.com/en/doc/php/codeigniter_i18n), which works great for my project.
But since I need to write separate modules, I recently added CodeIgniter Modular Extensions ( https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc ) and the navigation breaks.
How can I solve this issue please, I would love to use both the i18n library & Modular Extensions.
I think my application navigation fails to work because i18n library introduces adds a language segment in the site url, in my case my url is localhost/index.php/en/home and after adding Modular Extensions, my navigation/links stop working.
Thank you in advance.
Recently, I try to use HMVC with i18n and have similar problem. Below is my solution.
1.first you need to go to here HMVC select "Branches" to download HMVC extension, don't download the one on github it might not work.
2.Unzip HMVC extension inside the folder copy two files "MY_Loader.php" and "MY_Router.php" from core folder to Codeigniter's "application/core" after that copy "MX" folder from "third_party" to Codeigniter's "application/third_party". By this point your HMVC is installed, however it will not work because i18n cause the problem so if you run your website it might not display.
3.You need to get new version of i18n which support both HMVC and none HMVC, the old version of i18n seems not support HMVC. Go to here i18n download it and take time to read the description on github.
4.Before this step I suggest you to backup "application/core/MY_Config.php" and "application/core/MY_Lang.php" in case something goes wrong you can reverst back. Unzip i18n inside folder copy file "language.php" from config folder to Codeigniter's "application/config", copy two files "MY_Config.php" and "MY_Lang.php" from core folder to Codeigniter's "application/core", finally copy "MY_language_helper.php" from helpers folder to Codeigniter's "application/helpers". So far you have new i18n installed, but you need to configure it to make it work, otherwise you might get error message.
5.Open "application/core/MY_Config.php" and replace the line require_once APPPATH . "libraries/MX/Config.php"; to require_once APPPATH . "third_party/MX/Config.php"; then open "application/core/MY_Lang.php" replace the line require APPPATH . "libraries/MX/Lang.php"; to require APPPATH . "third_party/MX/Lang.php";. Why? because it point to the wrong directory, the MX folder is located in "third_party" not "libraries" in case you don't know, if you don't change it you might get error message.
6.To add new language(Not create language file) you need to open "application/config/language.php". You see at top the code block with comment says "Supported Languages" there already have English and Russian language configure for you just need to copy the template and change to the language you want, it's very easy. Be aware folder's name must exactly the same as folder in "application/language".
7.According to i18n github description you need to add these line
$route['^(en|de|fr|nl)/(.+)$'] = "$2";
$route['^(en|de|fr|nl)$'] = $route['default_controller'];
to the "application/config/routes.php". Be aware this line $route['^(en|de|fr|nl)/(.+)$'] = "$2"; in old i18n probably is $route['^(en|de|fr|nl)/(.+)$'] = "$1"; the difference is "$1" have to change to "$2", otherwise you will got problem.
8.To create language file is same as the method you did in old i18n. Now test your website with multi-language to make sure everything work fine.
9.Create your module. How? Create a folder name "modules" inside Codeigniter's application folder, inside modules folder you can start to create your module. That's say you want to create a module call foo, you just need to create a folder name it "foo" and then inside foo folder you can create three folders controllers, models and views. Create a php file with name foo with the code below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Foo extends MX_Controller
{
public function index()
{
echo "<h1>class foo this is module test</h1>";
}
}
Enter url to run your module, if you see "class foo this is module test" then it work.
Remember module class must extends from MX_Controller.
If you still encounter any problem just ask.

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

Yii import functionality

In my main config i have:
'import' => array(
'application.models.*',
'application.components.*',
I read somewhere that Yii import only calls the relevant class when needed.
Wanted to know if it's true and if importing all the folder at once is good practice in Yii.
Thanks,
Danny
In the import call, when you pass in a folder like 'application.models.*', what Yii does, its add that path to the php include_path, so that when you call a class contained in that folder, if the Yii autoloader fails php looks for that class in the include path, thats fast.
If you include a file like 'application.models.FormModel', then Yii autoloader is aware of it and also loads the class on deman only.
you can find more information on the Yii guide: importing classes

Importing class in a nested folder

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

Categories