ronrun
ronrun

Reputation: 1234

Laravel: How to initiate global libraries

I have
app/Libraries/Cart.php
app/Libraries/Currency.php
app/Libraries/SomeOther1.php
app/Libraries/SomeOther2.php
...

In all my controllers, I don't want to "use" and declare.
I don't want this:

<?php
namespace App\PathToFolder;

use App\Libraries\Cart;
use App\Libraries\Currency;

class SomeController extends Controller
{
  public someFunction()
  {
    $this->cart = new Cart;
    $this->cart->add(...);
    $this->cart->remove(...);
    $this->cart->clear(...);

    $this->currency = new Currency;
    $this->currency->format(...);
  }

I want this

<?php
namespace App\PathToFolder;

class SomeController extends Controller
{
  public someFunction()
  {
    $this->cart->add(...);
    $this->cart->remove(...);
    $this->cart->clear(...);
    $this->currency->format(...);
  }

There are no lines of "use" and "new". How to do? service provider? middleware?

This is also acceptable:

$this->library->cart->add(...);  
$this->library->cart->remove(...);  
$this->library->cart->clear(...);  
$this->library->currency->format(...);  

Upvotes: 0

Views: 883

Answers (3)

Nirali
Nirali

Reputation: 1786

There is one more below way to achieve this other then using traits or composer file as mentioned by others.

You can declare and initialize varible in parent controller and use it.

For Example,

Your parent controller a per your code

<?php

 namespace App\Http\Controllers;

 use Illuminate\Foundation\Bus\DispatchesJobs;
 use Illuminate\Routing\Controller as BaseController;
 use Illuminate\Foundation\Validation\ValidatesRequests;
 use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 use App\Libraries\Cart;
 use App\Libraries\Currency;

 class Controller extends BaseController
 {
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    private $cart;
    private $currency;

    public function __construct()
    {
        //blockio init
        $this->cart = new Cart;
        $this->currency = new Currency; 
    }

 }

Now you can you this function in your controller like this

<?php
namespace App\PathToFolder;

class SomeController extends Controller
{
  public someFunction()
  {
    $this->cart->add(...);
    $this->cart->remove(...);
    $this->cart->clear(...);
    $this->currency->format(...);
  }

Upvotes: 0

ZeroOne
ZeroOne

Reputation: 9117

usually i will do helper class where you don't need to load your massive init function in your controller or base controller

I created new Folder call Helpers and create new class name helpers.php

under autoload in composer.json, add your helper class in it..

"autoload": {
    ...
    "files": ["app/Helpers/helpers.php"]
},

run composer dumpautoload

and in helper.php class.. see sample below

if (! function_exists('get_cart')) {

    function get_cart()
    {
       return new Cart();
    }
}

usage:

get_cart()->add(...);
get_cart()->remove(...);

OR

IF you dont want to use helper class. Just do static class

usage:

Cart::add(...)
Cart::remove(...)

OR

use Trait but still you are using $this in $this->cart->add

Upvotes: 0

Karlo Kokkak
Karlo Kokkak

Reputation: 3714

You can try below code without use. Add the namespace in instantiation instead.

<?php
namespace App\PathToFolder;

class SomeController extends Controller
{
  public someFunction()
  {
    $this->cart = new \App\Libraries\Cart;
    $this->cart->add(...);
    $this->cart->remove(...);
    $this->cart->clear(...);

    $this->currency = new \App\Libraries\Currency;
    $this->currency->format(...);
  }

Upvotes: 1

Related Questions