In my project inside the vendor directory I created a new directory named Hello and created a new class HelloWorld.php with the namespace Hello\Test. Then in the root of Hello I created a composer.json with default meta data (author, license, desc, required), and added autoload PSR-4 Hello\\Test\\.
So what do I need to do next to autoload my package. I looked at some Symfony components and their composer.json package and configuration is the same.
Is it possible to autoload my local package from vendor like this?
Dir structure:
|-my_project
|-composer.json
|---vendor
|-------composer
|-------autoload.php
|-------Hello
|-----------composer.json
|-----------HelloWorld.php
./vendor/Hello/composer.json
{
"name": "Zend/Hello",
"description": "My test package",
"license": "MIT",
"authors": [
{
"name": "Zend Zend",
"email": "test#example.com"
}
],
"autoload": {
"psr-4": {
"Hello\\Test\\": ""
}
}
}
My HelloWorld.php class has namespace Hello\Test;
Inside index.php i do include 'vendor/autoload.php
And root composer.json
{
"autoload": {
"psr-4": {
"": "src/",
"Hello\\Test\\": "./vendor/Hello"
}
},
"require": {
"Hello/Test": "*"
}
}
composer update
Okay! Sorry for the late reply.
You only need one composer.json to make this work. The one in your main project directory.
Try using this:
{
"name": "Zend/Hello",
"description": "My test package",
"license": "MIT",
"authors": [
{
"name": "Zend Zend",
"email": "test#example.com"
}
],
"autoload": {
"psr-4": {
"Hello\\Test\\": "vendor/Hello"
}
}
}
And your HelloWorld.php file should be like:
<?php
namespace Hello\Test;
class HelloWorld {
// your methods goes here
}
Run a composer self update to get the latest composer the run composer update and it should now be added to your vendor/composer/autoload_psr4.php file
Related
I'm on Drupal 9.3.x trying to add a script to run with composer, following this guide
I've tried either to add the autoload and script part to the drupal project composer.json file
{
"name": "drupal/d9-starter-kit",
"description": "Project template for D9 starter kit",
"type": "project",
"license": "GPL-2.0-or-later",
"homepage": "https://www.drupal.org/project/drupal",
"support": {
"docs": "https://www.drupal.org/docs/user_guide/en/index.html",
"chat": "https://www.drupal.org/node/314178"
},
"autoload": {
"classmap": [
"Drupal\\d9_starter_kit\\Starter.php"
]
},
"scripts": {
"doStart": [
"Drupal\\d9_starter_kit\\Starter::doStart"
]
},
// require, extra, etc...
}
or to create a composer.json inside the custom module I've made
{
"name": "drupal/d9_starter_kit",
"type": "drupal-custom-module",
"description": "a module to script the start",
"license": "proprietary",
"authors": [
{
"name": "My name",
"email": "mymail#mail.com"
}
],
"require": {},
"autoload": {
"classmap": [
"src\\Starter.php"
]
},
"scripts": {
"doStart": [
"Drupal\\d9_starter_kit\\Starter::doStart"
]
}
}
and include that to the project composer file, according the the guide on Drupal site
The path of the Start class is web/modules/custom/d9_starter_kit/src/Starter.php
<?php
namespace Drupal\d9_starter_kit;
class Starter {
public static function doStart() {
print('test');
// TODO
}
}
I've tried various other syntax for the autoload part - e.g. d9_starter_kit\\Starter.php - but none works.
I get the following error
Could not scan for classes inside "Drupal\d9_starter_kit\Starter.php" which does not appear to be a file nor a folder
Class Drupal\d9_starter_kit\Starter is not autoloadable, can not call doStart script
What is the correct way to autoload the class and run a script? The objective of the latter is the same of the guide, copy, rename and edit some files in order of automate the "start up" of a custom project template.
I have a question related to Package for Laravel 5. I am creating one package and tried to use that. package successfully created and using composer i can also get that in New Laravel setup , issue is that when i tried to use that it's says class not found. Here's my composer.json and Steps that i followed:
for e.g. my username = git_test and packagename = mypackage
My Package Structure :
**git_test > mypackage > src
My composer.json file
{
"name": "git_test/mypackage",
"description": "XXXXXXXXXX",
"keywords": ["laravel"],
"license": "MIT",
"authors": [
{
"name": "XXXXXXX",
"email": "XXXXXX#gmail.com"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.0.*"
},
"autoload": {
"psr-4": {
"git_test\\mypackage\\": "src/"
}
},
"minimum-stability": "dev"
}
Here's my src/myclass.php
namespace git_test\mypackage;
class myclass {
function test(){ echo "This is Test"; }
}
Now i am going to use this in my new laravel project so i add package in my directory composer and try to use the myclass in my HomeController
HomeController Code
use git_test\mypackage\myclass as TaskClass;
class HomeController extends Controller {
public function index()
{
$atTaskObj = new TaskClass('');
}
I got the error like "git_test\mypackage\myclass" Not Found. where i am doing wrong? any suggestion please.
Thanks in Advance!!!
PSR-4 paths have to end with \\:
"autoload": {
"psr-4": {
"git_test\\mypackage\\": "src/"
}
},
Hi i have this folder structure:
I'm using composer for autoloading my files but it is dont work .. i do it first time and i dont know how to implement this.
My composer.json
{
"name": "Some name",
"description": "Some Framework",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "Some names of authors",
"email": "some#gmail.com"
}
],
"autoload": {
"psr-4": {
"Apison": "/../sdk/"
}
}
}
And my index.php
<?php
require_once 'vendor/autoload.php';
$app = new \Apison\Sdk\App();
When i update my composer it will write: Nothing to load and PHP will catch exeption on line with $app = new \Apison\Sdk\App();
Thanks for your tips
namespaces need \\:
"Apison\\": "../sdk"
documentation:
https://getcomposer.org/doc/04-schema.md#psr-4
Based on our chat, the solution is this:
"autoload": {
"psr-4": {
"Apison\\Sdk\\": "sdk"
}
}
Then the namespaces and file structure was changed to comply with the psr-4 standard
Composer not updating autoload_namespaces.php file, despite downloading package normaly. Can't understand where i did a mistake.
If i load something from packagist, namespaces file updating successfully.
Project structure
Main Composer.json
{
"repositories":[
{
"type": "package",
"package": {
"name": "test/framework",
"version": "1.0.0.1",
"dist": {
"url": "http://localhost/repo/1.zip",
"type": "zip"
}
}
}
],
"require": {
"test/framework": "*"
}
}
Package composer.json
{
"name": "test/framework",
"type": "library",
"require": {
"php": ">=5.2.4"
},
"autoload": {
"psr-0" : {
"Test" : "lib/"
}
}
}
autoload_namespaces.php
<?php
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);
You are downloading the package defined in the repositories via zip.
https://getcomposer.org/doc/05-repositories.md#package-2
In this case I feel you should define the package definition at the same place. See the above link for the example which contains autoload definition defined.
{
"repositories":[
{
"type": "package",
"package": {
"name": "test/framework",
"version": "1.0.0.1",
"dist": {
"url": "http://localhost/repo/1.zip",
"type": "zip"
},
"autoload": {
"psr-4" : {
"Test\\": "lib"
}
}
}
}
],
"require": {
"test/framework": "*"
}
}
You can also try some variation see my post over http://harikt.com/blog/2014/05/29/hidden-gems-of-composer/
PS : psr-4 is the recommended way for it can autoload psr-0 structured classes. See https://getcomposer.org/doc/04-schema.md#autoload
Just to add to what Hari K T said, be sure to remove the vendor directory after making an update to the composer.json file as composer uses the installed.json file in the ./vendor/composer directory to generate the autoload php files.
I had setup the composer.json correctly, but did not remove the existing vendor directory, so I assumed the answer provided by Hari K T didn't work.
I'm trying to figure out how to reference a custom class using composer
my composer.json file looks like this:
{
"name": "adtools_api",
"repositories": [
{
"type": "package",
"package": {
"name": "qz/adtools_middleware",
"version": "dev-master",
"source": {
"url": "repo-name",
"type": "git",
"reference": "master"
}
}
}
],
"require": {
"slim/slim": "2.*",
"qz/adtools_middleware": "src/"
}
}
and the folder structure looks like this:
app
routes
vendor
composer
qz
adtools_middleware
src
hello-world.php
slim
composer.json
index.php
I'm trying to reference the hello-world.php file which looks like this:
<?php
namespace HelloWorld;
class SayHello
{
public static function world()
{
return 'Hello World, Composer!';
}
}
?>
In the index.php file I'm trying to reference the class like this:
$hello = new HelloWorld\SayHello();
but getting an error telling me "Fatal error: Class 'HelloWorld\SayHello' not found in..."
If anyone can point me in the right direction that would be great! Thank you!
Can you check the autoload inside the vendor folder and see if your HelloWorld namespace is loaded?
If not, you may need to add autoload attribute to your composer.json file, like this
{
"autoload": {
"psr-0": {"HelloWorld": "qz/adtools_middleware/src/"}
},
to load the HelloWorld namespace in your project