Laravel-如何使用供应商类? [英] Laravel - How to use a vendor class?

查看:83
本文介绍了Laravel-如何使用供应商类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对m条route.php文件使用移动检测".我已根据需要在composer.json中添加了该软件包,并将其安装在供应商文件中.我现在如何使用它?

I want to use Mobile Detect on m routes.php file. I have added the package as a require in composer.json and it's installed in the vendor file. How can I use it now?

我尝试了这个答案,但没有运气,因为找不到该类:使用供应商的Laravel 4类

I tried this answer and no luck because the class wasn't found: 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 ,但是别名从未起作用,表示找不到该类.

I tried using this one: https://github.com/jenssegers/Laravel-Agent, but the alias never worked saying the class was not found.

推荐答案

该程序包以PSR-0命名空间.查看git repo,它似乎是Detection\MobileDetect ,尽管您将要确保它确实是正确的名称空间.您是否尝试过在routes.php文件中添加适当的名称空间?

This package is PSR-0 namespaced. Looking at the git repo, it appears to be Detection\MobileDetect though you will want to make sure this is indeed the correct namespace. Have you tried adding the proper namespace to your routes.php file?

use Detection\MobileDetect as MobileDetect;

,或者您可以引用适当的内联名称空间.这是一个示例:

or you can reference the proper namespace inline. Here is an example:

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

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

If this doesn't work for you, you may be able to get away by adding it to your composer.json classmap:

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

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

Of course, fill in the proper path then run a composer dump-auto.

这篇关于Laravel-如何使用供应商类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆