mardi 1 janvier 2019

How to autoload service namespace in laravel 5.6

I have created some library services in app\Library. I used AppServiceProvider to bind that service using following code:

$this->app->bind('App\Library\Globalfunction', function ($app) {
    return new Globalfunction();
});

app\Library\Globalfunction.php

<?php
namespace App\Library;

    class Globalfunction {
        protected $_ci;
        public $siteConfig=array();
        public $smtpConfig=array();
        public $socialConfig=array();

        public $paramConfig;
        public $paramFrom;

        public function test($param) {
            return $param;
        }
    }
?>

To use this test() in controller i am including namespace using following:

use App\Library\Globalfunction;

once namespace is included i use following code:

$globalFunction = new Globalfunction();
echo $globalFunction->test('hello');

All of this code working fine but i don't want to add use App\Library\Globalfunction; in each file so is there anyway i can do that? is there any autoload file where i can put this and i can access Globalfunction?

I google solution for that and i tried several solutions like add this in composer or create package etc but it's not working so please if anyone have solution for this problem please let me know.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2VjfUaN
via IFTTT

Aucun commentaire:

Enregistrer un commentaire