How to autoload service namespace in laravel 5.6












1















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



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


appLibraryGlobalfunction.php



<?php
namespace AppLibrary;

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 AppLibraryGlobalfunction;


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 AppLibraryGlobalfunction; 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.










share|improve this question























  • you have mentioned trying composer way. after that approach composer dump-autoload is needed.

    – Kul
    Jan 1 at 13:00













  • @Kul yes i did that too but it's not working. if you sure i can do that with composer then i will provide composer code too.

    – Dipen
    Jan 1 at 13:13
















1















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



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


appLibraryGlobalfunction.php



<?php
namespace AppLibrary;

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 AppLibraryGlobalfunction;


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 AppLibraryGlobalfunction; 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.










share|improve this question























  • you have mentioned trying composer way. after that approach composer dump-autoload is needed.

    – Kul
    Jan 1 at 13:00













  • @Kul yes i did that too but it's not working. if you sure i can do that with composer then i will provide composer code too.

    – Dipen
    Jan 1 at 13:13














1












1








1


1






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



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


appLibraryGlobalfunction.php



<?php
namespace AppLibrary;

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 AppLibraryGlobalfunction;


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 AppLibraryGlobalfunction; 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.










share|improve this question














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



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


appLibraryGlobalfunction.php



<?php
namespace AppLibrary;

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 AppLibraryGlobalfunction;


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 AppLibraryGlobalfunction; 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.







laravel laravel-5 composer-php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 12:51









DipenDipen

1401416




1401416













  • you have mentioned trying composer way. after that approach composer dump-autoload is needed.

    – Kul
    Jan 1 at 13:00













  • @Kul yes i did that too but it's not working. if you sure i can do that with composer then i will provide composer code too.

    – Dipen
    Jan 1 at 13:13



















  • you have mentioned trying composer way. after that approach composer dump-autoload is needed.

    – Kul
    Jan 1 at 13:00













  • @Kul yes i did that too but it's not working. if you sure i can do that with composer then i will provide composer code too.

    – Dipen
    Jan 1 at 13:13

















you have mentioned trying composer way. after that approach composer dump-autoload is needed.

– Kul
Jan 1 at 13:00







you have mentioned trying composer way. after that approach composer dump-autoload is needed.

– Kul
Jan 1 at 13:00















@Kul yes i did that too but it's not working. if you sure i can do that with composer then i will provide composer code too.

– Dipen
Jan 1 at 13:13





@Kul yes i did that too but it's not working. if you sure i can do that with composer then i will provide composer code too.

– Dipen
Jan 1 at 13:13












1 Answer
1






active

oldest

votes


















1














Maybe you can follow the same approach as Laravel?



Let me give you an example on how to achieve this.



First, create a Helpers.php file in app/Helpers.php.
You also need to autoload it.



"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"database/providers"
],
"files": [
"app/Helpers.php"
],
"psr-4": {
"App\": "app/"
}
}


Once that is done, you could define a function as such in your newly autoloaded Helpers.php:



if(! function_exists('global_function')) {

function global_function()
{
return new AppLibraryGlobalfunction();
}
}


Then to use it anywhere, you can just do this.



global_function()->test('hello');


This is just a simple example. Obviously there are a lot of considerations you have to make before implementing this.
However, Laravel has a similar approach to providing global helper functions. For example:



use IlluminateSupportFacadesSession; 

// This
echo Session::get('key');

// is the same as

echo session()->get('key');





share|improve this answer


























  • Yes this is the one solution like use AppLibraryGlobalfunction; but i want use class object instead of function. I know this code will do same thing but my first priority is object. can i do that?

    – Dipen
    Jan 1 at 13:57













  • global_function() will return an object of AppLibraryGlobalfunction. So I am not sure what you mean?

    – Mozammil
    Jan 1 at 14:00











  • Yes you are right. I just want to know that is that possible to autoload service class without create or use helper file?

    – Dipen
    Jan 1 at 17:06











  • Not that i know of :)

    – Mozammil
    Jan 1 at 17:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995589%2fhow-to-autoload-service-namespace-in-laravel-5-6%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Maybe you can follow the same approach as Laravel?



Let me give you an example on how to achieve this.



First, create a Helpers.php file in app/Helpers.php.
You also need to autoload it.



"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"database/providers"
],
"files": [
"app/Helpers.php"
],
"psr-4": {
"App\": "app/"
}
}


Once that is done, you could define a function as such in your newly autoloaded Helpers.php:



if(! function_exists('global_function')) {

function global_function()
{
return new AppLibraryGlobalfunction();
}
}


Then to use it anywhere, you can just do this.



global_function()->test('hello');


This is just a simple example. Obviously there are a lot of considerations you have to make before implementing this.
However, Laravel has a similar approach to providing global helper functions. For example:



use IlluminateSupportFacadesSession; 

// This
echo Session::get('key');

// is the same as

echo session()->get('key');





share|improve this answer


























  • Yes this is the one solution like use AppLibraryGlobalfunction; but i want use class object instead of function. I know this code will do same thing but my first priority is object. can i do that?

    – Dipen
    Jan 1 at 13:57













  • global_function() will return an object of AppLibraryGlobalfunction. So I am not sure what you mean?

    – Mozammil
    Jan 1 at 14:00











  • Yes you are right. I just want to know that is that possible to autoload service class without create or use helper file?

    – Dipen
    Jan 1 at 17:06











  • Not that i know of :)

    – Mozammil
    Jan 1 at 17:11
















1














Maybe you can follow the same approach as Laravel?



Let me give you an example on how to achieve this.



First, create a Helpers.php file in app/Helpers.php.
You also need to autoload it.



"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"database/providers"
],
"files": [
"app/Helpers.php"
],
"psr-4": {
"App\": "app/"
}
}


Once that is done, you could define a function as such in your newly autoloaded Helpers.php:



if(! function_exists('global_function')) {

function global_function()
{
return new AppLibraryGlobalfunction();
}
}


Then to use it anywhere, you can just do this.



global_function()->test('hello');


This is just a simple example. Obviously there are a lot of considerations you have to make before implementing this.
However, Laravel has a similar approach to providing global helper functions. For example:



use IlluminateSupportFacadesSession; 

// This
echo Session::get('key');

// is the same as

echo session()->get('key');





share|improve this answer


























  • Yes this is the one solution like use AppLibraryGlobalfunction; but i want use class object instead of function. I know this code will do same thing but my first priority is object. can i do that?

    – Dipen
    Jan 1 at 13:57













  • global_function() will return an object of AppLibraryGlobalfunction. So I am not sure what you mean?

    – Mozammil
    Jan 1 at 14:00











  • Yes you are right. I just want to know that is that possible to autoload service class without create or use helper file?

    – Dipen
    Jan 1 at 17:06











  • Not that i know of :)

    – Mozammil
    Jan 1 at 17:11














1












1








1







Maybe you can follow the same approach as Laravel?



Let me give you an example on how to achieve this.



First, create a Helpers.php file in app/Helpers.php.
You also need to autoload it.



"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"database/providers"
],
"files": [
"app/Helpers.php"
],
"psr-4": {
"App\": "app/"
}
}


Once that is done, you could define a function as such in your newly autoloaded Helpers.php:



if(! function_exists('global_function')) {

function global_function()
{
return new AppLibraryGlobalfunction();
}
}


Then to use it anywhere, you can just do this.



global_function()->test('hello');


This is just a simple example. Obviously there are a lot of considerations you have to make before implementing this.
However, Laravel has a similar approach to providing global helper functions. For example:



use IlluminateSupportFacadesSession; 

// This
echo Session::get('key');

// is the same as

echo session()->get('key');





share|improve this answer















Maybe you can follow the same approach as Laravel?



Let me give you an example on how to achieve this.



First, create a Helpers.php file in app/Helpers.php.
You also need to autoload it.



"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"database/providers"
],
"files": [
"app/Helpers.php"
],
"psr-4": {
"App\": "app/"
}
}


Once that is done, you could define a function as such in your newly autoloaded Helpers.php:



if(! function_exists('global_function')) {

function global_function()
{
return new AppLibraryGlobalfunction();
}
}


Then to use it anywhere, you can just do this.



global_function()->test('hello');


This is just a simple example. Obviously there are a lot of considerations you have to make before implementing this.
However, Laravel has a similar approach to providing global helper functions. For example:



use IlluminateSupportFacadesSession; 

// This
echo Session::get('key');

// is the same as

echo session()->get('key');






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 1 at 14:03

























answered Jan 1 at 13:24









MozammilMozammil

6,230420




6,230420













  • Yes this is the one solution like use AppLibraryGlobalfunction; but i want use class object instead of function. I know this code will do same thing but my first priority is object. can i do that?

    – Dipen
    Jan 1 at 13:57













  • global_function() will return an object of AppLibraryGlobalfunction. So I am not sure what you mean?

    – Mozammil
    Jan 1 at 14:00











  • Yes you are right. I just want to know that is that possible to autoload service class without create or use helper file?

    – Dipen
    Jan 1 at 17:06











  • Not that i know of :)

    – Mozammil
    Jan 1 at 17:11



















  • Yes this is the one solution like use AppLibraryGlobalfunction; but i want use class object instead of function. I know this code will do same thing but my first priority is object. can i do that?

    – Dipen
    Jan 1 at 13:57













  • global_function() will return an object of AppLibraryGlobalfunction. So I am not sure what you mean?

    – Mozammil
    Jan 1 at 14:00











  • Yes you are right. I just want to know that is that possible to autoload service class without create or use helper file?

    – Dipen
    Jan 1 at 17:06











  • Not that i know of :)

    – Mozammil
    Jan 1 at 17:11

















Yes this is the one solution like use AppLibraryGlobalfunction; but i want use class object instead of function. I know this code will do same thing but my first priority is object. can i do that?

– Dipen
Jan 1 at 13:57







Yes this is the one solution like use AppLibraryGlobalfunction; but i want use class object instead of function. I know this code will do same thing but my first priority is object. can i do that?

– Dipen
Jan 1 at 13:57















global_function() will return an object of AppLibraryGlobalfunction. So I am not sure what you mean?

– Mozammil
Jan 1 at 14:00





global_function() will return an object of AppLibraryGlobalfunction. So I am not sure what you mean?

– Mozammil
Jan 1 at 14:00













Yes you are right. I just want to know that is that possible to autoload service class without create or use helper file?

– Dipen
Jan 1 at 17:06





Yes you are right. I just want to know that is that possible to autoload service class without create or use helper file?

– Dipen
Jan 1 at 17:06













Not that i know of :)

– Mozammil
Jan 1 at 17:11





Not that i know of :)

– Mozammil
Jan 1 at 17:11




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995589%2fhow-to-autoload-service-namespace-in-laravel-5-6%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory