Jump to content

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:




│   composer.json



│   ├───AppName

│   │   ├───admin

│   │   │       authenticated.php

│   │   │       init.php

│   │   │       login.php

│   │   │       

│   │   └───php

│   │       └───search.php

│   └───Framework

│       ├───Helpers

│       │   └───OSHelpers.php


    │   autoload.php














    "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:




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,


Edited by Morrolan

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

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.