Laravel - How to use a provider class?
I want to use Mobile Detect feature in m routes.php file. I added the package as a requirement in composer.json and it was installed in the vendor file. How can I use it now?
I tried this answer and no luck because the class was not found: Laravel 4 using provider 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 because the class was not found.
source to share
This package is PSR-0
namespaced. Looking at the git repository it looks like Detection\MobileDetect
, although you want to make sure it is indeed the correct namespace . Have you tried adding the correct namespace to your routes.php
file?
use Detection\MobileDetect as MobileDetect;
or you can reference your own inline namespace. Here's an example:
$detect = new Detection\MobileDetect\Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
If that doesn't work for you, you can get away by adding it to the composer.json
classmap:
"autoload": {
"classmap": ["/vendor/serbanghita/namespaced/"],
}
Complete the correct path, of course, then run composer dump-auto
.
source to share
I also struggled with mobile detection in Laravel, but I found a solution! This is from the start (including installation):
-
in terminal in your Laravel project folder:
$ composer require mobiledetect/mobiledetectlib
-
in the mobile detection middleware file:
use Mobile_Detect; ... $detect = new Mobile_Detect; if ($detect->isMobile()) var_dump('is mobile'); else var_dump('is not mobile');
And you're ready to go;)
source to share