【问题标题】:Laravel - How to use a vendor class?Laravel - 如何使用供应商类?
【发布时间】:2014-12-13 11:24:48
【问题描述】:

我想在 m routes.php 文件上使用 Mobile Detect。我已在 composer.json 中将包添加为要求,并将其安装在供应商文件中。我现在如何使用它?

我尝试了这个答案但没有运气,因为找不到课程:Laravel 4 using vendor classes

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "laravel/framework": "4.2.*",
        "mobiledetect/mobiledetectlib": "*"
    },
    "autoload": {
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    },
    "minimum-stability": "stable"
}

编辑:我尝试使用这个:https://github.com/jenssegers/Laravel-Agent,但别名从来没有工作,说找不到类。

【问题讨论】:

  • 你能尝试运行php artisan dump-autoloadcomposer dump-autoloadphp composer.phar dump-autoload吗?
  • 我这样做了。现在呢?
  • 有机会修复它,我假设您仍然找不到类?确保您没有命名空间问题(请参阅@MatthewBrown)。
  • Sam,我认为他的问题更多是关于命名空间以及从基本的角度来看如何/使用什么来称呼它。这也是我的问题。我正在寻找@Matthews 的答案。

标签: php class laravel laravel-4 vendor


【解决方案1】:

这个包是PSR-0命名空间。查看 git 存储库,它似乎是 Detection\MobileDetect尽管您需要确保这确实是正确的命名空间。您是否尝试将正确的命名空间添加到您的 routes.php 文件中?

use Detection\MobileDetect as MobileDetect;

或者您可以引用正确的内联命名空间。这是一个例子:

$detect = new Detection\MobileDetect\Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');

如果这对您不起作用,您可以通过将其添加到您的 composer.json 类映射中来避免:

"autoload": {
    "classmap": ["/vendor/serbanghita/namespaced/"],
}

当然,填写正确的路径然后运行composer dump-auto

【讨论】:

  • 之后如何使用它?我在下面尝试了$detect = new MobileDetect();,得到了Class 'Detection\MobileDetect' not found
  • 试试答案的classmap部分,确保填写Mobile_Detect.php文件夹的正确路径
  • 我将它添加到类映射中:"/vendor/mobiledetect/mobiledetectlib/namespaced/Detection/MobileDetect.php",MobileDetect.php 的命名空间为 Detection。尝试use Detection as MobileDetect; 仍然出现类错误。
  • 如果你将它添加到你的类映射中,并运行composer dump-auto,你应该能够直接引用该类,而不需要use namespace。当您尝试 $detect = new Mobile_Detect; 时会发生什么
  • 我正在做$detect = new Detection; 并且找不到相同的类错误。 MobileDetect.php 有namespace Detection
【解决方案2】:

我也在为 Laravel 中的移动检测而苦苦挣扎,但我找到了解决方案!这是从头开始的(包括安装):

  • 在你的 Laravel 项目文件夹的终端中:

    $ composer require mobiledetect/mobiledetectlib
    
  • 在用于移动检测的中间件文件中:

    use Mobile_Detect;
    
    ...
    
    $detect = new Mobile_Detect;
    
    if ($detect->isMobile()) var_dump('is mobile');
    else var_dump('is not mobile');
    

你准备好了;)

【讨论】:

    猜你喜欢
    • 2013-03-02
    • 2018-03-29
    • 2019-02-18
    • 2018-06-04
    • 2016-10-28
    • 2020-07-05
    • 1970-01-01
    • 2017-05-05
    • 2018-06-09
    相关资源
    最近更新 更多