I hate to add more to the noise of "autoload isn't working!!!!!", but I can't seem to get this problem figured out, and I figured getting some fresh eyes on it would get the problem in a lot less time. Here is my index.php file:
<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';
require_once 'model/PageNav.php';
use ShinePHP\{Crud, CrudException, EasyHttp, EasyHttpException, HandleData, HandleDataException};
// ALWAYS serve over encrypted channels
try {
EasyHttp::checkHttps();
} catch (EasyHttpException $ex) {
echo $ex;
}
try {
// check if it's a GET request, if it is, serve page, if not, do nothing
if (EasyHttp::isRequestMethod('GET')) {
$Page = new PageNav('Home', 'view/home.php');
$Page->buildPage();
exit;
}
}
catch (EasyHttpException $ex) {
echo $ex;
}
So obviously I am using a package from composer called ShinePHP (it's one I've made, and I'm still working on the documentation, so I'm just using it for my own projects at the moment, composer just makes package management so easy!)
ANYWAYS...because I'm writing this question, I'm obviously getting the following error:
Fatal error: Uncaught Error: Class 'ShinePHP\EasyHttp' not found in /Applications/XAMPP/xamppfiles/htdocs/KRTY/src/index.php:11 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/KRTY/src/index.php on line 11
Now, I haven't manually touched the composer.json file, so here it is:
{
"require": {
"adammcgurk/shine-php": "~0.0.1"
},
"autoload": {
"psr-4": {
"ShinePHP\\": "src/"
}
}
}
I'm not getting any errors on requiring the vendor/autoload.php file (and I've tried changing the path to something that doesn't exist like vendor/alkdjfladksf/autoload.php and it throws an error how it should), I'm running PHP version 7.2.7 on XAMPP on Mac OS Mojave. Here is the directory structure, the highlighted index.php file is the one with the code above:
And here is the output of composer dump-autoload -o:
Generating optimized autoload files
And so...to add more to the fire of this question on Stack...How can I get composer to autoload my ShinePHP namespace with the classes as shown in the code?
This dependency does not have any autoloading rules, so Composer does not know where to find ShinePHP\EasyHttp class. You need to add autoloading configuration in composer.json of shine-php package:
"autoload": {
"psr-4": {
"ShinePHP\\": "src/"
}
},
Related
I have a dedicated server with WHM and cPanel. It already has composer installed on it.
I'm trying to run the following php lines in a webpage:
require __DIR__ . '/../vendor/autoload.php';
// Configure API key authorization: JWT
$config = \Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Authorization', '[my api key]');
$apiInstance = new \Swagger\Client\Api\MessagesApi(
new \GuzzleHttp\Client(),
$config
);
I've created a composer.json in the public_html folder and put the following in it:
{
"autoload": {
"psr-4": { "Swagger\\Client\\" : "lib/" }
}
}
And then I ran composer update in the terminal which seemed to install the dependencies and all the relevant files.
It's seeing the autoload.php file but I'm still getting a class not found error:
Fatal error: Uncaught Error: Class 'Swagger\Client\Configuration' not found in /home/mywebsite/public_html/converter/sms.php:9 Stack trace: #0 {main} thrown in /home/mywebsite/public_html/converter/sms.php on line 9
I've been at this for 4 hours now. What am I doing wrong? I can't find anything online that will guide me in the right direction.
In Composer Basic Usage documentation about autoloading it says
For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can include this file and start using the classes that those libraries provide without any extra work
So, as #NicoHaase said in the comments, if you installed Swagger with Composer (adding a require key and running composer update, for example) you don't need to specify the autoload path to Swagger in composer.json.
I am trying to understand how psr-4 works using composer. These are the contents of my composer.json file
{
"autoload": {
"psr-4": {
"Vehicle\\Car\\":"src/"
}
}
}
The Folder Structure is given below (please note I am using windows 10, so directory name casing should not matter I think)
The folder where I have created 'vendor' folder is inside D:\temp\composer_test
D:\
temp\
composer_test\
test.php
composer.json
composer.lock
vendor\
vehicle\
car\
src\
Tire.php
Contents of test.php
require __DIR__ . '/vendor/autoload.php';
$tire = new Vehicle\Car\Tire();
Contents of Tire.php
<?php
namespace Vehicle\Car;
class Tire {
public function __construct()
{
echo "initialize Tire\n";
}
}
But when I run the code (snapshot below) . I see error
D:\temp\composer_test>php test.php
PHP Fatal error: Uncaught Error: Class 'Vehicle\Car\Tire' not found in D:\temp\composer_test\test.php:3
Stack trace:
#0 {main}
thrown in D:\temp\composer_test\test.php on line 3
I Don't know what is wrong I am doing. Problem is when I install any other standard package I can use it successfully which seems like using the same format
Your composer.json should be:
{
"autoload": {
"psr-4": {
"Vehicle\\":"src/vehicle/"
}
}
}
Your directory structure should be:
D:\
temp\
composer_test\
test.php
composer.json
composer.lock
src\
vehicle\
car\
Tire.php
Make sure to run composer dump-autoload to re-general the autoload files.
Also your namespace does not make much sense, naming your namespace Vehicle would mean that you only have vehicles in your repo. Usually you would name your namespace based on your project name, could be your brand or your username, or something more generic such as App.
So this would make more sense:
{
"autoload": {
"psr-4": {
"App\\":"src/"
}
}
}
then you have:
D:\
temp\
composer_test\
test.php
composer.json
composer.lock
src\
vehicle\
car\
Tire.php
plane\
...
and test.php
require __DIR__ . '/vendor/autoload.php';
$tire = new App\Vehicle\Car\Tire();
I did my best to find a question/answer that applied, but I don't think I understand enough about the autoloader to recognize a suitable answer.
I have a package with the following composer.json:
{
"name": "Pva_agent",
"type":"library",
"description" : "query the pva agent",
"version":"0.1b",
"authors" : [
{
"name":"Ed Greenberg",
"email":"ed#precisionpros.com"
}
],
"minimum-stability":"dev",
"require": {},
"autoload": {
"psr-0": {
"Pva_agent": "."
}
}
}
My directory structure after composer installation of the package:
.
./vendor
./vendor/autoload.php
./vendor/Pva_agent
./vendor/Pva_agent/Agent.php
./vendor/Pva_agent/composer.json
./vendor/Pva_agent/.gitignore
./vendor/composer
./vendor/composer/autoload_psr4.php
./vendor/composer/autoload_real.php
./vendor/composer/autoload_classmap.php
./vendor/composer/autoload_namespaces.php
./vendor/composer/installed.json
./vendor/composer/autoload_static.php
./vendor/composer/ClassLoader.php
./vendor/composer/LICENSE
./composer.lock
./composer.json
./test_pva_agent.php
My test program:
<?php
require_once('vendor/autoload.php');
use Pva_agent\Agent;
$agent = new Agent();
My result:
edg#arthur pva_project $ php test_pva_agent.php
PHP Fatal error: Class 'Pva_agent\Agent' not found in /home/edg/PhpstormProjects/pva_project/test_pva_agent.php on line 6
PHP Stack trace:
PHP 1. {main}() /home/edg/PhpstormProjects/pva_project/test_pva_agent.php:0
edg#arthur pva_project $
I didn't think I needed the 'use' statement, since the autoloader should find the class, right?
Can somebody tell me where the problem lies?
Thanks,
Ed Greenberg
Your Pva_agent library should not sit in the vendor/ directory. This directory should contain only auto-generated data from Composer. This directory is usually not stored in VCS.
You should consider refactoring your directory structure to something similar to this one:
.
|____composer.json
|____composer.lock
|____src
| |____Pva_agent
|____vendor
Your library functionality should be added to src/Pva_agent directory.
Consider to use PSR-4 instead of PSR-0 for autoload functionality, as there is no need to regenerate the autoloader when you add classes. dump-autoloader has to be run in case of PSR-0 after adding classed.
For the directory structure above and the PSR-4 autoloader your composer.json autoload section should look similar to this one:
"autoload": {
"psr-4": { "Pva_agent\\": "src/Pva_agent" }
}
Your library should be auto loaded after this. Your auto-loaded library will be registered under the Pva_agent namespace.
I am making a php application using propel ORM. It gives me the following message when I try to run it:
Fatal error: Uncaught Error: Class 'Propel\Runtime\Propel' not found in C:\MAMP\htdocs\Conference\vendor\bin\generated-conf\config.php:2 Stack trace: #0 C:\MAMP\htdocs\Conference\vendor\bin\list.php(6): require_once() #1 {main} thrown in C:\MAMP\htdocs\Conference\vendor\bin\generated-conf\config.php on line 2.
In my config.php generated file I have this written:
'classname' => '\\Propel\\Runtime\\Connection\\ConnectionWrapper'
What does it all mean? Am I missing some file or what?
I think you are missing a step in the building.
I assume you have your schema.xml file complete and you also have a propel.yaml (or with allowed extension file) properly configured. Also I assume you got Propel with Composer.
If you have all that the next steps are:
1) Open a terminal and go to your project directory, where the schema.xml and propel.yaml files are.
2) Execute the following command to get yout generated-sql (I have to do it this way on Windows):
c:\MAMP\htdocs\Conference\vendor\bin\propel sql:build
3) Get your model classes with the following command:
c:\MAMP\htdocs\Conference\vendor\bin\propel model:build
4) After generating the classes, you have to autoload them. Open your composer.json file with your text editor and add the following:
"autoload": {
"classmap": ["generated-classes/"]
}
It should look like this, for example:
{
"require": {
"twig/twig": "~1.0",
"propel/propel": "~2.0#dev"
},
"autoload": {
"classmap": ["generated-classes/"]
}
}
5) To finish the classes autoloading, you need to execute on your console:
composer dump-autoload
6) And for the runtime connection settings run this for comunicate classes at runtime:
c:\MAMP\htdocs\Conference\vendor\bin\propel config:convert
7) Assuming you have created your database, the last thing you need to do is create the tables, this is with the following command:
c:\MAMP\htdocs\Conference\vendor\bin\propel sql:insert
And there you go! That works for me every time I build a project.
I have just started a new PHP project and right up front, I am running into issues with the autoloader. I searched for the error and consulted the documentation (http://www.php-fig.org/psr/psr-4/), but the issue persists.
Hence, I created a minimal example to narrow down the cause of the error - yet, even with this minimal example, it wont work :(
My folder structure is like this:
+ src/
| + Xyz.php
+ composer.json
+ test.php
Here is my code
composer.json:
{
"name": "sg/ABC",
"description": "abc",
"autoload": {
"psr-4": {
"sg\\ABC\\": "src/"
}
}
}
Xyz.php:
<?php namespace sg\ABC;
class Xyz
{}
?>
test.php:
<?php namespace sg\ABC;
use sg\ABC\Xyz;
$a = new Xyz();
?>
Even though running composer install shows no errors, I immediately get this error when running the code:
$ php test.php
PHP Fatal error: Class 'sg\ABC\Xyz' not found in /dir/x/test.php on line 5
Fatal error: Class 'sg\ABC\Xyz' not found in /dir/x/test.php on line 5
also, running composer dump-autoload (as suggested here and there in this board) does not help
You still need to include the composer autoload.php file to include the loaded libraries.
You need to require the autoloader .. it is usually require_once "path/to/vendor/autoload.php".