Laravel IoC doesn't automatically resolving the interface - laravel

I've just crossed this example, but it fails to resolve binding interface to implementation.
Having the follow code files:
// File: app/App/Services/Talkable.php
<?php
namespace App\Services;
interface Talkable {
public function talk();
}
// File: app/App/Services/Cat.php
<?php
namespace App\Services;
use App\Services\Talkable;
class Cat implements Talkable
{
public function talk()
{
return 'meow meow';
}
}
// File: app/Jobs/MakeSomeNoise.php
<?php
namespace App\Jobs;
use App\Jobs\Job;
use App\Services\Talkable;
class MakeSomeNoise extends Job
{
private $talkable;
public function __construct(Talkable $talkable)
{
$this->talkable = $talkable;
}
public function handle()
{
return ($this->talkable->talk());
}
}
The binding are taken place in app/Providers/AppServiceProvider.php
// File: app/Providers/AppServiceProvider.php
...
$this->app->bind('App\\Services\\Talkable', 'App\\Services\\Cat');
The MakeSomeNoise job is dispatched from a Controller
// File: any controller
public function makeNoises()
{
return $this->dispatch(new MakeSomeNoise); // (*)
}
At the (*), I expect Laravel will automatically resolve the binding, but it doesn't. Here the error,
Argument 1 passed to App\Jobs\MakeSomeNoise::__construct() must be an instance of App\Services\Talkable, none given, called in ...
But if I just inject into controller constructor, it works fine.
Any thought on this?

My mistake in the code. The DI should be taken in handle() method, not constructor.
public function handle(Talkable $talkable) {
// blah lbah
}

Related

Call to a member function reply() on null laravel botman

This is the code of model. I am unable to change access the function of conversation using the model method as i am passing data from view to botman conversation constructor through request.
<?php
This is the conversation class
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Conversations\TestConversation;
class CheckSaved extends Model
{
public function hasDate($scheduled_date):bool
{
if($scheduled_date)
{
$testconv = new TestConversation($scheduled_date);
$testconv->showOther();
}
return false;
}
}][1]
<?php
namespace App\Conversations;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Conversations\Conversation;
use App\Http\Traits\PropertyDetails;
use App\Models\CheckSaved;
class TestConversation extends Conversation
{
public $is_saved;
public function __construct($request)
{
if(isset($request->scheduled_date))
{
$checkDate = new CheckSaved();
$this->is_saved = $checkDate->hasDate($request->scheduled_date);
}
}
public function sayHi()
{
// dd($request);
$this->say("<input type='datetime-local' id='birthdaytime' name='birthdaytime'>");
$this->say("<button class='btn-date' onclick='test()'>Save</button>");
}
public function showOther()
{
$this->say('Hi');
}
public function run()
{
$this->sayHi();
}
}
This is the image of Model
I am getting reply() on null if try to call a function called "showOther()" inside TestConversation. I am stuck on it please someone help or contact me.

Laravel :: call method in helper class with constructor

as I mentioned in last question I am beginner in Laravel therefore I need some help ,I have to make helper class to create random Id for some tables ,I create this class with table constructor to recieve deffirent tables :
<?php
namespace App\Helpers;
use Illuminate\Support\Facades\DB;
class RandomId
{
public function __construct($my_table)
{
$this->my_table=$my_table;
}
public function get_id()
{
$id = mt_rand(1000000000, 9999999999);
if($this->check_id($id)){
get_id();
}
return $id;
}
public function check_id($id)
{
$table=DB::table($this->my_table)->where('id',$id)->get();
if (count($course)==0){
return false;
}
return true;
}
}
then I add it as alias in config/app.php
'RandomId' => App\Helpers\RandomId::class,
now I want to call it in controller ,I did somethinf like this but don't work :
<?php
namespace App\Http\Controllers\course;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Helpers\RandomId;
class Course_controller extends Controller
{
public function add(Request $request)
{
$id=RandomId::get_id('courses');
dd($id);
}
}
I get this error :Non-static method App\Helpers\RandomId::get_id() should not be called statically
It's because get_id function is not static. You should add static keyword when defining static methods:
public static function get_id()
{
....

bind abstract class service in provider laravel

Gives an error:
Target [MyVendor\ProductList\ProductServiceInterface] is not instantiable.
ProductServiceInterface
namespace MyVendor\ProductList;
interface ProductServiceInterface
{
public function productList();
public function getProductSpeed();
}
ProductColorService
namespace MyVendor\ProductList\Service;
abstract class ProductColorService implements \MyVendor\ProductList\ProductServiceInterface
{
public function productList()
{
$color = "black";
return $color;
}
}
** ProductSpeedService **
namespace MyVendor\ProductList\Service;
abstract class ProductSpeedService implements \MyVendor\ProductList\ProductServiceInterface {
public function getProductSpeed() {
$speed = 200;
return $speed;
}
}
Provider :
namespace MyVendor\ProductList;
use Illuminate\Support\ServiceProvider;
use MyVendor\ProductList\ProductServiceInterface;
use MyVendor\ProductList\Service\ProductColorService;
class ProductColorServiceProvider extends ServiceProvider
{
public function boot()
{
$this->loadRoutesFrom(__DIR__ . '/routes/color.php');
}
public function register()
{
$this->app->bind('MyVendor\ProductList\ProductServiceInterface','MyVendor\ProductList\Service\ProductColorService');
$this->app->bind('MyVendor\ProductList\ProductServiceInterface','MyVendor\ProductList\Service\ProductSpeedService');
}
}
Whats wrong in this code? Actually I need to override some functions in interface using service. ie each service should override one method.
namespace MyVendor\ProductList\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use MyVendor\ProductList\Models\Product;
use MyVendor\ProductList\ProductServiceInterface;
class ProductListController extends Controller
{
public function index(ProductServiceInterface $product_service)
{
$color = $product_service->productList();
$speed = $product_service->getProductSpeed();
dd($color," = ",$speed);
$product = Product::get();
return view('ProductList::product',compact('product'));
}
}
here productList() in one service and productSpeed() from another service
You can't instantiate abstract classes. The whole point of them is there can never be an instance of them, so if you bind your interface to your abstract class with a method, it simply can never be abstract. Abstract is for classes in inheritance structures you do not want to have the users to create new ones.
So there is no reason for the abstract, abstract is for something like this.
abstract class Vehicle {
protected $wheels;
public function getWheels() {
return $this->wheels;
}
}
class Car extends Vehicle {
protected $wheels = 4;
}
class Bike extends Vehicle {
protected $wheels = 2;
}
You can not make a vehicle as it is not a concrete implementation, as it is a parent class to help with methods shared across two classes. So short version, just remove the abstract keyword. If you don't know why you add it, it is not needed.

how to access to laravel global Classes in packages

I am developing a Laravel package . In the packeges I need to File::delete for delete files, but The following error message is displayed :
Class 'Chee\Image\File' not found
Can you help me?
You have to declare it in the top of your class:
namespace Chee\Image;
use File;
class Whatever()
{
}
Instead of using the File Facade, you can also get it directly from the Laravel IoC container:
$app = app();
$app['files']->delete($path)
In a service provider, you can inject it as a dependency your package class:
class Provider extends ServiceProvider {
public function register()
{
$this->app['myclass'] = $this->app->share(function($app)
{
return new MyClass($app['files']);
});
}
}
And receive it in your class:
class MyClass {
private $fileSystem;
public function __construcy($fileSystem)
{
$this->fileSystem = $fileSystem;
}
public function doWhatever($file)
{
$this->fileSystem->delete($file)
}
}
You should be able to just use:
\File
Namespaces are relative to the namespace you declare for the class you are writing. Adding a "\" in front of a call to a class is saying that we want to look for this class in the root namespace which is just "\". The Laravel File class can be accessed this way because it is an alias that has an declared in the root namespace.
Assuming you have file something like that:
<?php
namespace Chee\Image;
class YourClass
{
public function method() {
File::delete('path');
}
}
you should add use directive:
<?php
namespace Chee\Image;
use Illuminate\Support\Facades\File;
class YourClass
{
public function method() {
File::delete('path');
}
}
Otherwise if you don't use PHP is looking for File class in your current namespace so it is looking for Chee\Image\File. You could look at How to use objects from other namespaces and how to import namespaces in PHP if you want

Grouping event registrations on a service provider

I want to define a service provider that registers events. So have done the following (taken from a book)
<?php
namespace MyApp\Providers;
use Illuminate\Support\ServiceProvider;
class EventsProvider extends ServiceProvider
{
public function boot()
{
Event::listen('some.event', function($parameter)
{
// Handle the event..
});
}
}
Then I added it to the provider array.
But when I execute the code I get the following error
implement the remaining methods
(Illuminate\Support\ServiceProvider::register)
It compels me to declare a register() method.
When I add to the EventsProvider class a method named register() (without implementation, and just make it return null) I get the following error
Class 'MyApp/Providers/EventProvider' not found
Why is that, and how can I solve it?
You just need to declare it, because it's declared in the Interface:
<?php namespace MyApp\Providers;
use Illuminate\Support\ServiceProvider;
class EventsProvider extends ServiceProvider {
public function boot()
{
Event::listen('some.event', function($parameter)
{
// Handle the event..
});
}
public function register()
{
}
}
Then execute
composer dump-autoload --optimize
But you might also have a typo somewhere because it says Class 'MyApp/Providers/EventProviders' not found, but it should be MyApp/Providers/EventProvider.

Resources