How to autoload a custom class in Zend Framework 2? - php

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.

Related

How to use one module in two different application/project in yii2

I have module created in the basic project of yii2 and now i want to access or use that module another project/application of mine....
How can I achieve this.
please help me out here.
To use module in different apps there are 3 things you need.
The module must not be dependent on classes from core project. For any class that needs to be implemented by core project the module should define interface and depend on that interface instead of class itself.
The module should use different namespace than app and autoloader must know how to load classes from that namespace. (more about that later)
You have to add module in your config in same way you've added it in first project.
The points 1 and 3 are pretty much self-explaining. If are not sure how to add module in config see the yii2 guide.
Now back to the second point. While naive way of copying module over to second project would work it will turn maintaining the module into nightmare because each change would have to be done in each copy of module. So it's better to keep the code of module in one place and make it available for each project. There are multiple ways of doing that.
If you want to, you can turn your module into extension and make it publicly available through packagist as it was suggested by M. Eriksson in comments. After that you would simply add your extension through composer as any other dependency.
Composer also allows you to define and use private repositories if you don't want to publish your module at packagist. See composer documentation for more details.
The most trivial way is to simply put the code into separate folder outside of project. If you do that, you have to make sure that autoloaders in your projects are capable of finding the files locations to load classes. There are two options how to do that. In any case you will want to avoid conflicts with namespaces used by your project, that's why you need to use different namespace.
Let's assume that you've put your module files into folder /path/to/modules/myModule and all classes in your module belongs to namespace modules\myModule. You have to make sure that your webserver can access that folder and that it can run php scripts there.
First option is to use Yii's autoloader. That autoloader uses aliases to look for classes. If you add #modules alias and point it to /path/to/modules folder, the Yii autoloader will try to look for any class from modules\* namespace in /path/to/modules folder. You can add the alias in your config file (web.php, console.php or any other config file you use):
return [
// ...
'aliases' => [
'#modules' => '/path/to/modules',
// ... other aliases ...
],
];
The second option is to use project's composer.json file to set autoloader generated by composer to load your classes.
{
"autoload": {
"psr-4": {
"modules\\": "/path/to/modules"
}
}
}
You can find more info about this in composer's documentation.
Don't forget to run composer dump-autoload after you change autoload settings in your composer.json file to update the generated autoloader.

autoloading nested classes with composer

I have created a very basic validator class.
My base code is in a my src/ folder, which gets autoloader with
"kevdotbadger\\Validator\\": "src/"
this works fine, so that when I instantiate a new "kevdotbadger\Validator\ Validator is gives me src/Validator.php
My Validator.php class then loads a bunch of sub-classes in my src/Rules directory. These are magically loaded by using the __call, so ->between() should look for src/Rules/between.php. However, for some reason it won't usual load despite it being setup in my composer.json file.
My whole codebase is available at https://github.com/kevdotbadger/validator/
Have I setup my namespace correctly? I think the problem might be with php version 5.3, however I need to use version 5.3.
Thanks.
Well you need to keep the guidelines of psr-4 as you are using it to autoload.
change the folder name "rules" to "Rules"
Uppercase all your file names of classes like:
between.php --> Between.php
that should do the job

How to use ZF2 ZendPdf in Yii Framework

I want to parse a PDF document in my project, I'm using Yii framework and I'm able to load ZendFramework 1.12, actually I'm using Zend_Lucene and Zend_Mail successfully, but ZendPdf fails to parse the PDF, so I wanted to try with ZF2 (ZendFramework 2), but I'm not able to make it work... I just downloaded the ZF2 library and added the following to my base Controller:
public function __construct($id,$module=null){
Yii::import('application.vendors.ZendFramework.library.*');
Yii::setPathOfAlias('Zend',Yii::getPathOfAlias('application.vendors.ZendFramework.library.Zend'));
parent::__construct($id,$module);
}
So every time I use a Controller this code is executed. Then in the controller I have:
use ZendPdf\PdfDocument;
use Zend\Mail;
class AjaxController extends Controller{...
...
public function actionTestPdf(){
$filepath = realpath('./path/').'/pdftest2.pdf';
$pdf = PdfDocument::load($filepath);
}
When I run the controller: /ajax/TestPdf
I get: Fatal error: Class 'ZendPdf\PdfDocument' not found
What am I doing wrong?
I'd suggest you to install your ZF2 stuff with composer and include the autoloader from composer to get PSR compatible autoloading.
Alternative 1)
You may also try pointing an alias ZendPdf\PdfDocument to the class file location.
Alternative 2)
import the needed classes or directories in your application config.
In this particular case I would recommend NOT using ZendPdf from ZF2, because it is functionally equivalent to Zend_Pdf from ZF1 and so all you're going to do is add unnecessary complexity with classloading, etc, but not solve your underlying problem, as the document will still most likely NOT load using ZendPdf from ZF2.
You should investigate the underlying reason why the document is failing to load in ZF1.

Using ProcessBuilder in a Silex Project

I am wanting to use the Symfony\Component\Process\ProcessBuilder class and can see that it is included as part of the Silex codebase within the vendors folder. I am using the Silex phar file and assume that because I can readily instantiate other Symfony components like Request, Response and so on that it will correctly locate the file to include when I use the full namespace.
$foo = new Symfony\Component\HttpFoundation\Request(); //works fine
However, when I try and create and instance of it using:
$foo = new Symfony\Component\Process\ProcessBuilder(); //class not found
It gives me a class not found error. Does anyone know why this is and how I can use this class from the Silex phar without including the component seperately within my project?
It looks like the Process Symfony component is not included in the compiled Silex phar file.

php zf project: db classes in models directory do not get auto-included

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.

Categories