Symfony2 composer vendor namspace not found - php

I'm trying to use a vendor lib that I created my self. For now I am not able to put it in GIT or SVN so I am trying to get it running without.
This is my directory structure(borrowed from an answer below):
vendor/
ISTlibraries/
Saml2Handler/
src/
Saml2Handler/
In my composer.json I have added
"autoload": {
"psr-0": {
"": "src/",
"Saml2Handler": "vendor/ISTlibraries/Saml2Handler/src/"
}
},
vendor/ISTlibraries/Saml2Handler/src/ is the path to my sourcecode. The class I am trying to get is called Saml2Controller which have this namespace defined
namespace Saml2Handler;
When I try from inside my symfony2 controller to initiate the class I get an error:
FatalErrorException: Error: Class 'Saml2Handler\Saml2Controller' not found in ...
In the controller I try a simple new Saml2Controller and I have written
use Saml2Handler\Saml2Controller;
Where am I going wrong?

The error is expected. I believe that you need the following directory structure:
vendor/
ISTlibraries/
Saml2Handler/
src/
Saml2Handler/ <--- you don't have this
Saml2Controller.php
... in order to have namespace Saml2Handler within your Saml2Handler.php.

Related

Composer Autoload Classes from outside vendor

I'm struggling to have my custom classes autoloaded with composer.
my directory structure:
--muttley
--library
--MyClass.php
--public
--index.php
--vendor
--composer.json
in my composer.json:
"autoload": {
"psr-4": {
"Library\\": "library/"
}
}
MyClass.php:
namespace Library\MyClass;
class MyClass {
}
in index.php:
use Library\MyClass;
require_once dirname(__FILE__).'/../vendor/autoload.php';
the root directory is defined using DocumentRoot /www/muttley/public/. I keep getting the error:
Fatal error: Class 'Library\MyClass' not found in /var/www/muttley/public/index.php on line 58
Is there anything that I might be missing?
Simple mistake. Change:
namespace Library\MyClass;
to
namespace Library;
Make sure you have ran composer dumpautoload too!

Symfony4 Error loading classes custom folder "Expected to find class... but it was not found"

Problem
I'm trying to setup a custom directory structure
for some shared classes in my Symfony project. I
want to create a custom folder in the root of my
project and I want to use the Symfony auto-load
feature to automatically register services from
that folder.
So I added a custom services namespace to the
services.yaml file:
# src ./config/services.yaml
services:
...
TestNamespace\:
resource: '../TestNamespace/*'
...
And I added an empty class in the custom folder:
# src ./TestNamespace/TestClass.php
namespace TestNamespace;
class TestClass
{
}
When I run the app I get the following error:
(1/2) InvalidArgumentException
Expected to find class "TestNamespace\TestClass" in file
"/path/to/ClassLoadErrorDemo/demo/TestNamespace/TestClass.php"
while importing services from resource
"../TestNamespace/*", but it was not found! Check the
namespace prefix used with the resource.
(2/2) FileLoaderLoadException
Expected to find class "TestNamespace\TestClass" in file
"/path/to/ClassLoadErrorDemo/demo/TestNamespace/TestClass.php" while
importing services from resource "../TestNamespace/*", but it was not
found! Check the namespace prefix used with the resource in
/path/to/ClassLoadErrorDemo/demo/config/services.yaml (which is loaded
in resource "/path/to/ClassLoadErrorDemo/demo/config/services.yaml").
I double checked the paths, namespace and the class
name multiple times and everything seems fine and I
don't understand why I still get the error.
Controllers in the ./src folder seem to load fine.
What am I doing wrong here?
Steps to reproduce
I created a demo repo to isolate the problem.
git clone https://github.com/smoelker/SymfonyClassLoadErrorDemo.git
cd SymfonyClassLoadErrorDemo/demo
composer install
mv TestNamespace/TestClass.php_ TestNamespace/TestClass.php
php bin/console server:start
Update your composer.json autoload setup
{
[...]
"autoload": {
"psr-4": {
"TestNamespace\\": "TestNamespace/",
"": "src/"
}
},
[...]
}
After run: composer dump-autoload and try again.
composer dump-autoload --classmap-authoritative will only work if your src directory is present at the time you run the command.
This can be an issue with multi-stage Docker builds in particular, when you are normally only copying the composer.json/composer.lock into the build image.

Laravel 5 FatalErrorException with a provider

I configured my config/app.php file and I added this provider:
'providers' => [
/*
Foti Services Providers
*/
Foticos\LaravelFotiServices\LaravelFotiServicesProvider::class,
],
I have created and added namespace to my LaravelFotiServicesProvider file:
<?php
namespace Foticos\LaravelFotiServices;
use Illuminate\Support\ServiceProvider;
class LaravelFotiServicesProvider extends ServiceProvider {
...
But Laravel 5 reports me this error:
FatalErrorException in ProviderRepository.php line 146:
Class 'Foticos\LaravelFotiServices\LaravelFotiServicesProvider' not found
What´s the problem?
As you have placed your files inside the vendor folder, you should provide a directive in composer.json to let Laravel find your classes. But, be ware that placing your own files inside the vendor folder is not a good idea as it's usually used for third party libraries
You should place your source files inside the app folder, and they will be automatically PSR-4 namespaced under the namespace App because in composer.json is specified:
"psr-4": {
"App\\": "app/"
}
So, if you create a subfolder: app/Libs/Foticos/ the files inside the folder will have the App\Libs\Foticos namespace
Instead, if you want to create a package (to use the sources in more than one project, or to re-distribuite them) , check here to see how you should structure your folders for package-development
EDIT
If you absolutely want to leave your folder in vendor, try to add this to composer.json:
"psr-4": {
"App\\": "app/",
"Foticos\\": "vendor/foticos/laravel_foti_services/src/"
}
Then place the file in:
vendor/foticos/laravel_foti_services/src/LaravelFotiServices/LaravelFotiServicesProvider.php
And do: composer dump-autoload

Composer autoload won't work after deployment

I am relatively new to using composer and autoload to organize my code. I have a git repository and on my local machine, I set up composer in the root dir of my project. I specified everything in a composer.json needed to get running. Using "composer install", all libraries are automatically installed.
{
"name": "my/repo",
"description": "bla",
"version": "1.2.3",
"require":
{
"php": "5.6.*",
"geraintluff/jsv4": "1.*",
"lcobucci/jwt": "^3.0"
},
"autoload":
{
"psr-4":
{
"MyNamespace\\": "src/"
}
}
}
So - once I ran "composer install" on my local machine, everything was autoloaded in my code. Fine.
But now I need to deploy the whole thing on another linux system. So i pull from the git and run composer install. All the libraries are fetched and the autoload file shows up in vendor/
Yet, I cannot use autoload (yes, I did require_once(__DIR__ . '/../vendor/autoload.php');). Everytime I try to instantiaze a class, i get a
PHP Fatal error: Class 'X' not found in /var/www/bla/x.class.php on line 123
Using use X; does not solve the problem, nor does trying to instantiate the class with its full Namespace name (e.g. $x = new \A\B\X();)
Here is the folder structure (if this matters):
+ src/
| + X.class.php // namespace here is "MyNamespace"
| + Y.class.php // same namespace
+ test/
+ run.php // namespace is "Test"
Here is a snippet of this code (run.php):
<?php namespace Test; // different namespace than the rest of the code
// making the namespace also "\MyNamespace" wouldnt work either
require_once(__DIR__ . '/../vendor/autoload.php');
use \MyNamespace\Y; // whether this line is here or not does not change the error
session_start();
// same error as with "just" implements Y {}
class SomeClass implements \MyNamespace\Y {
// ...
}
?>
So here, the Fatal error is thrown for the line where Y is extended. No matter if I use the full namespace or not. Only thing that will help is a require_once()...
So, this forces me to go back to the cumbersome way of doing all the require/includes myself!? Is there any way to solve this?
PS: composer dumpautoload wont help
PPS: composer validate shows no errors
For PSR-4 compliance, your file structure should be:
+ src/
| + X.php
| + Y.php
Note the removal of the .class.php suffix. http://www.php-fig.org/psr/psr-4/

Making Legacy Application PSR-4 Compatible

I have a question with regards to refactoring a legacy PHP application to be compatible with PHP's PSR-4 standards. I have some classes located at app/classes/ folder which are not yet namespaced properly and I want them to be autoloaded directly when I call composer's vendor/autoload.php. I added a name space according to \<NamespaceName>(\<SubNamespaceNames>)*\<ClassName> and I have tried creating a vendor/myapp/classes/src/ directory under the vendor folder of composer, and executed a dump-autoload command but to no avail. The class doesn't get loaded up and composer can't figure out where to find it. Any pointers on how to do this?
Thanks,
Jan
Thing/s to Note:
-> I don't want to upload the source code to any repository that can be publicly searchable like Packagist as the code is for internal use only.
EDIT:
Here is my composer.json:
...
"autoload":{
"psr-4": {
"Myapp\\" : "Myapp"
}
},
...
But now the structure looks like:
->Myapp
-->Classes
--->Module1
---->submodule.php
--->Module2
---->submodule2.php
--->Module3
---->submodule3.php
--->Config
---->config.db.php
->vendor
-->autoload.php
Here are my issues/questions:
When I try to load submodule.php, which in turn would load submodule2.php and submodule3.php, it would tell me that submodule3.php is not found. The namespace of submodule3.php is Myapp\Classes\Module3 but it says its not found.
I want to include forcibly, config.db.php, on every call of autoload.php
I now figured it out. But the source files will not reside in the /vendor folder, which is okay for my case. If you want to make your files be autoloaded automatically no matter which folder, just add it to the psr-4 block in the composer.json, or create it if it's not yet there. In my composer.json, I have this block on the top level object, which adds the folder I want to get autoloaded and includes also the specific file I want to include, like a config file of some sorts:
...
"autoload":{
"files": [
"somefolder/somefile.php"
],
"psr-4": {
"MyApp\\" : "MyApp"
}
},
...
Which simply means that composer should autoload files residing in the MyApp directory which will also have a namespace of MyApp.
So my folder structure looks like this now:
->MyApp
-->Classes
--->MyClass1.php --> namespace MyApp\Classes classname: MyClass1
-->Components
--->MyClass2.php --> namespace MyApp\Classes classname: MyClass2
->somefolder
-->somefile.php
->vendor
-->autoload.php
->index.php
So if I want to include the file MyClass1.php in index.php, I will just add it like:
include_once( __DIR__ . "/vendor/autoload.php");
use \MyApp\Classes\MyClass1;
$foo = new MyClass1();
$foo->bar();
Hope it helps.
You could approach this by creating your own composer package, adding it to your private git repository, and adding the package to your project's composer.json file:
"require": {
"{your-vendor}/{package-name}": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:{your-vendor}/{package-name}.git"
}
],
Once this is done, run composer update.

Categories