R
R
Ruslan Yanborisov2019-02-07 12:23:30
Composer
Ruslan Yanborisov, 2019-02-07 12:23:30

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',
    //Ниже ещё огромный список, который я решил не вставлять 
)

Here I did everything right.
After, in the /config/app.php file, I did the following:
'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,
  // Опять же, ниже список продолжается, вставлять его не стал 
        ]

Here, too, I seem to have done well.
And already in the controller I tried to connect it and use it:
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');
      }
    }
    }
}

The error is clear, but I do not understand where exactly I made a mistake, or I missed something.
As debag shows, the error is in the file: /vendor/laravel/framework/src/Illuminate/Foundation/AliasLoader.php.
5c5bf78f3edf1155279889.png
I just wanted to check for myself what was wrong there, but even thought that I was going down the most stupid path and, perhaps, I could do all this through the terminal or something like that, and decided to ask if it was worth continuing this barbarity or is there anyway some Russian-language documentation on this topic.
The file itself was stitched from the off site: http://mobiledetect.net .
Thank you for your attention

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2019-02-07
@Sanasol

https://getcomposer.org/
composer require mobiledetect/mobiledetectlib

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question