Zend Framework 2 load class PHPGangsta_GoogleAuthenticator - php

I'm trying to load a class that has an underscore in it in ZF2.
This is the project I want to use: https://github.com/PHPGangsta/GoogleAuthenticator
The folder paths look like this:
/module
/Application
/Service
/MyService.php
/vendor/
/PHPGangsta
/GoogleAuthenticator.php
GoogleAuthenticator.php has a class named PHPGangsta_GoogleAuthenticator which I want to use in MyService.php, without having to require any files.
Also, I cannot change files inside PHPGangsta, because the project is submoduled under git.
Can you help configure zf2 class autoload?

Assuming you installed ZF2 using Composer (which is the recommended method), edit your composer.json to add phpgangsta/googleauthenticator to the require section. Then run composer install. That's it. You should then be able to use the library in your application - you don't need to do any additional autoload configuration.

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.

Yii2 - extension structures

Recently I added a telegram bot extension in my Yii2 application to use it. but actually it is not a Yii2 extension but its a normal php namespace structured files and classes.
the name of this telegram extension is irazasyed/telegram-bot-sdk actualy its the name that added to my composer.json. I want to know how can I make some classess like this extension?
the irazasyed/telegram-bot-sdk structure is like this:
vandor >>
irazasyed
telegram-bot-sdk
composer.json
license
src
class1.php
class2.php
and the class files can be accessed from the namespace like \Telegram\Bot\Api from any controller in my application.
i want to know how can I make something like this myself.
I want this structure:
vendor >>
myCustomName
myCustomPakageName
composer.json
license
src
Class1.php
Class2.php
and access the class files from this namespace \something\somethingElse\Class1;
how can I do this?
Just add your files in a folder (or folders) in the project folder and use suggested root namespace. You don't need composer.json since you don't need to install it using composer. And you should not put it in vendor folder.
Example for basic project template:
- put everything in myCustomName folder in application root folder,
- set namespaces for each class like app\myCustomName (+ whatever subfolder you are using)
Example for advanced project template:
- put everything in myCustomName folder in selected application root folder (like frontend or common),
- set namespaces for each class like frontend\myCustomName (or common instead of frontend or whatever + whatever subfolder you are using)

Adding composer into Codeigniter

I am using Codeigniter since a year and I also started learning Laravel recently and I noticed that having a composer in your framework really helps you in a many ways.
I notice that the Codeigniter 3 has this option in config.php file to add a composer in it.
$config['composer_autoload'] = TRUE;
so I am thinking to add a composer in my CI.
Is it best practice to add a Composer in CI?
What should be the directory structure for that and is it work smoothly with the CI?
This is how I implemented composer in CodeIgniter 3.It is very easy. You have to install composer on your machine and I think you have it because you use laravel.
First copy and paste composer.json file in the project folder to application folder
Secound in the config.php file $config['composer_autoload'] = TRUE;
Now you have composer in your project. I will saw you an example like how to install mpdf using composer
Open cmd and direct to application folder
Inside application directory Type composer require mpdf/mpdf
Now a vendor folder will be created inside application folder and inside vendor
folder you can see all your packages downloaded by composer.
Now since you autoloaded composer now you can just use the code given by mpdf official manual like
function m_pdf()
{
$mpdf = new mPDF();
// Write some HTML code:
$mpdf->WriteHTML('Hello World');
// Output a PDF file directly to the browser
$mpdf->Output();
}
You can install any packages in https://packagist.org/ like mpdf very simply like this. Remember you don't need to type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php'; since you already autoloader composer. If not prefer to autoload composer you must type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php' at the beginning of each controllers where you use the mpdf vendor libraries.
in my experience, the best way is installing composer and dependencies independent of CodeIgniter, like 'composer require ', and then give access to the packages in your base controller.
add a file MY_Controller.php to application/core folder and make it extend CI_Controller. then add require_once(APPPATH . 'vendor/autoload.php') at the top. make all your controllers extend this base class, and you have access to all your packages. the same goes if you wanna access them in your models. make the base class MY_Model.php

"Class HelloController not found" error on composer autoload config with psr4

I try to create my own composer library. I've chose to use psr4 for autoloading mechanism. It works fine with the library project but something goes wrong when I add this library to another project as dependency. I expect the library project create an instance of a class which is located in main project. However this class cannot be found by composer autoloader.
My library project source is here : https://github.com/brnogz/kwinsey
My example project which uses this library like that(HelloWorld class is located in controller/HelloWorld.php file) : https://gist.github.com/brnogz/e27a1dd40ba00b818b23fe7ab8815fad
Please move all your sources to a src subfolder and use "src/" as the PSR-4 target folder. Autoloading from a project root folder is pretty much undefined behaviour.

app structure in Laravel 4 and class does not exist

I have structured my app as the following
note: "MyCMS" is a folder inside the "app" directory of laravel root directory
http://paste.laravel.com/POt
the problem is that whenever i try to access the route "admin/users"
i get the following error
Class \MyCMS\Admin\UserController does not exist
any ideas where the problem might be ?
In your UserController.php, it should be
namespace MyCMS\Admin\Controllers; not namespace MyCMS\Admin;
And a side note, I don't think you can just call $this->package('MyCMS/Admin'); when it's in your app directory. If you look into laravel's source code for the package method, it's actually guessing the path for your "package" and going two levels up to get the resources. It's a very specific file structure.
sometime we face this issue then we should use <?php instead of <? in UserController.php
Then use composer du or composer dump-autoload
The reason for your issues is quite simple. Packages don't go in the app directory of Laravel.
If you want to create a package, do so with the following command:
php artisan workbench vendor/package --resources
This will then create a workbench folder in your Laravel installation, where a pre-defined directory structure will be at the ready for your newly created package.
/src
/Vendor
/Package
PackageServiceProvider.php
/config
/lang
/migrations
/views
/tests
/public
You may then register the provider by adding it to the providers array in the app/config/app.php file. After a composer dump-autoload your package should be ready to use in your app.
See this section in Laravel's documentation on how to create packages properly.

Categories