Laravel - Service Provider : Class not found - php

I started a project on Laravel 5.4 today and got a ServiceProvider problem.
Here my service provider :
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class WizamProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//die('YESSS');
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind('Wizam\Test', function()
{
if(class_exists("Domains\Domomat\Test"))
return new \Domains\Domomat\Test;
else
return new \Core\Classes\Test;
});
}
}
I added this provider into config/app.php (App\Providers\WizamProvider::class), dump my autoloader like twenty times, clear cache, clear config. Nothing happened.
Here my route :
Route::get('/sub', function()
{
$test = new \Wizam\Test();
echo $test->render();
});
When I go to '/sub', I got Class 'Wizam\Test' not found. I cannot see my error, can you help me ?
Thanks !

To use the container you cannot instantiate using the new command, you can either inject it through the constructor
__constructor(\Wizam\Test $test)
{
}
or using the app(\Wizam\Test::class) I believe is the other way to do it as mentioned in the comments.

If someone once will get error saying that your interface not found ('class not found'), during making service provider closure, make sure your interface and class located in app/ folder. It took a lot of time fixing this. Have a nice day.
P.S. answer is not related to topic, but this page is first I found when I started to google the solution, so I beg you leave this here.

Related

Registering a custom controller class in Laravel

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

In Laravel, why "should you only bind things into the service container and never attempt to register anything else..."

I did see the SO warning that The question you're asking appears subjective and is likely to be closed when I posted this.
I continued posting it because it is from an authoritative source... which is the actual Laravel documentation.
I am asking what they mean and what examples can be given for the warning that is given(see below).
I was reading the documentation on service providers and found the following:
Writing Service Providers
All service providers extend the Illuminate\Support\ServiceProvider
class. Most service providers contain a register and a boot method.
Within the register method, you should only bind things into the
service container. You should never attempt to register any event
listeners, routes, or any other piece of functionality within the
register method.
Source is here
No example is given and I do not quite get what they mean by that statement.
The answer may have been answered elsewhere, in which case I apologise as I didnt find it.
What do they mean? Could you give an example of what not to do?
After all of the providers have been registered, they are “booted”.
A common mistake when using service providers is attempting to use the services provided by another provider in the register method. Since, within the register method, we have no guarantee all other providers have been loaded, the service you are trying to use may not be available yet.
So, service provider code that uses other services should always live in the boot method. The register method should only be used for, you guessed it, registering services with the container. Within the boot method, you may do whatever you like: register event listeners, include a routes file, register filters.
bind('servicName',function($app){
return 'your code logic';
})
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
}
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
app()->bind('servicName',
function($app){
return dd('your code logic');
});
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
}

Laravel webwizo shortcodes package Shortcodes class not found

I recently started using laravel so i'm a beginner, and right now i'm working on a small project which requires me use shortcodes(like the ones in wordpress).
So i searched for a little bit and found this package:
https://packagist.org/packages/webwizo/laravel-shortcodes
I ran the installation and usage the way it's written but i get the error : Class 'App\Providers\Shortcode' not found in the provider I have to make using the laravel make:provider command as specified in the package instructions, below is my exact usage and install code.
added this to the providers array :
/*
* shortcodes providers
*/
Webwizo\Shortcodes\ShortcodesServiceProvider::class,
App\Providers\ShortcodesServiceProvider::class,
Added this to aliases:
'Shortcode' => Webwizo\Shortcodes\Facades\Shortcode::class,
this is the content of my ShortcodesServiceProvider in app/providers:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Shortcodes\JobsForm;
class ShortcodesServiceProvider extends ServiceProvider
{
/**
Bootstrap the application services.
*
#return void
*/
public function boot()
{
//
}
/**
Register the application services.
*
#return void
*/
public function register()
{
Shortcode::register('jobs', JobsForm::class);
}
}
I use laravel 5.4 so that might be an issue.
The thing is the class obviously exists, it gives the Shortcodes class not found error because I think it searches for it in the app/providers/ShortcodesServiceProvider file, and obviously it's not there it's in the vendor file.
Is there something I'm missing i've checked and double checked, I can't seem to get this thing to work.
It shoould work considering it has an alias defined right ?
I used it in the view like this:
return view('quarx-frontend::pages.' . $page->template)->with('page', $page)->withShortcodes();
Thanks for taking the time to read this any help would be much appreciated.
If you need any more info I'll be glad to supply it.
p.s. sorry for bad english ,not a native speaker :P
It searches for Shortcode in the App\Providers; namespace and not in the root namespace where the Facade is defined.
You can fix this in App\Providers\ShortcodesServiceProvider.php by either doing:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Shortcodes\JobsForm;
use Shortcode;
class ShortcodesServiceProvider extends ServiceProvider
{
Or use \Shortcode
/**
* Register the application services.
*
* #return void
*/
public function register()
{
\Shortcode::register('jobs', JobsForm::class);
}
I would recommend the first option.

Which provider do I use to bind my database repositories in laravel 5?

I am working with laravel 5 and I am novice developer so I just learnt how to use the Repository pattern. Now as novices go, I first use the routes.php file to bind my UserRepositoryInterface to DbUserRepository like so:
App::bind('UserRepositoryInterface', 'DbUserRepository');
This is just psuedo, image the namepspaces with the above code too.
So after this I realized that that there is something called a Service Provider which is supposed to contain code like so. Now I refactored this in the AppServiceProvider in my Providers folder and it works fine still.
But since I will be having so many more repositories, is this is a good idea to place them into the AppServiceProvider or should I go ahead and make a dedicated provider for my repositories and bind them there? What is the best way to do this? Is there a standard for this?
So later I got to understand that this all about preference so I coded a RepositoryServiceProvider in which I bind all the repository contracts to the desired implementations like so:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Archive\Repositories\TenantRepository;
use Archive\Repositories\TenantRepositoryEloquent;
use Archive\Repositories\UserRepository;
use Archive\Repositories\UserRepositoryEloquent;
use Archive\Repositories\OrderRepository;
use Archive\Repositories\OrderRepositoryEloquent;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind(TenantRepository::class, TenantRepositoryEloquent::class);
$this->app->bind(UserRepository::class, UserRepositoryEloquent::class);
$this->app->bind(OrderRepository::class, OrderRepositoryEloquent::class);
}
}

laravel 5 configuration service providers for local only

I just read the Laravel 5 doc on configuration based on your environment http://laravel.com/docs/5.0/configuration#environment-configuration
I understand how the .env files can be used to configure some variables but how do I configure what service providers get loaded based on the environment?
For example in my ''local'' environment I use this debugbar plugin which needs a service provider but I don't want to define it in config/app.php for production.
In Laravel 4.2 I could just create config/local/app.php but that doesn't seem to work anymore, what is the alternative?
This article was helpful for me:
https://mattstauffer.co/blog/conditionally-loading-service-providers-in-laravel-5
And this is what I've done and it works:
<?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->app->environment('local')) {
$this->app->register('Barryvdh\Debugbar\ServiceProvider');
}
}
}
I believe if you set APP_DEBUG to false in your .env file the debugbar will go away automatically.
Also I wrote a middleware class to take care of that.
The handle method could look like this in your case:
public function handle($request, Closure $next)
{
if (app()->environment('production'))
\Debugbar::disable();
else
\Debugbar::enable();
return $next($request);
}
Then you just add it to the list of middleware in app/Http/kernel.php and you're good to go.
I have some additional conditions and more environments so this exact code is not tested but it should work and you should get the idea.
Take care.

Categories