I developing a project using Zend framework and I came across the following problem. I'm using Zend framework MVC folder structure generated using their zf.sh script.
My library folder has the Zend library folder and it's classes can be called normally inside the application. I created another folder inside my library for my classes. This is the folder structure now:
MyProject
|_application
|_docs
|_public
|_library
|_Zend
|_Buyers
|_Donations.php
|_scripts
I named my Donation class "Buyers_Donations" as the Zend framework naming convention.
When I tried using this class inside my controller
$obj= new Buyers_Donation();
it gave an error can not find class Buyers_Donation inside the controller.
But when I added the following line in my Bootstrap it worked:
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
$moduleLoder = new Zend_Application_Module_Autoloader(
array(
'namespace'=>'',
'basePath'=>dirname(__FILE__)
));
Could someone please explain what actually happened and what is the use of the module autoloader although I don't have any modules in my application ?
As you suspected, you shouldn't be using the module autoloader since you're not using modules. Assuming the Zend* classes are autoloading correctly for you, all you need to do is tell the standard autoloader that it should also be used for classes in your 'Buyers' namespace. So instead of the code snippet you posted, just do:
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Buyers_');
you can also set this in application.ini if you prefer.
I'm also assuming that your classes are in the library folder, and not in the public directory as your question implies (this would be bad).
If you do not wish to use the zend's auto loading feature, you will have to include files manually by using require_once(), such as:
require_once 'Buyer/Donations.php';
If you do wish to use zend loader with your own library code that uses your own namespace, you may register it with the autoloader using the registerNamespace() method. in the bootstrap, you could do so as follows:
protected function _initAutoload()
{
$autoloader = Zend_Loader_Autoloader::getInstance()->
registerNamespace('Buyers_')
return $autoloader;
}
If the auto loader doesn't work, make sure you set the include path to the library folder somewhere. It's automatically added by the zend framework to public/index.php:
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
Related
I'm using zend framework2 skeleton, and I'm trying to add my own classes behind vendor folder. The problem is: I don't know how to prepare the classes to be loaded on demand (autoloaded) only when I'll instantiate one of them from any module.
I tried with the solution explained at: How to load a custom library in Zend Framework 2? , but I'm getting the same error:
Fatal error: Class 'myNamespace\myClass' not found in path\IndexController.php on line x
My question is: Where and how should I insert my own classes to be later used from my modules?
Firstly, if they are your own classes and directly related to the modules (i.e not some custom library you have written) then you are better off having them in the actual module, not in the vendor folder.
However, if you are trying to include your own library that you have written I'd suggest you consider storing this elsewhere and adding it as a dependancy via composer.
I don't recommend this but you should be able to get the autoloading working by using the psr0 autoloader built in to composer (assuming your classes follow psr0). Example composer.json config:
{
[...]
"autoload": {
"psr-0": {
"MyNameSpace": "path/to/root/src/directory"
}
}
[...]
}
I know its a bit old but i was facing the same issue so what i did was:
inside vendor folder i created a new folder and gave it the name of Custom. Inside that folder i added my class file (MyClass.php).
inside MyClass.php i added this line (namespace Zend\Custom;).
And inside my Controller at the top
use Zend\Custom\MyClass;
and within a method of that controller
$someVar = new MyClass();
$someVar->someClassMethod();
I hope this helps.
I'm trying to install Zend PDF in order to fill out editable PDFs on my client's shared hosting account (media temple). I have it enabled now, confirmed in phpinfo http://i.imgur.com/lDiLk.png but after that, I can't find out what I need to get started. If I try loading the Zend_Pdf class, I receive a "Fatal error: Class 'Zend_Loader' not found" message.
These are very different. Zend Guard Loader is used to run PHP scripts encoded by Zend Guard. And Zend Loader component is used, among other things, to simplify the development. For example, this...
$pdf = new Zend_Pdf(); // what's Zend_Pdf, people?
... statement is meaningless to PHP unless it knows what is Zend_Pdf class. Thankfully, in PHP there's a special mechanism of importing these files automatically - autoloading. Here's how to do it with Zend_Loader:
set_include_path(
implode(PATH_SEPARATOR, array(
get_include_path(),
PATH_TO_ZF_LIBRARY
)));
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
...
$pdf = new Zend_Pdf(); // no error, as correct file should be imported automatically
Actually, if it's only a single file that will use Zend component, it's not required that you use the autoloading mechanism. A simple require_once (no pun intended) would suffice:
require_once 'Zend\Pdf.php';
...
$pdf = new Zend_Pdf(); // oh, now I know all about Zend_Pdf!
Zend Guard Loader and Zend Loader are not the same thing. Zend Guard is another Zend product and has nothing to do with Zend Framework. Zend Loader is the name of the class loader within Zend Framework.
Make sure that you have a copy of Zend Framework within your application and that you've setup the include path to point at this location.
I have been trying to write a Controller Plugin which I will be using for user authentication.
I have written the plugin and it should work but I just don't get how to get the plugin loaded... I have read that the Zend Framework has a lot of autoloading possibilities..
My current directory structure:
domains
example.com
Application
configs
controllers
IndexController.php
AuthController.php
ErrorController.php
forms
layouts
scripts
layout.phtml
models
plugins
AuthenticationPlugin.php
views
helpers
scripts
auth
login.phtml
error
error.phtml
index
index.phtml
Bootstrap.php
library
Zend
pubic_html
.htaccess
index.php
Can anyone help me?
Thanks in advance!
Assuming your appnamespace is Application_, then your plugin class should be:
named Application_Plugin_AuthenticationPlugin
stored in the file application/plugins/AuthenticationPlugin.php
registered with the frontcontroller using something like (in application/configs/application.ini):
resources.frontController.plugins.auth = "Application_Plugin_AuthenticationPlugin"
You could create your own library folder with a similar folder structure to Zend's. For instance (assuming your own namespace My_):
library
My
Controller
Plugin
Authentication.php
Authentication.php would contain a class named My_Controller_Plugin_Authentication.
You would then register the namespace in your bootstrap (manual):
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('My_');
Failing that, you could use the structure above using the resource autoloader (manual). Zend Framework expects that classes in those folders are namespace prefixed too, so your class name would be Plugin_AuthenticationPlugin.
Add the following line in for example your Bootstrap file:
Zend_Controller_Front::getInstance()->registerPlugin(new AuthenticationPlugin());
How to use zend library without using zend framework installation?
I am trying to use zend library(Mail and Mime) without zend framework installation, its not returning any error messages...
but for my project i'm using Mail and Mime library only, How to use Zend Library without installing zend framework ..
Thanks,
Vinoth S
Download Zend Framework and put it into a folder accessible by your PHP. Then either do
include '/path/to/folder/containing/Zend/lib/Zend/Mail.php';
include '/path/to/folder/containing/Zend/lib/Zend/Mime.php';
$mailer = new Zend_Mail;
Or - better and more conventient - setup your autoloader and/or include path so PHP can find the classes directly, without you having to include them.
Also see
the requirements appendix for a detailed list of requirements for Zend Framework.
Register the autoloader and set include path like this:
set_include_path(implode(PATH_SEPARATOR, array(
realpath('./library'),//the path
get_include_path(),
)));
require "Zend/Loader/Autoloader.php";
$autoloader = Zend_Loader_Autoloader::getInstance();
I've done it more than once to integrate zend libs in other non-zend projects.
Autoloader is not suggested for just inclusion of some libraries as it involves in worse performances (see zend reference about |end_Loader for that).
The best way (from both clear code and performances point of view) is very simple:
1) set the include path: (necessary or you'll have fatal inclusion errors):
set_include_path(implode(PATH_SEPARATOR, array(
'/',
get_include_path(),
)));
2) do a "require_once" of the library/ies you need, following the structure Zend/
e.g:
require_once "Zend/Mail.php";
//you can use now Zend_Mail* classes
note1: you don't have to place a "require_once" of all the needed classes, the main included class already do a require_once of the dependent classes.
I'm trying to create a Zend Framework project using PHP 5.3.2 and Zend Framework 1.10.3.
i created the source files of the project using the zend framework tool and the db related classes i created with zend-db-model-generator.
example:
in models directory i have the following:
FbUser.php - class Default_Model_FbUser
FbUserMapper.php - class Default_Model_FbUserMapper
DbTable/FbUser.php - class Default_Model_DbTable_FbUser
The models in the models directory should be included automatically when I use them in one of the controllers, but it seems that it doesn't.
what should i configure in order for the db class models will be auto-included when used ?
update
I tried adding the following code to the bootstrap file:
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Default_');
the autoloader still looks for Default/Model/FbUser.php in include path instead of Model/FbUser.php in the zf project.
I did not need to use Zend_Loader_Autoloader at all, although it's a cool component to auto-load classes in your include path.
i need to add to application.ini which is the main config of the application, the following line:
appnamespace = "Default_"
The program understands the application name space and then loads the db model classes properly.