I am using Laravel 5.2 and wrote my own Service Provider. I want to inject a Request object into the register method.
The base problem is that I want to call different service container depending on a special request param - all the service container implementing the same interface/contract of course.
The error message I am getting is:
ReflectionException in Container.php line 559:
Function registerService() does not exist
My service provider looks like that:
<?php
namespace App\Providers;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use App\Contracts\Extractor;
class ExtractorServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = true;
/**
* Available services for channels
*
* #var array
*/
protected $availableServices = ['AExtractor', 'ZExtractor'];
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->call('registerService');
}
/**
* #param Request $request
* #return void
*/
protected function registerService(Request $request)
{
$tag = DB::table('channels')->where('id', $request->channel)->value('tag')->first()->tag;
$selectedExtractor = $tag . 'Extractor';
$extractor = 'AExtractor';
if(in_array($selectedExtractor, $this->availableServices)) {
$extractor = $selectedExtractor;
}
$this->app->bind('App\Contracts\Extractor', "App\\Helpers\\{$extractor}");
}
/**
* Get the services provided by the provider.
*
* #return array
*/
public function provides()
{
return [Extractor::class];
}
}
How can I use $this->app->call('registerService'); to call my registerService function and inject the Request object?
The problem is you're calling App:call in a wrong way: you have to specify the object on which you want to call the method and the method, like this :
$this->app->call( [ $this, 'registerService' ] );
Related
So i've got some middleware here:
namespace App\Http\Middleware;
use Closure;
use App\ChatLog;
use App\Http\Services;
class LogChat
{
protected $chatLogService;
public function __construct(ChatLogService $chatLogService)
{
$this->chatLogService = $chatLogService;
}
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$chatLog = new ChatLog;
$chatLog->companyid = '5';
$chatLog->type = 'REQUEST';
$chatLog->ipaddress = '1';
$chatLog->name = 'bob';
$chatLog->message = 'testmessage';
$chatLog->action = 'click';
$chatLog->timeTaken = '1';
$chatLog->fullLog = 'all the log';
$this->chatLogService->store($chatLog);
return $next($request);
}
}
which calls this service here:
namespace App\Http\Services;
use App\ChatLog;
use Illuminate\Support\Facades\DB;
class ChatLogService
{
/**
* Display a listing of the resource.
*
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \App\ChatLog $chatLog
*/
public function store(ChatLog $chatLog)
{
$chatLog->save();
}
/**
* Display the specified resource.
*
* #param \App\ChatLog $chatLog
*/
public function show(ChatLog $chatLog)
{
//
}
}
which is injected in here:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind(ChatLogService::class, function ($app) {
return new ChatLogService();
});
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
}
But this gives me the following exception: ReflectionException
Class App\Http\Middleware\ChatLogService does not exist
What i want to do is call the "store" method in a nicely abstracted way and i feel this solution is very close - however, I can't seem to get around this error. I'm not sure if it's something simple that i'm missing, or if the approach is fundamentally wrong!
I can't see what's wrong!
Ta :)
I am trying to share variables on multiple views so i tried the view composer but it is not working as its not passing variables and is there any way to debug it
ComposerServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
view()->composer(
'layouts.admin.dashboard',
'App\Http\ViewComposers\StatComposer'
);
}
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
}
So now here will be the Composer File
StatComposer.php
<?php
namespace App\Http\ViewComposers;
use Analytics;
use Spatie\Analytics\Period;
use App\Libraries\GoogleAnalytics;
use Illuminate\Http\Request;
use Illuminate\View\View;
class StatComposer
{
/**
* Create a movie composer.
*
* #return void
*/
public function __construct()
{
$result = GoogleAnalytics::topCountries();
$country = $result->pluck('country');
$country_sessions = $result->pluck('sessions');
$topBrowsers = GoogleAnalytics::topBrowsers();
$browser = $topBrowsers->pluck('browser');
$browser_sessions = $topBrowsers->pluck('sessions');
$totalPageViews = GoogleAnalytics::fetchVisitorsAndPageViews();
$date = $totalPageViews->pluck('date');
$visitors = $totalPageViews->pluck('visitors');
$pageViews = $totalPageViews->pluck('pageViews');
}
/**
* Bind data to the view.
*
* #param View $view
* #return void
*/
public function compose(View $view)
{
$view->with('country', 'country_sessions', 'browser', 'browser_sessions','date','visitors','pageViews');
}
}
So I am unable to find way to debug it as the variables are not passing to the view i am trying to pass them to and the view is giving error of undefined variable.
The issue is your declaring a lot of variables without providing the values in the compose function. Also your not actually saving the data anywhere. It should be like this.
protected $country;
/**
* Create a movie composer.
*
* #return void
*/
public function __construct()
{
$result = GoogleAnalytics::topCountries();
$this->country = $result->pluck('country');
}
/**
* Bind data to the view.
*
* #param View $view
* #return void
*/
public function compose(View $view)
{
$view->with('country', $this->country);
}
Note we are declaring the classes property at the top, then saving to that property and then setting the 'country' variable in the with method with the $this->country property.
I am just starting to get the hang of Service Providers and the IoC container, however one thing is confusing me. I have a SpamServiceProvider that requires two other classes to function. However one of those classes, InvalidKeywords, has a array $blacklist parameter which needs to be passed to its constructor.
If I register that class in the AppServiceProvider and pass in the $blacklist array, everything works fine. However, if I try to bind the class in the SpamServiceProvider instead it will not inject the $blacklist into InvalidKeywords constructor.
So I guess my question is why is this? And is there a way to keep bindings like this together in a single container or do I simply have to bind InvalidKeywords inside the AppServiceProvider?
This works
class SpamServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = true;
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind(SpamManager::class, function ($app) {
return new SpamManager(new InvalidKeywords, new RepeatedCharacters);
});
}
}
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind(InvalidKeywords::class, function ($app) {
return new InvalidKeywords(config('spam.blacklist'));
});
}
}
This does not work
class SpamServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = true;
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind(InvalidKeywords::class, function ($app) {
return new InvalidKeywords(config('spam.blacklist'));
});
$this->app->bind(SpamManager::class, function ($app) {
return new SpamManager(new InvalidKeywords, new RepeatedCharacters);
});
}
}
In the second case you're not resolving the InvalidKeywords class from the container, simply creating a new instance. Instead, try using app or resolve helpers when creating the SpamManager:
$this->app->bind(SpamManager::class, function ($app) {
return new SpamManager(resolve(InvalidKeywords::class), resolve(RepeatedCharacters::class));
});
// or
$this->app->bind(SpamManager::class, function ($app) {
return new SpamManager(app(InvalidKeywords::class), app(RepeatedCharacters::class));
});
I would create a singleton with InvalidKeywords as well:
$this->app->singleton(InvalidKeywords::class, function ($app) {
return new InvalidKeywords(config('spam.blacklist'));
});
I have tried for days to get this to work.
I am using this framework: https://github.com/DennisSkoko/discord-bot to create a bot. But I wan't to add the DIC container http://container.thephpleague.com/ to this project.
What I want to do is in the Main class is to register service providers (modules/packages/bundles) or whatever you want to call them, just like Laravel etc. does.
So I created a module/ dir and added a serviceprovider class and some other classes as the docs state: http://container.thephpleague.com/service-providers/
First I edited the start.php to inject the container:
use League\Container\Container;
require "vendor/autoload.php";
require "bot.php";
$container = new Container;
$main = new DS\Main(new Example($container));
$main->run();
I changed the Example bot to include a getContainer() function so I could read it from the Main class, like this:
use DS\Bot;
use Discord\Discord;
use DS\Service;
class Example extends Bot
{
/**
* Example constructor
*/
public function __construct($container)
{
$this->config = [
"token" => "My token"
];
$this->container = $container;
}
/**
* Will be executed when the WebSocket is ready and before the services are implemented.
*
* #param Discord $discord
*
* #return void
*/
public function setup(Discord $discord)
{
echo "Example is ready to start!";
}
public function getContainer()
{
return $this->container;
}
/**
* Will return an array of Service that the bot uses.
*
* #return Service[]
*/
public function getServices()
{
return [];
}
}
I then changed the Main class to to register my service provider.
namespace DS;
use Discord\Discord;
/**
* A class that bring bots script into life.
*/
class Main
{
/**
* #var Discord
*/
private $discord;
/**
* #var Bot
*/
private $bot;
/**
* Main constructor.
*
* #param Bot $bot
*/
public function __construct(Bot $bot)
{
$this->bot = $bot;
$this->discord = new Discord($this->bot->getConfig());
$this->discord->on("ready", function ($discord) {
$this->bot->setup($discord);
$this->setServices($this->bot->getServices());
});
$this->container = $this->bot->getContainer();
$this->container->addServiceProvider('Mynamespace\HelloWorld\ServiceProvider');
}
/**
* Will run the bot.
*
* #return void
*/
public function run()
{
$this->discord->run();
}
/**
* Will add the services for the WebSocket.
*
* #param Service[] $services
*
* #return void
*/
private function setServices($services)
{
foreach ($services as $service) {
$this->discord->on($service->getEvent(), $service->getListener());
}
}
}
Here is the problem:
PHP Fatal error: Uncaught exception 'InvalidArgumentException' with message 'A service provider must be a fully qualified class name or instance of (\League\Container\ServiceProvider\ServiceProviderInterface)'
Which is weird because I did it just like in the documentation and extended League\Container\ServiceProvider\AbstractServiceProvider
This I have double checked 20 times.
So I have no idea what to do about this. How can I use addServiceProvider() in the Main class and register things in the DIC?
I do not really want to do it in the Example class if possible. Because that should be extended by the user and the Main class will bootstrap the bot.
I also tried $this->bot->addServiceProvider() but then I get the following error:
PHP Fatal error: Call to undefined method Example::addServiceProvider()
Any help appreciated.
I'm trying my hand at building a custom Service Provider package, however I'm running into the following error. Does anyone have experience with this?
Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\Support\ServiceProvider
Package Folder Structure:
[root]
....packages/
........mbarwick83/
............previewr/
................src/
....................PreviewrServiceProvider.php
....................Previewr.php
................composer.json
config/app.php:
Mbarwick83\Previewr\PreviewrServiceProvider::class
Service Provider:
<?php
namespace Mbarwick83\Previewr;
use Illuminate\Support\ServiceProvider;
class PreviewrServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = false;
/**
* Perform post-registration booting of services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any package services.
*
* #return void
*/
public function register()
{
$this->app->bind('Mbarwick83\Previewr\Previewr',function($app){
return new Previewr($app);
});
}
}
Previewr.php (class):
<?php
namespace Mbarwick83\Previewr;
class Previewr
{
/**
* Create a new Previewr Instance
*/
public function __construct()
{
//
}
/**
* Friendly welcome
*
* #param string $phrase Phrase to return
*
* #return string Returns the phrase passed in
*/
public function something($phrase)
{
return $phrase;
}
}
Controller/view:
use Mbarwick83\Previewr\PreviewrServiceProvider as Previewr;
public function index(Previewr $previewr)
{
echo $previewr->something('Hello, League!');
}
composer.json:
"autoload": {
"psr-4": {
"Mbarwick83\\Previewr\\": "packages/Mbarwick83/Previewr/src"
}
},
In your controller, are you sure you want to inject your service provider?
use Mbarwick83\Previewr\PreviewrServiceProvider as Previewr;
Chances are you want to use this instead:
use Mbarwick83\Previewr\Previewr;