Composer for autoloading php - php

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

Related

How to autoload a class inside the composer file and run a script?

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.

Composer class not Found Error

I've been working on a project and for which I needed to make a package. But it always returns
PHP Fatal error: Uncaught Error: Class 'youtubetomp3\downloader' not found
Here the structure of directory.
youtubetomp3/
src/
downloader.php
test/
downloaderTest.php
composer.json
composer.lock
and other files
The composer.json contains following details.
{
"name": "princeyadav05/youtubetomp3",
"description": "Downloads mp3 of a video given video-id",
"keywords": ["youtube", "songs", "downloader", "package"],
"license": "",
"authors": [
{
"name": "Prince Yadav",
"email": "princeyadav96#gmail.com"
}
],
"type": "package",
"require": {
"php": ">=5.4",
"php-ffmpeg/php-ffmpeg": "^0.11.0"
},
"require-dev": {
"phpunit/phpunit": "5.2.*"
},
"autoload": {
"psr-4": {
"princeyadav05\\youtubetomp3\\": "src/"
}
}
}
And this is how I'm creating object of class.
<?php
require 'vendor/autoload.php';
require 'scrapper.php';
include 'database.php';
echo "** WELCOME TO THE MP3 DOWNLOADER ** \n \n";
$name = readline("Hey There. Lets start with your name : ");
echo "\nHello " . $name . ".\n";
$search = readline("Please enter the search query : ");
$data_array = searchVideo($search); //returns data array
displayVideos($data_array); // returns video id
$download = new downloader();
$video_details = $download->downloadSong($video_id);
// returns array with video title, Duration, url, path
savingToDb($video_title, $video_duration, $video_url, $songs_path);
?>
Please help. I've tried many things but nothing worked.I'm really frustrated.
It's not clear whether you're including autoload.php file generated by composer. Would you mind posting the full code?
Then I'm not sure about your autoload directive, I would try something like this:
"autoload": {
"classmap": [
"src/"
]
},
see composer docs for more info

Composer create local package

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

How to use my Package in Laravel5

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/"
}
},

Referencing a custom composer package in Slim PHP

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

Categories