Morrolan Posted January 8, 2016 Share Posted January 8, 2016 (edited) 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 January 8, 2016 by Morrolan Quote Link to comment Share on other sites More sharing options...
Morrolan Posted January 8, 2016 Author Share Posted January 8, 2016 To be clear, I want to use composer as I will be using third-party packages in my refactoring. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.