I'm building a laravel package, but it won't autoload.
This is the autoload section of my package:
"autoload": {
"classmap": [
"src"
],
"psr-4": {
"Doitonlinemedia\\Translatableroutes\\": "src/"
}
}
Classes are namespaced like Doitonlinemedia\Translatableroutes
Folder structure is like:
- vendor
- - doitonlinemedia
- - - translatableroutes
- - - - composer.json
- - - - src
- - - - - class1.php
I've added the ServiceProvider in app.php like:
Doitonlinemedia\Translatableroutes\TranslatableRoutesServiceProvider::class
But it keeps saying Class 'Doitonlinemedia\Translatableroutes\TranslatableRoutesServiceProvider' not found
Run composer dump-autoload on every change.
What am I doing wrong?
EDIT
For development I added: "Doitonlinemedia\\Translatableroutes\\": "packages/doitonlinemedia/translatableroutes/src" to the main composer.json file. But when I require this composer package into a new project I'm getting the above problems.
To try out this package: https://github.com/doitonlinemedia/TranslatableRoutes and follow the instructions
PHP namespaces are case sensitive.
You have to put this into your composer.json
"psr-4": {
"Doitonlinemedia\\Translatableroutes\\": "src/"
}
instead of this
"psr-4": {
"doitonlinemedia\\translatableroutes\\": "src/"
}
For me it works also without the classmap option
run composer dump-autoload again and retry.
Edit
This works for me
{
"name": "doitonlinemedia/translatableroutes",
"description": "Create translatable resource routes",
"authors": [
{
"name": "Tim van Uum",
"email": "tim#doitonlinemedia.nl"
}
],
"minimum-stability": "stable",
"license": "MIT",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"classmap": [
"src"
],
"psr-4": {
"Doitonlinemedia\\Translatableroutes\\": "src/"
}
}
}
In addition you should add this package to your requirements "illuminate/support": "~5.2".
Note: It seems to composer do not refresh the autoload entries of composer.json files located in vendor packages if they are not updated.
After editing you have to push this into your repository. Reinstall the package in your project.
If you are still developing actively on your package I recommend to setup an environment by following this instructions: Developing your packages in Laravel 5.
It's so much easier to develop your package without reinstalling or updating the remote repository always.
Thanks to some help on laracast forum. I found out that my project was bit corrupt I guess, cause the package was already working.
https://laracasts.com/discuss/channels/laravel/trouble-autoloading-custom-package-laravel-52
Related
I'm getting the following error
"Class 'Martins\ArtisanGUI\ArtisanGUIServiceProvider' not found",
I already write it in config/app.php like Martins\ArtisanGUI\ArtisanGUIServiceProvider::class and recheck namespaces, and composer but I can't figure out what is wrong.
You're editing the composer file of your package, not your project.
As the package does not appear to be installed and managed by composer, you need to make the project's composer aware of this package.
You should be able to add a local vcs repository in your project's composer.json file:
"repositories": [
{
"type": "vcs",
"url": "packages/martins-74/artisangui"
}
],
"require": {
"martins-74/artisangui": "*"
}
This will make it so you won't have to duplicate any composer rules across both json files.
you have two composer.json files in you projects :
the root of you Laravel project
in your package root (packages/martins-74/artisangui)
in the first composer.json file (the laravel composer.json):
"psr-4": {
"App\\": "app/",
"Martines\\ArtisanGUI\\" : "packages/martins-74/artisangui/src"
}
the seconds composer.json file (your package composer.json):
"psr-4": {
"Martines\\ArtisanGUI\\": "src",
}
after that run composer dump-autoload
hope this work for you
I'm developing a custom laravel package for my needs, and discovered strange issue. My package is supposed to use custom helper function called t().
function t($string, array $options = [])
{
// Function code goes here
}
It is located in my package folder, right near the package service provider.
The service provider itself loads successfully, but helper file is not. I added following lines "autoload" section of the package's composer.json, just as I saw in other package:
"files": [
"src/helpers.php"
]
then I dumped autoload. Everything works fine but this t() function. It's not found. What am I doing wrong?
P.S.: sure, I can include it in package service provider using require_once, but what is composer for either way?)
UPDATE
package composer.json:
{
"name": "astatroth/laravel-i18n",
"require": {
"astatroth/laravel-config": "^1.0"
},
"license": "MIT",
"authors": [
{
"name": ".......",
"email": "........"
},
{
"name": ".......",
"email": "........"
}
],
"autoload": {
"psr-4": {
"Astatroth\\LaravelI18n\\": "src/"
},
"files": [
"src/helpers.php"
]
},
"minimum-stability": "dev"
}
Package file structure:
laravel-i18n
config
src
I18nServiceProvider.php
helpers.php
composer.json
My package
Packages or libraries by design does not support Composer configuration files, in other words composer.json file from your package folder is never read.
To workaround this problem, use vcs type instead of package when requiring the sources.
Related:
Why using PSR4 autoload doesn't add any classes into classmap/namespaces file?
Using autoload in package doesn't generate classmap at GitHub
Please run composer update if you use repositories symlink for adding your package to laravel project. I had a same issue, running composer dump-autload doesn't work, because your vendor folder not updated with your new file.
Hope this can solve your issue.
I have been trying to create a simple composer package. But I'm stuck with the following issue. Dont know how to resolve this.
[InvalidArgumentException]
Could not find package sarav/sample-package at any version for your minimum-stability (stable). Check the package spelling or your minimum-stability
Package Url : https://packagist.org/packages/sarav/sample-package
I ran the following composer command
composer require sarav/sample-package
My composer contents
{
"name": "sarav/sample-package",
"description": "Sample package",
"type": "Library",
"license": "MIT",
"authors": [
{
"name": "Sarav",
"email": "me#sarav.co"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": {
"Sarav": "src/"
}
}
}
Your package config looks good to me, but your Git repo doesn't have any tagged versions.
Use git tag v1.0.0 or whatever version is appropriate, then git push origin --tags to update on GitHub.
Alternatively, you can go without tagged versions by specifying the master branch when you require the package, like so:
composer require sarav/sample-package dev-master
You can require any branch in this manner, but the dev- prefix is mandatory.
I'm creating my first composer package. I'm testing it by pulling it into a vanilla Laravel project.
The issue I'm having is that when I require my composer package in the main Laravel composer.json file and then try to update the autoload.
My package's composer.json:
{
"name": "cschmitz/l5-simplefm",
"description": "A Laravel 5 wrapper for Soliant Consulting's SimpleFM package.",
"require": {
"soliantconsulting/simplefm": "3.0.*"
},
"license": "MIT",
"authors": [
{
"name": "cschmitz",
"email": "schmitz.chris#gmail.com"
}
],
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"L5SimpleFM\\": "src/"
}
}
}
My package's folder structure in the Laravel project's vendor folder:
My Laravel project's composer.json:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"cschmitz/l5-simplefm": "dev" // Requiring my package here
},
...
After this, I performed a composer dump-autoload. I don't get any errors from composer, but when I check my vendor/composer/autoload_psr4.php file, my namespace isn't listed in the array:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
...
'App\\' => array($baseDir . '/app'),
// I expected to see `'L5SimpleFM\\' => array($vendorDir . '/cschmitz/L5SimpleFM/src')` as the last key of this array, but nothing shows past the App key
);
I looked around online and on stackoverflow, but the various answers and suggestions I found didn't resolve the issue.
Can anyone see what I'm missing?
Update
Per Alexandru Guzinschi's answer, I tried telling my Laravel project that there was a local composer repository to inspect by adding the following block into my Laravel project's composer.json file:
"repositories":[
{
"type": "vcs",
"url": "../cschmitz/L5SimpleFM"
}
],
I then moved the package folder starting at the cschmitz directory out to the same level of my laravel project. The file structure looks like this now:
LaravelProjectFolder/
composer.json
cschmitz/
L5SimpleFM/
composer.json
Initially I ran into the error "No driver found to handle VCS repository vendor/cschmitz". After reading a bit I found that to be able to use this kind of local repository, the repo itself needs to be under version control (i.e. git, svn, etc). Makes sense. I hadn't put it under version control yet because this was just a test project used to try to develop the package.
I created a git repository at the root of my Laravel project and ran composer update. Now I'm getting the error:
[Composer\Repository\InvalidRepositoryException]
No valid composer.json was found in any branch or tag of ../cschmitz/L5SimpleFM, could not load a package from it.
This is confusing because I can ls ../cschmitz/L5SimpleFM/composer.json and see the file. I can also run git ls-tree -r master --name-only and see the composer.json file in the local repository version control:
Is there something that would prevent my Laravel project from seeing the local repository's composer.json file?
It is not enough to move your library inside vendor directory, because Composer doesn't scan that directory and doesn't know about your change.
You need to tell Composer about your package by publishing it to Packagist, or by loading it from local file system. In your Laravel project composer.json file, change:
"repositories": [
{
"type": "vcs",
"url": "/path/to/cschmitz"
}
],
"require": {
"cschmitz/l5-simplefm": "dev-develop"
}
Next you need to run composer update in order for your library to be installed.
Not related to your current issue, but it will became an issue soon:
After you will do this and you will continue working on your cschmitz/l5-simplefm library, you will notice that you need to commit your changes made to your library and then run composer update again in your Laravel project before you can use those changes. You have a few solutions here on how to circumvent this issue.
I'm trying to address this issue now for a long time but still could not figure out what's my mistake.
I've got two repositories I want to combine. The Application and the Framework Core.
// Application
- composer.json
- public/
- CCF/
- core/ <- here the core package should go
- vendor/ <- here it goes instead
Now basically I want to create a composer package that install's into CCF/core/ instead of CCF/vendor/clancats/core/etc..
So I created a custom installer at:
vendor/composer/installers/src/Composer/Installers/ClanCatsFrameworkInstaller.php
class ClanCatsFrameworkInstaller extends BaseInstaller
{
protected $locations = array(
'core' => 'CCF/core/',
'orbit' => 'CCF/orbit/{$name}/',
);
}
The composer.json of the core
{
"name": "clancats/core",
"description": "The ClanCatsFramework Core repository",
"type": "clancatsframework-core",
"license": "MIT",
"require": {
"php": ">=5.3",
"composer/installers": "~1.0"
}
}
composer.json of the application that should implement the core at CCF/core
{
"type": "project",
"require": {
"php": ">=5.3",
"clancats/core": "dev-master"
},
"config": {
"vendor-dir": "CCF/vendor"
},
}
But after all that composer still installs the core package at /vendor/clancats/etc..
So my question is what is my mistake that composer won't install the core package to CCF/core/?
Added composer/installers to the requires.
I created a custom installer which defines the path to the core
Set the type of the core package to my custom one.
Another question that is spinning around my head, is my pattern wrong? I mean did i misunderstand how to use composer?
Github:
framework install repo: https://github.com/ClanCats/Framework
core repo: https://github.com/ClanCats/Core
Thanks for your help guys :)
So for everyone who runs into the same problem:
My mistake was that i didn't require the composer/installers in the main repository.
The composer installers have to be required in both repositories.
So in this case:
{
"type": "project",
"require": {
"php": ">=5.3",
"clancats/core": "dev-master",
"composer/installers": "~1.0"
},
"config": {
"vendor-dir": "CCF/vendor"
},
}
solves the problem.