This question already has an answer here:
How to create a PSR-4 autoloader for my project?
(1 answer)
Closed last year.
I am learning to compose and libraries. the folder where I find a problem is on:
https://github.com/raoufcode/comp
I installed Composer on my Windows, then I created local classes (src folder) and at the same time I imported a library (erusev / parsedown) via the command line.
the problem is that the library works fine when I instantiate it but my own class located in the Fag.php folder in the source folder does not work when I call with use in index.php.
When I require my class file, it works but I want composer to do the job dynamically
Any solutions for my own class so that it works with Composer please?
I think that when you say "dynamic instantiation" you want to mean class autoloading, So I supose that you should learn how to config the autoloader of composer. That's a link that could be useful.
How to Autoload Classes With Composer in PHP
Depending on your git repo, that you linked I can only find following problem:
You try to load the class before the autoloader registerd.
You index.php should look like this:
<?php
require_once 'vendor/autoload.php'; // only the autoloader needed to include
use Try\Fag; // use need to be after the auto loader
$fag= new Fag();
You already auto load your class with your composer config autload/psr-4
Related
I'm new to mpdf and I'm having a problem implementing it on my project. I saw a link in Facebook that has a tutorial for mpdf, I read and followed all the instructions but unfortunately there is still an error about some deprecated value.
Here is the link I followed : http://codesquery.com/convert-html-pdf-codeiniter-using-mpdf/
And here is the screenshot of the problem:
I hope you guys can help me.
I guess you use php 7. You must change names of function. In old php for example 5.19 the same name of function and class means that this function is a constructor. In PHP 7 it must be for example:
public function __construct(mPDF $mpdf){}
Click here to see a list of all modification
You can easily install mpdf by using composer in codeigniter. This is how I implemented composer in CodeIgniter 3.It is very easy. You have to install composer on your machine download it from https://getcomposer.org/. After installing composer in your pc,
Copy and paste composer.json file in the project folder to application folder.
In the config.php file $config['composer_autoload'] = TRUE;
Now you have composer in your project. Now i will saw you 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 in your controllers.
function m_pdf(){
$mpdf = new mPDF();
// Write some HTML code:
$mpdf->WriteHTML('Hello World');
// Output a PDF file directly to the browser
$mpdf->Output();
}
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. If you encountered any problems please comment below.
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
I want to use the following AntiXSS library in one PHP file. It is my first time using Composer, but I followed their installation steps and I installed it successfully. I downloaded the library through Composer, updated it and Composer created the vendor/ folder in my directory with the necessary files.
Now I added the following require 'vendor/autoload.php' into my PHP file. I created a new AntiXSS class but I obtain the following error:
Class AntiXSS not found in my directory on line 3.
I tried to use an absolute path instead of vendor/autoload.php but it isn't working yet and I don't know if I should do something more.
Best regards
The class is located in the voku\helper namespace. Use new \voku\helper\AntiXSS() to instantiate it or use use imports to import the namespace.
See php.net for more information about namespaces.
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.
I'm hoping someone can spot what I've forgotten to do. Here are my steps:
Downloaded and unpacked the ZendFramework-2.3.5 into /usr/share.
Updated include_path in my php.ini file to include '/usr/share/ZendFramework-2.3.5/library' per the INSTALL.md, and restarted Apache to confirm the path is set (now ".:/usr/share/php:/usr/share/ZendFramework-2.3.5/library").
Created a test script in my web document root (using the class 'CamelCaseToUnderscore' as an example):
use Zend\Filter\Word\CamelCaseToUnderscore;
$filter = new CamelCaseToUnderscore();
echo $filter->filter('BigsAndLittles');
...and I get the fatal error "class 'zend\filter\word\camelcasetoseparator' not found".
In order to do use Zend classes like this, do I need to do some additional configuration or create an autoloader or something to find them? Seems like this should have worked. If I include the CamelCaseToUnderscore.php file in a require_once statement, then I get a fatal error that it's parent class doesn't exist (CamelCaseToSeparator.php). What am I missing?
You can use require 'Zend/Mvc/Application.php' to test if your include path is correct, but you will need an autoloader:
http://framework.zend.com/manual/current/en/modules/zend.loader.standard-autoloader.html.
You can find an example here (lines 18-20):
https://github.com/zendframework/zf2/blob/master/demos/Zend/Feeds/consume-feed.php
I strongly suggest using composer as it will save you a lot of time troubleshooting your include paths, but it also allows you manage version better. It makes it easier for other developers and to deploy your code.
Starting with composer is very easy, just install it and create composer.json:
https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup
Run:
composer require zendframework/zendframework
Composer will download all libraries to vendor folder and will generate an autoloader, all you have to do is to include
require 'vendor/autoload.php';
https://getcomposer.org/doc/01-basic-usage.md#autoloading
Most popular PHP frameworks use composer for managing dependencies:
https://github.com/zendframework/zf2/blob/master/composer.json
https://github.com/symfony/symfony/blob/2.7/composer.json