Composer Autoload.php not loading in Namespaces and Classes on live hosting - php

I have a simple PHP web app with the following structure:
/ (composer.json .htaccess ...)
/Core/ (Router.php, Controller.php ...)
/App/ (/Controllers, /Models, /Views ...)
/Public/ (index.php ...)
/Vendor/ (autoload.php /composer ...)
The codes runs without issue on my local server.
When I copy it to a live server and make the necessary changes to .htaccess in route I get the following error: Fatal error: Class 'Core\Router' not found in.... I have tried 3 different hosts but no luck.
The issue seems to be to do with the composer autoload function not loading in the namespaces and classes using psr-4. I have this set up in my composer.json file:
{
"autoload": {
"psr-4": {
"Core\\": "Core/",
"App\\": "App/"
}
}
The code for my project is on GitHub at
https://github.com/imoprojects/upbook
I am new to programming in an MVC structure and also with using composer in this way.
If anyone could assist with what maybe happening, I would really appreciate it.
Cheers,
Ian

You configure this:
"Core\\": "Core/",
... but your classes are at:
core
This will only work in case insensitive file systems.

Related

Issues doing require_once in a Laravel folder

I have a situation where I've been forced to use something that SHOULD be a composer package in my Laravel project, instead, as just part of my project files (the composer package as far as I can tell contains bugs that I cannot resolve and I'm using a folder of similar php files my colleague provided to me)
I'll try to give as much context as I can:
I've installed all the files into a folder at this directory: \app\Channels\V1\Helpers\MarketplaceWebService\. The files are similar to this project, but different
In order to get the classes in the above folder loaded, I've added a line to my composer.json file:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories",
"app/Channels/V1/Helpers/MarketplaceWebService"
]
},
That last line in the classmap array - I used that to load my classes into my project, so I can now require them via use MarketplaceWebService_Client; at the top of any of my normal laravel files
Now for a lot of cases, this works -- I can initiate classes and they pull from the correct place.
However, any time I use one of the MarketplaceWebService that have a relative require_once, it fails
I have ANOTHER composer package by the same guy, tilluels, called amazon-mws-orders, and it looks based on the error message I'm receiving that these relative require_once calls are for some strange reason being made relative to that package, rather than relative to the location of the file I'm in.
So for example, if a file has require_once ('Interface.php');, and Interface.php is in the same folder as that file, it works just fine, BUT if a file has require_once ('../Model.php'), I get this lovely message:
500 - {"message":"MarketplaceWebService_Client::main():
Failed opening required '../Model.php'
(include_path='/home/devchannelapi/laravel/vendor/tilleuls/amazon-mws-orders/src:.:/opt/alt/php72/usr/share/pear')",exception":"Symfony\\Component\\Debug\\Exception\\FatalErrorException",
"file":"/home/devchannelapi/laravel/app/Channels/V1/Helpers/MarketplaceWebService/Model/SubmitFeedResponse.php","line":22
And I set some breakpoints in Xdebug and it is, in fact, line 22 on /Model/SubmitFeedResponse.php where it says require_once('../Model.php'), and the Model.php class is indeed one directory above that file, so you'd expect the require_once statement to work. And in fact, if I make a copy of Model.php and put it into the /Model folder, and change the require statement to require_once('Model.php'), it actually DOES work! But whenever I leave the relative requires, it gives me an error message like above, as if it's looking for relative requires in /home/devchannelapi/laravel/vendor/tilleuls/amazon-mws-orders/src
Now I've already done composer dump-autoload, I don't know enough about Laravel class loading to understand why else this might be happening.
Any help would be appreciated.
I was able to replace all relative imports with __DIR__ imports
eg require_once('../Model.php); becomes require_once(__DIR__ . '/../Model.php);
Was a bit of a pain to edit every file in the folder, but Sublime Text was a great help!

Composer class not found even if it exists

I'm developing a Laravel package but I have a problem with composer autoloading.
My package has 2 folders under src folder. One of them is named Laravel and the other one is Telegram. Here is the package structure:
./packages
.../typhoon
...../src
......./Laravel
........./Providers
............LumenServiceProvider.php
............LaravelServiceProvider.php
......./Telegram
..........Api.php
.....composer.json
This package is developed under SaliBhdr/Typhoon namespace.
I have added the packages/typhoon/src directory in Laravel's composer file like so:
"autoload": {
"psr-4": {
"App\\": "app/",
"SaliBhdr\\Typhoon\\" : "packages/typhoon/src/"
}
},
And add the src/ address in package composer.json file like so:
"autoload": {
"psr-4": {
"SaliBhdr\\Typhoon\\": "src/"
}
},
Here is the strange behavior begins. When I execute the php artisan serve command Laravel throws an error that says :
Class 'Salibhdr\Typhoon\Laravel\Providers\LumenServiceProvider' not found
And if I check if the class exists with class_exists('Salibhdr\Typhoon\Laravel\Providers\LumenServiceProvider') function it returns false. But if I check if Salibhdr\Typhoon\Telegram\Api exists it returns true.
I checked the autoload_classmap file and notice that the composer includes all the classes under Telegram subfolder but not Laravel subfolder.
Why composer acts weird like this? why did it include one subfolder and not the other? It is something that I do every day and never seen anything like this.
I desperately need help. Any help would be appreciated
You are trying to initialize Salibhdr\Typhoon\Laravel\Providers\LumenServiceProvider but in your composer it's "SaliBhdr\\Typhoon\\": "src/".
Notice the capital B in your composer. PHP classes are case sensitive so you have to make sure it's either both lowercase or both uppercase.
Also make sure to run composer dumpautoload if you modify composer.json.

SLIM Error :: AuthController not found when SLIM app hosted on server

I have a REST api app using SLIM framework.
Below is my app dir structure
Below is structure in my controllers dir
Authcontroller.php is in Auth folder.
Below are namespaces given to ActionController and Authcontroller
Both controllers (Action and Auth) are defined in container as below in the mainapp
Above app is working fine on my localhost ,but when i upload same to my server, it get below error on above line 58
Please help me here , i am going mad,have changed around 3 hosting servers(SHARED-HOSTING),but still same error. PLEASE HELP!!
If your deployment server is Linux, mostly, because path is case-sensitive.
If your composer.json contains section such as below
...
"autoload": {
"psr-4": {
"App\\": "app"
},
},
...
Composer PSR-4 autoloading expect to find class
\App\Controllers\Auth\AuthController
in file
app\Controllers\Auth\AuthController.php
but it can not find it because yours is
app\controllers\Auth\AuthController.php
Since your development machine seems to use Windows which by default is case-insensitive,
app\controllers\Auth\AuthController.php
equals to
app\Controllers\Auth\AuthController.php
Which is why it works on your development machine but not on deployment server.
So the solution is to rename any directory/files to match its case and also make sure that all files is copied to deployment server.
After you change directory/filename case, run
$ composer dump-autoload
So new autoload file is generated.

Fatal error: Class 'CDbTestCase' not found

I'm new in php and I have to do the testing for an app. I'm trying to make unit testing but an error messages is displayed. I have many weeks ago and I cannot fix it, please help me!!
The message says: Fatal error: Class 'CDbTestCase' not found.
I read and follow many tutorials about this issue but it doesn't work.
I'm using Yii, Eclipse IDE and Composer.
I think the problem is in bootstrap.php but I don't use it because I'm working with composer, this is the composer.json
{
"require-dev": {
"yiisoft/yii": "1.1.*",
"phpunit/phpunit": "4.6.*",
"phpunit/phpunit-selenium": ">=1.2",
"codeception/codeception":"*",
"phpunit/dbunit": ">=1.2"
},
"autoload": {
"psr-0": {"": "vendor/autoload.php"},
"psr-4": {"": "/../framework/test/CDbTestCase.php"}
}
}
The bootstrap file is needed to load the yii framework. CDbTestCase is part of the yii framework so failing to include yii will give you this error if your tests depend on yii's unit test related classes.
Use the included bootstrap file and make sure you also include composer's autoload.php file. I normally add this to my yii config file (I believe by default, yii uses the test.php config file for custom testing related settings. You can include autoload.php inside this file)
Somewhere at the top of your yii config file
// Include composer autoload
require_once 'path/to/composer/vendor/autoload.php';
Your Composer autoloading is completely wrong.
Don't include the composer autoloader "vendor/autoload.php" in the autoloading of your own composer.json. There is only one autoloader, and it is created using your autoload data as well as any libraries.
The PSR-4 autoloading is also wrong. You have to state a directory where classes are put into files according to PSR-4. You point to one single file.
You don't use class prefixes for PSR-0 and PSR-4. This is bad for performance, because you are stating that ANY class could be in the directory. So Composer has to search for that class in this directory as well, even for classes that are guaranteed to not be there. Always use a prefix, and make it as long as possible to allow unique matches: One prefix should only ever point to exactly one directory structure.
You should explain why you don't use a bootstrap file in your tests. The minimal required bootstrap code for PHPUnit is to include the Composer autoloader. Additionally, you should add things that are required in your tests and have to be done once, like initialization of framework components -YMMV.

Laravel 4, Composer, PSR-0 Autoloading Issues

I'm experiencing a weird problem with a package I'm developing in the workbench. It involves this little peice of my Composer file:
"psr-0": {
"Vendor\\": "src/"
}
What I'm wanting to do is change the path like this:
"psr-0": {
"Vendor\\": "src/models/"
}
Laravel has problems with this. The classes get added to my application just fine, but all Laravel pathing to package resources get jacked up.
Things like this:
View::make('package::myview')
Config::get('package::myvars')
These do not work at all. I get errors like this:
No hint path defined for [packge]
But if I remove the "models/" from the PSR-0 path then it all works fine.
So basically, it looks like Laravel insists that my Composer file have only "src/" in my PSR-0 paths.
This is a bug or am I missing something?
Laravel presumes 2 levels down from the Provider file, but you can manually set the path to the src/ when you register the package:
$this->package('vendor/package', null, __DIR__.'/../../../');

Categories