Jump to content
Morrolan

Basic composer PSR4 autoloading issue

Recommended Posts

Hi all,

 

I am trying to implement PSR-4 autoloading in my project.  It is an existing application I have inherited that doesn't use OOP, but I am hoping to refactor over time to use OOP, and PSR4 is crucial for me as it is a big task.

 

At this present time we do not rely on any external libraries (old application entirely written in-house), so composer is ONLY being used for autoloading.

 

First of all, I assume that the vendor dir needs to be present on the server/git repo for autoloading, as /vendor/autoload.php is required, even though the consensus is to add the vendor directory to .gitignore?

 

My app structure on my server looks like this:

 

 

/var/www/appurl/

│   composer.json

│  

├───htdocs

│   ├───AppName

│   │   ├───admin

│   │   │       authenticated.php

│   │   │       init.php

│   │   │       login.php

│   │   │       

│   │   └───php

│   │       └───search.php

│   └───Framework

│       ├───Helpers

│       │   └───OSHelpers.php

└───vendor

    │   autoload.php

    │  

    └───composer

            autoload_classmap.php

            autoload_namespaces.php

            autoload_psr4.php

            autoload_real.php

            ClassLoader.php

         LICENSE

 

 

composer.json:

 

{

    "autoload": {
        "psr-4": {
           "Framework\\": "htdocs/Framework"
        }
    }
}

 

 

login.php calls authenticated.php when a user successfully logs in, and authenticated calls my init.php script which contains:

<?php

require_once('../../../vendor/autoload.php');

?>

it is search.php that has the error.  The user is redirected to search.php when they are authenticated.

 

search.php has the lines:

use Framework\Helpers\OSHelpers;
$current_os = OSHelpers::GetConciseOSFromUserAgent($user_agent);

the static call to OSHelpers works fine if require_once is used.

 

OSHelpers.php is namedspaced as:  <?php namespace Framework\Helpers;

 

The error I receive is:

 

 

Fatal error: Class 'Framework\Helpers\OSHelpers' not found in /var/www/appurl/htdocs/AppName/php/search.php on line 829

 

 

I'm at a bit of a loss about why it does this?  When I follow the example in this video (https://www.youtube.com slash watch?v=VGSerlMoIrY) my example app works fine, but trying to replicate this in a slightly more complicated structure is giving me problems.

 

Many thanks in advance,

Morrolan

Edited by Morrolan

Share this post


Link to post
Share on other sites

To be clear, I want to use composer as I will be using third-party packages in my refactoring.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.