Laravel 5 Custom ServiceProvider not found - php

I'm making a Laravel ServiceProvider for a package.
The package is https://github.com/sumocoders/Teamleader
I get the following error
FatalErrorException in ProviderRepository.php line 150:
Class 'Notflip\Teamleader\TeamleaderServiceProvider' not found
I have no clue what I'm doing wrong, Here's my folder structure
composer.json in my package
"autoload": {
"psr-4": {
"Notflip\\Teamleader": "src/"
}
}
TeamleaderServiceProvider
<?php namespace Teamleader\Laravel;
use Illuminate\Support\ServiceProvider;
class TeamleaderServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* #return void
*/
public function publishes()
{
$this->publishes([
__DIR__.'/Config/config.php' => config_path('teamleader.php'),
]);
}
public function register()
{
$this->app->bind('Teamleader\Laravel', function () {
return new Teamleader(config('teamleader.API_GROUP'), config('teamleader.API_SECRET'), config('teamleader.SSL'));
});
}
}
Facade
<?php namespace Teamleader\Laravel\Facade;
class Teamleader extends Facade
{
protected static function getFacadeAccessor()
{
return 'Teamleader';
}
}
In my config.php I added the following line to the providers
'Notflip\Teamleader\TeamleaderServiceProvider',
And this line to the aliasses
'Teamleader'=> 'Notflip\Teamleader\Facade\Teamleader'
Anyone has any idea what I might be doing wrong? Thank you! I'm so close to the result!

Your definition in composer is missing the initial slashes and you haven't specified the path to src from root.
"psr-4": {
"\\Notflip\\Teamleader": "notflip/teamleader-laravel/src/"
}
Also your declaration of the name space at the top of TeamleaderServiceProvider is wrong, it should be:
<?php namespace Notflip\Teamleader;

Solved
In the facade, the IOC binding was named wrong ( wrong case )
The name should have been 'teamleader' in lowercase.
Facade
class Teamleader extends Facade
{
protected static function getFacadeAccessor()
{
return 'teamleader';
}
}
Service Provider
<?php namespace Teamleader\Laravel;
use Illuminate\Support\ServiceProvider;
class TeamleaderServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* #return void
*/
public function publishes()
{
$this->publishes([
__DIR__.'/Config/config.php' => config_path('teamleader.php'),
]);
}
public function register()
{
$this->app->bind('teamleader', function () {
return new Teamleader(config('teamleader.API_GROUP'), config('teamleader.API_SECRET'), config('teamleader.SSL'));
});
}
}

Related

Target [Interface] is not instantiable while building [Controller]

I'm working with Laravel 9 and I have a Controller like this:
use App\Repositories\HomeRepositoryInterface;
class HomeController extends Controller
{
private $homeRespository;
public function __construct(HomeRepositoryInterface $homeRepository)
{
$this->homeRespository = $homeRepository;
}
...
And here is the HomeRepositoryInterface:
<?php
namespace App\Repositories;
interface HomeRepositoryInterface
{
public function newest();
}
And this is the HomeRepository itself:
<?php
namespace App\Repositories;
use App\Models\Question;
class HomeRepository implements HomeRepositoryInterface
{
public function newest()
{
return $ques = Question::orderBy('created_at', 'DESC')->paginate(10);
}
}
But now I get this error:
Target [App\Repositories\HomeRepositoryInterface] is not instantiable while building [App\Http\Controllers\HomeController].
So what's going wrong here?
How can I solve this issue?
It seems that you did not introduce the service container.
For this, it is better to create a service provider as shown below and introduce the repository class to the container.
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Repositories\HomeRepositoryInterface;
use App\Repositories\HomeRepository;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
// Bind Interface and Repository class together
$this->app->bind(HomeRepositoryInterface::class, HomeRepository::class);
}
}
Next, you should introduce this service provider in the config/app.php file.
'providers' => [
...
...
...
App\Providers\RepositoryServiceProvider::class,
],

Facades in laravel 8 Is perfectly working in controller file but i want to use on facade function in blade file not working

Facades in Laravel 8 Are perfectly working in the controller file but I want to use on facade function in the blade file not working
error:
Error Call to undefined method App\PaymentGateway\PaymentFacade::process() (View: D:\alpha\resources\views\admin\auth\register_coverage.blade.php)
**On Blade file, this is facade function **
{{ \Payment::process() }} //// this is not working
Controller file
public function registerStore(Request $request) {
dd(Payment::process()); ///// this is working
}
I want to know the solution to this problem.
Payment File
<?php
namespace App\PaymentGateway;
class Payment {
public static function process(){
return "testing";
}
}
PaymentFacade File
<?php
namespace App\PaymentGateway\Facades;
class PaymentFacade {
protected static function getFacadeAccessor(){
return 'payment';
}
}
PaymentServiceProvier File
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\PaymentGateway\Payment;
class PaymentServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind('payment',function(){
return new Payment;
});
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
}
Config/app file
'providers' => [ App\Providers\PaymentServiceProvider::class,
],
'aliases' => [ 'Payment'=> App\PaymentGateway\Facades\PaymentFacade::class,
],
You should extend the base Facade class. Change App/PaymentGateway/Facades/PaymentFacade.php to
<?php
namespace App\PaymentGateway\Facades;
use Illuminate\Support\Facades\Facade;
class PaymentFacade extends Facade {
protected static function getFacadeAccessor(){
return 'payment';
}
}

ServiceProvider binded class not found

Case (L5.4)
Currently trying to write an api wrapper using the package development Laravel offers.
I got a ServiceProvider which binds the model (Niki::class)
class NikiServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
$this->publishes([
__DIR__ . '/config/niki.php' => config_path('niki.php'),
]);
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind('niki', function () {
return new Niki;
});
}
}
A Facade which registers the name of the component
class Facade extends \Illuminate\Support\Facades\Facade
{
/**
* Get the registered name of the component.
*
* #return string
*/
public static function getFacadeAccessor()
{
return 'niki';
}
}
And a model
class Niki extends Model
{
/**
* Config
*
* #var array
*/
public function __construct()
{
$this->config = config('niki')['api_key'];
}
public static function getHouses()
{
$response = $this->config;
return $response;
}
}
Above files are located in packages/prsc/niki/src and are being loaded using the psr-4 autoloading:
"psr-4": {
"App\\": "app/",
"PRSC\\Niki\\": "packages/prsc/niki/src/"
},
Error
So now my problem, the bind in the ServiceProvider returns a FatalError because of the file not being found.
FatalThrowableError in NikiServiceProvider.php line 37: Class
'PRSC\Niki' not found
I think it's just a namespace problem. I'm not sure I have all the clue about your namespaces, but here is something that should work (if I did not misunderstood):
Replace:
return new Niki;
By:
return new \PRSC\Niki\Niki;
If it does not work, please add your namespaces in each code snippet you pasted.

Custom class and ServiceProvider - Laravel 5.4

I try to make a new custom class in laravel 5.4 to check the user type.
I add this new class in a new folder app\ItSolution, code:
<?php
namespace App\ItSolution;
class DemoClass {
public function getPermission() {
switch(Auth::user()->user_type_id) {
case 1:
return 'admin';
break;
case 2:
return 'promoter';
break;
case 3:
return 'customer';
break;
default:
return false;
}
}
}
I want to use this class in all my app , so i try to make a new ServiceProvider, code :
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App;
class AuthLibServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
App::bind('democlass', function()
{
return new \App\ItSolution\DemoClass;
});
}
}
And i make a new facade for my class in the same folder app\ItSolution, code:
<?php
namespace App\ItSolution;
use Illuminate\Support\Facades\Facade;
class DemoClassFacade extends Facade {
protected static function getFacadeAccessor() { return 'democlass'; }
}
After that i add this line in app/config.php
'aliases' => [
...
'DemoClass'=> App\ItSolution\DemoClassFacade::class,
]
'providers' => [
...
App\Providers\AuthLibServiceProvider::class,
]
But i have this error when i try to use the DemoClass alias in my controller
DemoClass::getPermission():
Class 'App\Http\Controllers\DemoClass' not found
How can i fix that please, Thnaks.
In laravel 5.4 you don't need a Service Provider to register a facade, you can use automatic Facadaes, you have to define only the DemoClass.
i.e in a controller:
use Facades\ {
App\ItSolution\DemoClass
};
And call the function
DemoClass::getPermission()
Source here
You're registering this class as facade, so you'll need to add this to the beginning of the class:
use DemoClass;
Or you can just use full namespace when using the facade:
\DemoClass::

Laravel cannot find class of a package

Hello I have created a new Package for a Laravel project I am working on, I am new to the concept of packages and laravel itself, but here is the code I have come up with,
/workbench/cycs/proofhq/src/Cycs/Proofhq/ProofhqServiceProvider.php
public function boot()
{
$this->package('cycs/proofhq');
}
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
$this->app->booting(function()
{
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Cycs', 'Cycs\Proofhq\Facades\Supyo');
});
$this->app['proofhq'] = $this->app->share(function($app)
{
return new Proofhq;
});
}
/workbench/cycs/proofhq/src/Cycs/Proofhq/Proofhq.php
<?php namespace Cycs\Proofhq;
class Proofhq {
public static function greeting() {
return "What's up dawg!";
}
}
/workbench/cycs/proofhq/src/Cycs/Proofhq/Facades/Proofhq.php
<?php namespace Cycs\Proofhq\Facades;
use Illuminate\Support\Facades\Facade;
class Proofhq extends Facade {
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor() {
return 'proofhq';
}
}
I have added the package to the app/config/app.php and the providers array, then try to access the package functions via a simple get,
Route::get('/test', function(){
echo proofhq::greeting();
});
But I get the following error,
Class 'proofhq' not found
I cannot work out why, I have followed the examples to the letter, and the class exists.
Can anyone shed anylight on this for me?
composer dump-autoload and change the class name first letter in the routes to uppercase seems to do the trick!

Categories