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.
Related
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
I'm working on a native PHP project. Using terminal in the root I added 2 github libraries and they get saved in the vendor folder. In a sub folder in the root in a PHP file I add
use nadar\quill\Lexer;
$lexer = new Lexer($row['reply']);
echo $lexer->render();
But I get
Uncaught Error: Class 'nadar\quill\Lexer' not found in /home/USER/public_html/production/filename.php:469
I tried another library and get the same error Cannot find the class. How to solve this? Tis is the first library url: https://github.com/nadar/quill-delta-parser
Every file where you need classes loaded by the autoloader need to include the autoload file. Otherwise, these classes are unknown by the interpreter.
Add this to the beginning of your file:
require __DIR__ . '/vendor/autoload.php';
I have followed the instructions at getcomposer.org combined with those at developers.facebook.com but I am struggling to get the PHP SDK up and running.
The install worked fine and all files were generated including the autoload.php in the vendor folder.
I have put this line in my index.php:
require 'vendor/autoload.php';
When I call FacebookSession::setDefaultApplication('YOUR_APP_ID', 'YOUR_APP_SECRET'); I am getting a fatal error, CLASS 'FacebookSession' not found.
What do I need to do?
I guess it is something with path names and autoload.php but I can't figure it out.
Vendor folder is at root with index.php.
The use keyword does not include or autoload. It only tells PHP to import the specified namespace to the current scope which has been autoload-ed in this case with composer.
Have you tried to add use Facebook\FacebookSession; (at the beginning of your PHP file after the require vendor/autoload.php line) as xurshid29 commented?
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
I am trying to include the YouTube Analytics Service of Google but I can not access it through the Vendor folder.
include(app_path.'path/to/analytics/Google_YoutubeAnalyticsService.php')
It is not working, because it defaults to the App folder.
How can I get out of the App folder and into the Vendor folder (where the YouTube Analytics file is at)?
The error is {
include(C:\xampp\htdocs\mysite\app/path/to/analytics/Google_YoutubeAnalyticsService.php):
failed to open stream: No such file or directory
From where do you want to include that file ?
Place a reference to your file in composer.json autoload object:
"autoload": {
"files":["your_file_path"]
}
Run composer dumpautoload, and you'll have your file :)
Actually you have in the helpers function the path so basically the function base_path give the direction to the root of your project so
echo base_path() . '/vendor';
Should be the route to your vendor folder.
You can se all the documentation in
Helper Functions Laravel
Be sure that you are seeing the documentation of the laravel version that you are using (I put the link for the 4.2 version).
This question was asked a long time ago and the answers reflect that. Most the time now all you need to do is import it using the "use" statement if you installed it with composer. Composer will already reference all the important directories.
It should be something like this, but it will vary depending on the project.
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\Class;
That could include a base class as well as some exception classes.
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\ClassException;
Usually most packages if compliant with modern composer and php standards work in this fashion.