How to bind Services in Laravel? - laravel

Suppose I have a service: App\Services\FooService.
To use this service I need to bind it using this statement:
$this->app->bind('FooService', \App\Services\FooService::class);
So where do I need to put the above statement? In which file?

you need to bind in AppServiceProvider
app/Providers/AppServiceProvider.php
namespace App\Providers;
use App\SocialProvider;
use App\TwitterSocialProvider;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind('FooService', \App\Services\FooService::class);
}
}

Related

Laravel: Issue with calling custom Facade

I'm using Laravel 8 and I'm creating a custom Facade, but I cannot recall it with LogActivity::log($payload) but only with LogActivityFacade::log($payload).
Cannot see where is my fault...
app\Helpers\LogActivityFacade.php
<?php
namespace App\Helpers;
use Illuminate\Support\Facades\Facade;
class LogActivityFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'logactivity';
}
}
app\Helpers\LogActivityHelper.php
<?php
namespace App\Helpers;
use App\Repositories\LogActivityRepository;
class LogActivityHelper
{
public function log($payload)
{
$repository = new LogActivityRepository();
$repository->store($payload);
}
}
app\Providers\LogActivityServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\Facades\App;
use App\Helpers\LogActivityHelper;
use Illuminate\Support\ServiceProvider;
class LogActivityServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind('logactivity', function() {
return new LogActivityHelper();
});
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
}
config/app.php
In providers array
[...]
App\Providers\LogActivityServiceProvider::class,
In alias array
'LogActivity' => App\Helpers\LogActivityFacade::class,
I tried also composer dump-autoload and php artisan config:clear, but I can access to the Facade (and it works...) only with LogActivityFacade::log() instead of LogActivity.
This is the expected behavior. Laravel doesn't create new classes for you it just proxies methods from the service class in the facade using the __call magic method. If you take a peek at, for example, the Auth or Route facade in the vendor directory you will see that they are named Auth and Route respectively not AuthFacade and RouteFacade. So just name your facade LogActivity. If you need to differentiate it from the service class you can use namespacing or just postfix the service class name with something as you have already done.
You can do this for easy access to the facades
namespace App\Facade;
use Illuminate\Support\Facades\Facade;
abstract class BaseFacade extends Facade
{
/**
* #return string
*/
public static function getFacadeAccessor()
{
return static::class ;
}
/**
* #param $class
*/
static function shouldProxyTo($class)
{
app()->singleton(self::getFacadeAccessor(),$class);
}
}
extend other facades
namespace App\Facade\Plugins;
use App\Facade\BaseFacade;
/**
* #method static convertPersianNumberToEnglish($number)
* #method static bool checkDataIsTrue(array $results = [])
* #method static string|null removeFileTypeName(string $string = null)
*/
class GlobalPluginsFacade extends BaseFacade
{
}
register in services provider
public function boot()
{
// global facades
GlobalPluginsFacade::shouldProxyTo(GlobalPluginsRepo::class);
}
And easy to use.
GlobalPluginsFacade::getFunction();

Where is a "global bootstrap place" in Laravel

I am trying to install kylekatarnls/business-day package into Laravel 6. The docs say to "First load the mixin in some global bootstrap place of your app:". Where do I put this code?
One option is to place this in the boot method of your AppServiceProvider.
<?php
namespace App\Providers;
use Cmixin\BusinessDay;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
$additional_holidays = [
// ...
];
BusinessDay::enable(\Illuminate\Support\Carbon::class, 'us-national', $additional_holidays);
}
}
Another option is to create your own Service Provider and place the code in the new service provider's boot method.

Undefined variable: event error in AppServiceProvider

I'm trying to call some function after the a laravel job gets processed and i have my code just like the example on the Docs page, but i get undefined variable event error.
<?php
namespace App\Providers;
use App\Mail\EmailProcessed;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Queue;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Support\Facades\Log;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public $event;
public function boot()
{
Queue::before(function (JobProcessing $event) {
Log::info($event->job->resolveName());
});
Queue::after(function (JobProcessed $event) {
Log::notice($event->job->resolveName());
});
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
I get this error
[2019-01-30 14:08:55] local.ERROR: Undefined variable: event
{"exception":"[object] (ErrorException(code: 0): Undefined
variable: event at /var/www/html/email-verification-
app/app/Providers/AppServiceProvider.php:27)
You should delete that bunch of code :
/**
* Bootstrap any application services.
*
* #return void
*/
public $event;

When i am registering user observer in Laravel while fetching single user also i'm getting list of all users

Getting issue with Laravel Observer
I created a user observer when i'm doing api call in order to fetch single user also i'm getting list of all user exist in table. what i'm doing wrong.
Here my ObserverProvider and UserObserver which i register in app.php
ObserversServiceProvider
namespace App\Providers;
use App\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;
class ObserversServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot() {
User::observe(UserObserver::class);
}
/**
* Register the application services.
*
* #return void
*/
public function register() {
}
}
UserObserver
namespace App\Observers;
use App\User;
class UserObserver
{
/**
* Listen to the User created event.
*
* #param User $user
* #return void
*/
public function created(User $user) {
// doing something
}
}
I'm getting list of all user when i'm doing.
$user = User::where('user_id',1)->first();

Laravel Error After configuration

after running php artisan app:name <app_name>
I encounter this App\Providers\AppServiceProvider not found error
How am i Gonna fix this? Thanks
Add a file AppServiceProvider.php in app/Providers/ with the following content:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
If this doesn't work, try installing the framework again, since something went wrong during the install. Or maybe you accidentally deleted this file.

Resources