Answer the question
In order to leave comments, you need to log in
How to add custom library to laravel?
I want to make a check in the controller, from which device the user entered the page, and use it to find out which view to issue to the user. In simple terms - I want to make a mobile version of my personal account.
I found an excellent Mobile Detect PHP class for this, but, surprisingly, I did not find information on how to connect it to laravel.
I decided to do something very rude and possibly wrong:
I stupidly uploaded the file to /vendor/user_bib/Mobile_Detect.php.
After that, in the /vendor/composer/autoload_classmap.php file, I connected the class I needed:
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'User\\Mobile_Detect' => $vendorDir . '/user_bib/Mobile_Detect.php', //Вот мой класс
'App\\Console\\Kernel' => $baseDir . '/app/Console/Kernel.php',
'App\\Exceptions\\Handler' => $baseDir . '/app/Exceptions/Handler.php',
'App\\Http\\Controllers\\AdminController' => $baseDir . '/app/Http/Controllers/AdminController.php',
'App\\Http\\Controllers\\AjaxController' => $baseDir . '/app/Http/Controllers/AjaxController.php',
//Ниже ещё огромный список, который я решил не вставлять
)
'aliases' => [
'Mobile_Detect' => User\Mobile_Detect::class, //Вот мой класс
'App' => Illuminate\Support\Facades\App::class,
'Artisan' => Illuminate\Support\Facades\Artisan::class,
'Auth' => Illuminate\Support\Facades\Auth::class,
// Опять же, ниже список продолжается, вставлять его не стал
]
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Mobile_Detect;
use Illuminate\Support\Facades\Auth;
class HomeController extends Controller
{
public function index()
{
if(Auth::user()->moderated == 0){
header('Location: https://domain.ru/home/edit');
}else{
$detect = new Mobile_Detect; //Эта строка должна быть по инструкции, но она вызывает ошибку: Class 'User\Mobile_Detect' not found
if ( $detect->isMobile() ) {
return view('lk.home_mobail');
}else{
return view('lk.home');
}
}
}
}
Answer the question
In order to leave comments, you need to log in
https://getcomposer.org/composer require mobiledetect/mobiledetectlib
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question