composer does not generate autoload.php - php

i install composer for windows using this link http://getcomposer.org/download/ > http://getcomposer.org/Composer-Setup.exe
my web server is WAMP php 5.4 with openssl enabled.
i created composer.json with this code
{
"require": {
"doctrine/orm": "*"
}
}
and run with this code in .php file
<?php
// bootstrap.php
// Include Composer Autoload (relative to project root).
require_once "vendor/autoload.php";
and i got error Warning: require_once(vendor/autoload.php): failed to open stream
how to get composer's autoload.php?
why composer does not generate it?
how to use doctrine without composer?

Did you actually look to see if a vendor/autoload.php was created?
Did composer throw any error messages? Unless you got an error then I'm willing to bet that a vendor/autoload files was made. Is there anything in vendor?
I'm guessing that your bootstrap.php is not in your root directory (same directory as composer.json). If so you need to adjust the path in your require statement.

Remove the autoload part from your composer.json, then run composer install
This will generate an updated autoload object, good luck!
"autoload": {
"classmap": [
"..."
]
}

Related

Composer.json to autoload classes

So I was following this tutorial on how to autoload classes, the guy teaching it already had Composer installed, he went ahead and created a composer.json file in his project directory with the following content:
{
"autoload": {
"classmap": [
"./"
]
}
}
which supposedly autoloads any classes within the main project directory. And i cannot do this because if if i create a composer.json that is located anywhere but inside located in C:/Users/Mypc, the composer.json file cannot be found, i cannot install it in command line wich is the tutorial's step, composer install creates his autoloading. My autoloader cannot seem to find any classes inside any path I create in the class map, be it absolute or relative, I also managed to install and move composer file to usr/local/bin/composer but that's all I can do.
I can't create an autoloader no matter what I do and I also can't move the composer.json to my project directory inside xampp/htdocs/myproject, because if i do so i can't composer install since composer.json can't be found. I am using xampp.
So i finaly found out what the issue was.
I simply had to use the change director with cd C:\whateverpath\totheprojectdir and then install the composer on whatever the project was on.

Installing Paypal PHP SDK using composer and PuTTY

Using PuTTY (I don't have root access to the server) I typed composer require "paypal/rest-api-sdk-php:*" as it said here: https://github.com/paypal/PayPal-PHP-SDK/wiki/Installation-Composer and it works fine, but how can I install it into directory d/pp/ instead of root? I tried d/pp/composer require "paypal/rest-api-sdk-php:*" but that's not it
I already downloaded the SDK directly but it seems not to work properly so I want to try it this way and I want to learn it anyway
Create a file named composer.json in your root directory and add the following code to it:
{
"config": {
"vendor-dir": "d/pp"
},
"require": {
"paypal/rest-api-sdk-php": "*"
}
}
now run composer install.
The config parameter lets you define configurations for your packages.
The vendor-dir directive sets the package's destination.
If you want to add more packages - in your require:{} section just add those too and run composer update.

Composer & Linux production server - autoload not working

I have already tried searching for this question and seen a couple of answers, but no luck...
I have composer installed with Slim Framework v3.
I am using autoload for my files using PSR-4 in the composer.json file like this:
"autoload": {
"psr-4": {
"App\\": "App"
}
}
And this is my folder structure:
I am running it on a localhost Mac OS X El-Capitan using Apache 2.4 and everything works like magic.
But when I upload it to my Production Linux server (also with Apache 2.4), the autoload seems to be extremely confused and I am getting errors like these:
Warning: include(/home/friendsapp/public_html/vendor/composer/../../app/Middleware/AuthMiddleware.php): failed to open stream: No such file or directory in /home/friendsapp/public_html/vendor/composer/ClassLoader.php on line 412
Warning: include(): Failed opening '/home/friendsapp/public_html/vendor/composer/../../app/Middleware/AuthMiddleware.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/friendsapp/public_html/vendor/composer/ClassLoader.php on line 412
Fatal error: Class 'App\Middleware\AuthMiddleware' not found in /home/friendsapp/public_html/public/index.php on line 5
I have namespaced my classes exactly according to my folder structure.
<?php
namespace App\Middleware;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \App\Middleware\Middleware;
use \App\Share\ErrorCode;
use \App\Models\ResultMessage;
use \App\Mappers\AccessTokenMapper;
class AuthMiddleware extends Middleware {
Any help would be most appreciated! :)
Looking at the path in the errors /app/Middleware/AuthMiddleware.php
It appears the issue is caused by a namespace conflict of App\\ being pointed to /app in your production environment as opposed to your PSR-4 declaration pointing to /App.
To avoid conflicts and map all of the namespaces of a specified directory you can use the autoload classmap or config optimize-autoloader (optional) options in composer.json in order to define the physical path of all the files and objects in the specified directories for composer to load. Additionally with the PSR-4 declaration, any files not found in the classmap paths will be attempted to be loaded from the App namespace path declaration(s). For example when using the exclude-from-classmap option.
"config": {
"optimize-autoloader": true
},
"autoload": {
"psr-4": {
"App\\": "App/"
},
"classmap": [
"App/",
],
}
After making the change in your composer.json, be sure to run php composer.phar update --lock in your development environment.
Then after uploading the composer.lock and composer.json files to your production environment, run php composer.phar install --no-dev -o or php composer.phar dump-autoload --no-dev -o from the production environment.
The -o option will force the optimize-autoloader classmapping to run and --no-dev will prevent the development packages (require-dev) from being installed. Using optimize-autoloader is recommended for production environments.
As a general practice, anytime you deploy your development changes to your production environment you need to run php composer.phar install --no-dev -o See How to deploy correctly when using Composer's develop / production switch?. This way the changes applied from your development environment using php composer.phar update are installed in your production environment correctly.
For my production server the following worked:
composer install --no-dev -o
then restart php
on serverpilot:
rm -rf vendor/*
composer5.6-sp install --no-dev -o
sudo service php5.6-fpm-sp restart

include php package in file instead of composer

I want to include a php package, a css parser: https://github.com/sabberworm/PHP-CSS-Parser In installation guide it says to "Add php-css-parser to your composer.json":
{
"require": {
"sabberworm/php-css-parser": "*"
}
}
(I don't even know what composer.json is) Since I need only in one script, is there a way to include it only in a file? Like:
<?php
include //something here
//Do stuff with css parser
?>
Thank you
Your best approach would be to familiarize yourself with composer, what it's for and how it works.
You could copy the code from the module manually and include it in your project, but why bother?
You should simply need to run composer require sabberworm/php-css-parser from your project's root directory. Composer will automatically create the composer.json file which defines your project information and its dependencies, and the composer.lock file which stores information about the version of each dependency you've installed so you can deploy specific versions to other places.
If you don't have composer installed, check out the download instructions.
Once you've installed it, you need to include the composer autoloader and you will be ready to include your custom package:
<?php
// Include composer's autoloader
include 'vendor/autoload.php';
// Now you can use any composer installed package:
$parser = new Sabberworm\CSS\Parser($sText);

Trouble installing Clickatell Php Library

I'm trying to install the clickatell php library after this instruction:
https://github.com/arcturial/clickatell
But so far i was only able to install composer.
They say: this library uses composer and can be acquired using the following in your composer.json file.
{
"require": {
"arcturial/clickatell": "*"
}
}
So I have to add this to the composer json file? Then require the json file in my php script? I've no idea what to do next. Any help would be great.
When you are in your project root, run the following:
create a composer.json file with the following contents:
{
"name": "your/project",
"description": "project description",
"require": {
"arcturial/clickatell": "*"
}
}
php -r "readfile('https://getcomposer.org/installer');" | php
This will download a file called composer.phar
php composer.phar install
This will install the dependencies you specified in your composer.json file. Once complete, your project will now have a folder called "vendor".
Include the "vendor/autoload.php" file in your script
require_once __DIR__ . '/vendor/autoload.php';
...
Now you should be able to use the library as specified in the documentation.

Categories