简体   繁体   English

如何使用PSR-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 . 我正在尝试使用device-detector在我的php脚本中设置设备检测系统。 I am following its documentation to set it up but getting errors while doing that. 我正在按照其文档进行设置,但这样做时会出错。 I have downloaded the required files using composer. 我已经使用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. 在文档中,它要求仅将piwik / device-detector添加到我的项目需求中 ,但是我在运行它时遇到了错误。

Notice: Undefined variable: userAgent in C:\\wamp\\www\\trackme\\track.php on line 11 注意:未定义的变量:第11行的C:\\ wamp \\ www \\ trackme \\ track.php中的userAgent

NOTE: I am relatively new to autoloading. 注意:我是自动加载的新手。

COMPOSER.JSON 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] track.php [已更新]

<?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;
}

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. 您可以看到代码一直处理到第14行-这意味着在第10和12行上找到了类。

What is missing thought is the doctrine/cache package which hasn't been installed. 缺少的想法是尚未安装的doctrine/cache程序包。 You did put it into "suggest" section which is not installed and therefore unknown to Composer autoloader. 您确实将其放入“未建议”部分,该部分未安装,因此Composer自动加载器未知。 See details here in the docs: https://getcomposer.org/doc/04-schema.md#suggest 在文档中查看详细信息: https : //getcomposer.org/doc/04-schema.md#suggest

You need to move it to "require" section, that's it. 您需要将其移动到“ require”部分,仅此而已。

I'd suggest to define that $userAgent prior using it, too :) 我建议也使用$userAgent来定义它:)

Update 1 by comments: 通过评论更新1:

In order to use piwik only, move it to "require" section. 为了仅使用 piwik,请将其移动到“ require”部分。 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. 这会将其添加到“需要”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM