I've bound an interface to its implementation, like this:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\Mail\Contracts\Webhook;
use App\Services\Mail\Clients\MailgunWebhook;
class MailServiceProvider extends ServiceProvider {
protected $defer = true;
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot() {
//
}
/**
* Register the application services.
*
* #return void
*/
public function register() {
$this->app->bind(
Webhook::class,
MailgunWebhook::class
);
}
}
I ran all of these:
php artisan config:clear
php artisan clear-compiled
php artisan optimize
composer dumpautoload
Yet, I still got the "Target is not instantiable error", when trying to use the binding.
After I commented out the $defer property, the binding started to work.
Why can't I use $defer in this case?
As stated in the documentation, to which I linked myself and failed to read it in full, $defer needs the provides() method.
In my case, all I needed to add in my service provider class is this:
public function provides() {
return array(Webhook::class);
}
Thanks to James Fenwick for his comment.
Related
I have created a service provider for the first time, to pass some data to my project view but i get an error message : Undefined variable: core Since i have already registred my new Service Provider in the config file correctly
App\Providers\CoreServiceProvider::class
This is my CoreServiceProvider.php :
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Core;
class CoreServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
view()->composer('*', function($view){
$core = Core::all();
return $view->with('core', $core);
});
}
}
My index.blade.php
dd{{$core}}
if you are making a Custom ServiceProvider class it wont be loaded into the application.
You need to manually register those into Application.
You can register custom service provider under. providers array of config/app.php file.
I'm using PHPStorm and I'm learning Laravel.
For "key too long" error I'm following the fix here: https://laravel.com/docs/master/migrations#creating-indexes
But, PHPStorm complaints for
Method 'defaultStringLength' not found in \Illuminate\Support\Facades\Schema
Why and how can I Solve? This is my AppServiceProvider.php file
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
/**
* Fix for key too long.
* #see https://laravel.com/docs/master/migrations#creating-indexes
*/
Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
I faced the same problem once and followed the answer from the following link to solve my problem. https://stackoverflow.com/a/44859379/4437710
Basically, replace use Illuminate\Support\Facades\Schema; with use Schema; I don't know the reason for this strange behavior. I'm not sure if it will work on your case too. But you can give it a try.
Another trick taken from the internet (Not tested by me):
For Laravel 5.4, use \Illuminate\Database\Schema\Builder::defaultStringLength(191); for correct function reference path instead
I have this this class that is a ServiceProvider
namespace Package\Avatar;
use Illuminate\Support\ServiceProvider;
class AvatarServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
include __DIR__.'/routes.php';
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
try{
$this->app->make('Package\Avatar\AvatarController');
} catch (\Exception $e){
dd($e);
}
}
}
But when I try to access to some url of AvatarCotroller class the screen is Blank, and no show neither error. But whenever I comment this line
$this->app->make('Package\Avatar\AvatarController');
I can get the normal errors of Laravel.
You can get rid of including the routes.php in the boot method of the service provider. Simply use $this->app->call('Package\Avatar\AvatarController#method') to call the method on the controller
try
php artisan optimize : to reuse all frequently used classes php will make an cached class in cache/service.php. So we if add new service we need to run it. We need to use it whenever we add new dependency without using composer.
php artisan cache:clear : clear all the above cache and remap everything
where is the appropriate place to catch every time laravel is used even non http based action?
I want to catch everything even artisan commands, Queues or Task that running.
the only place I can think of is bootstrap\app.php
but its too hacky and with my experience with laravel I am sure there is some built in way of doing it
is there some one place to catch them all?
you can add your logger to your app/Providers/AppServiceProvider.php's boot() function.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
// Your logger goes here
error_log('log...');
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
I am currently working on view composer:
The problem right now that i have, The route that im calling inside the ComposerServiceProvider.php says that it cant find the route to the ViewComposers/LespakketComposer.php
In the Config\app.php i did add the correct App\Providers\ComposerServiceProvider::class,
Here is my code in ComposerServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
class ComposerServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
View::composer('*','App\Http\ViewComposer\LespakketComposer');
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
Here is my error:
ReflectionException in Container.php line 734:
Class App\Http\ViewComposer\LespakketComposer does not exist
The Routes in my folder structure
Does anyone has a solution for my problem?
( The file i am requesting is an Class Indeed )
You forgot a s:
App\Http\ViewComposers\LespakketComposer