php Laravel - 如何使用供应商类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26432447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - How to use a vendor class?
提问by jstudios
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?
我想在 m routes.php 文件上使用 Mobile Detect。我已将该包添加为 composer.json 中的需求,并将其安装在供应商文件中。我现在如何使用它?
I tried this answer and no luck because the class wasn't found: Laravel 4 using vendor classes
我尝试了这个答案,但没有运气,因为找不到该类: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"
}
EDIT: I tried using this one: https://github.com/jenssegers/Laravel-Agent, but the alias never worked saying the class was not found.
编辑:我尝试使用这个:https://github.com/jenssegers/Laravel-Agent,但别名从来没有工作,说找不到类。
回答by Matthew Brown
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?
这个包是PSR-0
命名空间的。查看 git repo,您似乎Detection\MobileDetect
需要确保这确实是正确的 namespace。您是否尝试过将正确的命名空间添加到您的routes.php
文件中?
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');
If this doesn't work for you, you may be able to get away by adding it to your composer.json
classmap:
如果这对你不起作用,你可以通过将它添加到你的composer.json
类图中来逃脱:
"autoload": {
"classmap": ["/vendor/serbanghita/namespaced/"],
}
Of course, fill in the proper path then run a composer dump-auto
.
当然,填写正确的路径然后运行一个composer dump-auto
.
回答by tsveti_iko
I was also struggling with the mobile detection in Laravel, but I found the solution! This is from the beginning (incl. installation):
我也在 Laravel 中为移动检测而苦苦挣扎,但我找到了解决方案!这是从头开始(包括安装):
in the terminal in your Laravel project folder:
$ composer require mobiledetect/mobiledetectlib
in a Middleware file for mobile detection:
use Mobile_Detect; ... $detect = new Mobile_Detect; if ($detect->isMobile()) var_dump('is mobile'); else var_dump('is not mobile');
在 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');
And you are ready to go ;)
你准备好了;)