Search code examples
phpcomposer-phpautoloaderpsr-4

How to autoload classes using PSR-4?


I am trying to set up a device detection system in my php script using device-detector. I am following its documentation to set it up but getting errors while doing that. I have downloaded the required files using composer. I even cloned the repo to download the required files and kept it in a separate folder out of the vendor.

In the documentation it asked to Just add piwik/device-detector to your projects requirements which I did but getting errors on running it.

Notice: Undefined variable: userAgent in C:\wamp\www\trackme\track.php on line 11

NOTE: I am relatively new to autoloading.

COMPOSER.JSON

{
    "name": "piwik/device-detector",
    "type": "library",
    "description": "The Universal Device Detection library, that parses User Agents and detects devices (desktop, tablet, mobile, tv, cars, console, etc.), clients (browsers, media players, mobile apps, feed readers, libraries, etc), operating systems, devices, brands and models.",
    "keywords": ["useragent","parser","devicedetection"],
    "homepage": "http://piwik.org",
    "license": "LGPL-3.0+",
    "authors": [
        {
            "name": "The Piwik Team",
            "email": "hello@piwik.org",
            "homepage": "http://piwik.org/the-piwik-team/"
        }
    ],
    "support": {
        "forum": "http://forum.piwik.org/",
        "issues": "https://github.com/piwik/device-detector/issues",
        "wiki": "http://dev.piwik.org/",
        "source": "https://github.com/piwik/piwik"
    },
    "autoload": {
        "psr-4": { "DeviceDetector\\": "piwik/device-detector" }
    },
    "require": {
        "php": ">=5.3.2",
        "mustangostang/spyc": "*"
    },
    "require-dev": {
        "phpunit/phpunit": "4.1.*",
        "fabpot/php-cs-fixer": "~1.7"
    },
    "suggest": {
        "doctrine/cache": "Can directly be used for caching purpose"
    }
}

track.php [Updated]

<?php

//date_default_timezone_set('Asia/Kolkata');

require_once 'vendor/autoload.php';

use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\DeviceParserAbstract;

DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE);

$dd = new DeviceDetector($userAgent);

$dd->discardBotInformation();

$dd->skipBotDetection();

$dd->parse();

if ($dd->isBot()) {
  // handle bots,spiders,crawlers,...
  $botInfo = $dd->getBot();
} else {
  $clientInfo = $dd->getClient(); // holds information about browser, feed reader, media player, ...
  $osInfo = $dd->getOs();
  $device = $dd->getDevice();
  $brand = $dd->getBrand();
  $model = $dd->getModel();
}

echo $osInfo;
}

Solution

  • The setup is right and your class files are correctly located. You can see that your code is processed until line 14 - that means that classes on lines 10 and 12 are found.

    What is missing thought is the doctrine/cache package which hasn't been installed. You did put it into "suggest" section which is not installed and therefore unknown to Composer autoloader. See details here in the docs: https://getcomposer.org/doc/04-schema.md#suggest

    You need to move it to "require" section, that's it.

    I'd suggest to define that $userAgent prior using it, too :)

    Update 1 by comments:

    In order to use piwik only, move it to "require" section. First, remove the "autoload" section completely. That is this part:

    "autoload": {
        "psr-4": { "DeviceDetector\\": "piwik/device-detector" }
    },
    

    Then call

    composer require piwik/device-detector
    

    That will add it to "require" section.