How to use php library in laravel

To use external classes or any other PHP library into your Laravel project, you have to do the following steps:

1. Create a folder somewhere in your Laravel app that will contain the PHP files you're going to use:

For example you have a custom class, create a folder in the directory app/libraries. Inside app/libraries, paste the PHP files you'll be using (the library files you've downloaded).

2. In your composer.json file, add the folder/directory into your autoload classmap:

"autoload": {
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
            "app/libraries", <------------------ YOUR CUSTOM DIRECTORY
            "app/database/migrations",
            "app/database/seeds",
        ]
    }

3. Once you're done, just run composer dump-autoload and you should be able to call your class as follows:

Assuming your class name is SomeClass.php and it's inside the app/libraries directory and you're properly namespaced the class you've just copied over, you can now use SomeClass.php anywhere you need it.

$class = new \some_class_namespace\SomeClass();

You can also give it an alias in your config/app.php file:

/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/

'aliases' => [
    ....
    'SomeAlias' => 'app\libraries\SomeClass',
    ....
],

After then you can instantiate the class from anywhere in your application just like any other classes:

$class = new SomeAlias();

Tutorial last revisioned on August 11, 2022 with Laravel 9

Laravel is an MVC framework with its own folder structure, but sometimes we want to use something external which doesn't follow the same structure. Let's review two different scenarios - when we have external class and when it's just a .php file.

Let's say we have a simple example, a PagesController.php file like this:

namespace App\Http\Controllers;

class PagesController extends Controller
{
  /**
   * Display homepage.
   *
   * @return Response
   */
  public function getHome()
  {
    return view('pages.home');
  }

}

Pretty simple, right? Now, let's say we want to have our product prices on the homepage, but they come from some kind of external class or PHP file.

Use an external class in Controller

Let's say we have a simple class to define the prices:

class PricesClass {
  public function getPrices() {
    return ['bronze' => 50, 'silver' => 100, 'gold' => 150];
  }
}

Now, where to put this class and how to use it? A couple of steps here:

1. You can put a class itself anywhere you want within \App folder

By default, Laravel offers you some folders there like Providers, but I personally prefer to create a separate one - like App\Libraries, App\Classes or App\Services. Or you can call it your own application - App\MyApp. This is totally your choice.

So, in this example, let's save the class as App\Classes\PricesClass.php.

2. Namespace within the file

Now we have to tell Laravel what is the namespace of this new file - it's the same as the folder:

namespace App\Classes;

class PricesClass {
// ...

3. Use the class in your Controller

Let's get back to our PagesController.php - here we have to add use statement for that external class, and then we're free to use it! Like this:

namespace App\Http\Controllers;

use App\Classes\PricesClass;

class PagesController extends Controller
{
  /**
   * Display homepage.
   *
   * @return Response
   */
  public function getHome()
  {
    $pricesClass = new PricesClass();
    $prices = $pricesClass->getPrices();
    return view('pages.home', compact('prices'));
  }

}

That's it, nothing more complicated than that.

Have you tried our tool to generate Laravel adminpanel without a line of code?
Go to QuickAdminPanel.com

Use an external PHP file in Controller

Another case - it's not always an OOP file that we want to use. For some cases it's just a list of functions. Of course, we can wrap them in a class as well, but not always. So, how to use the same function, if we just have a prices.php file like this:

function getPrices() {
  return ['bronze' => 50, 'silver' => 100, 'gold' => 150];
}

And that's it - no class, no namespace, nothing.
Let's place our function as app/functions/prices.php file. Then - we have three different ways of include it:

1. Just include the class with PHP functions like include() or require() - and don't forget app_path() function:

public function getHome()
{
  include(app_path() . '\functions\prices.php');
  $prices = getPrices();
  // ...

Note that you still need a slash symbol before the folder functions.
You can read more about app_path() and other Helper functions in the official documentation.

2. In composer.json file you just add needed files in "autoload" section - in a new entry called "files":
(thanks for this suggestion to the commenters Joseph and Hisham)

    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\": "app/"
        },
        "files": [
            "app/functions/prices.php"
        ]
    },

This way you don't need to use any include() functions anywhere within your controllers - just use the functions straight away.

3. Autoload the whole folder in composer.json
(thanks to YOzaz for pointing this out in comments)

Another way is just autoload the folder with that file - so you would place any similar external "helpers" in that folder, and that would be included in the future. In this case - add this folder in array or "classmap":

    "autoload": {
        "classmap": [
            "database",
            "app/functions"
        ],
    },

Choose this option if you want those files to be included in a manner of "set it and forget it".

Notice: if you make these changes to composer.json file, don't forget to run composer dump-autoload for changes to take effect.

What is the use of library in Laravel?

This library is for Laravel 5.8 and later versions. This package allows you to manage the user's role and permissions in a database.

What is a library in PHP?

According to the PHP online documentation, the standard PHP library (SPL) is a collection of interfaces and classes that are meant to solve common problems.

How do I run a program in Laravel?

##Mac Os, Ubuntu and windows users continue here:.
Create a database locally named homestead utf8_general_ci..
Pull Laravel/php project from git provider..
Rename . ... .
Open the console and cd your project root directory..
Run composer install or php composer. ... .
Run php artisan key:generate..
Run php artisan migrate..

What is :: In Laravel?

Basically its know as Scope resolution operator (::) Simply it is token which allow access to static, constant and overridden properties of method of a class. Example- in laravel we call model like this.