Sorry for the English, but I am using the google translator.
First of all I leave my code:
FtpServiceProdiver.php
<?php namespace Jaimemse\Ftp;
use Illuminate\Support\ServiceProvider;
class FtpServiceProvider extends ServiceProvider {
protected $defer = false;
public function boot()
{
$this->package('jaimemse/ftp');
}
public function register()
{
$this->app->bind('ftp', function()
{
return new Ftp;
});
}
public function provides()
{
return array();
}
}
Ftp.php (the class)
<?php namespace Jaimemse\Ftp;
class Ftp {
public function hello()
{
return 'hola';
}
}
Facades/Ftp.php (Facade)
<?php namespace Jaimemse\Ftp\Facades;
use Illuminate\Support\Facades\Facade;
class Ftp extends Facade {
protected static function getFacadeAccessor() { return 'ftp'; }
}
app.php
'Jaimemse\Ftp\FtpServiceProvider',
'Ftp' => 'Jaimemse\Ftp\Facades\Ftp',
If instead of that Facade put this, if it works:
'Ftp' => 'Jaimemse\Ftp\Ftp',
The problem I have is that when using the alias in the file app.php seeks Ftp class in the folder Facades/Ftp.php
Call to undefined method Jaimemse\Ftp\Facades\Ftp::hello()
Someone can help me? Thanks!
You have to extend the BaseController:
<?php namespace Jaimemse\Ftp;
class Ftp extends \BaseController {
public function hello()
{
return 'hola';
}
}
Also your route should be (with namespace):
Route::get('/ftp', 'Jaimemse\Ftp\Ftp#hello');
Also
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\ServiceProvider;
should be
use \Illuminate\Support\Facades\Facade;
use \Illuminate\Support\ServiceProvider;
You should put in app.php
'Jaimemse\Ftp\FtpServiceProvider', in 'providers' array (before 'aliases')
and in 'aliases' array
'Ftp' => 'Jaimemse\Ftp\Facades\Ftp',
I fixed it by adding in register method:
FtpServiceProvider.php
public function register()
{
$this->app->bind('ftp', function()
{
return new Ftp;
});
$this->app->booting(function()
{
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Ftp', 'Jaimemse\Ftp\Ftp');
});
}
Ftp.php
class Ftp {
public function hello()
{
return 'hello';
}
}
App.php
'Jaimemse\Ftp\FtpServiceProvider',
I have not added any app.php alias in the file. I deleted Facade file.
Now I can do things like:
Ftp::hello();
Hope that helps someone. Thank you!
Related
I'm trying to render PWA directives, but I have this error
Error Class 'App\Services\PWA\MetaService' not found
I have this in AppServiceProvider.php
public function boot()
{
Blade::directive('PWA', function (){
return (new App\Services\PWA\MetaService())->render();
});
}
But the class exists
this is the class
<?php
namespace App\Services;
class MetaService
{
public function render(): string
{
return "<?php \$config = (new \App\Services\PWA\ManifestService)->generate(); echo \$__env->make( 'pwa::meta' , ['config' => \$config])->render(); ?>";
}
}
and that is located on app/Services/PWA
and I have the same problem with ManifestService (undefined class)
The MetaService class namespace must be App\Services\PWA. The path you are pointing to is just a folder structure, namespace is the thing that is important.
I'm building a Lumen app. I'm trying to use interface for my repositories. All my logic is wrapped in a composer package.
Here's my vendor/package/src/app/Providers/PackageServiceProvider.php:
<?php
namespace Vendor\Package\App\Providers;
use Illuminate\Support\ServiceProvider;
class SmsBackendCoreServiceProvider extends ServiceProvider
{
protected $defer = false;
public function register()
{
$this->app->bind(
'Vendor\Package\App\Repositories\Contracts\SmsService',
'Vendor\Package\App\Repositories\Services\SmsJson'
);
}
public function boot()
{
$this->app->group(
['namespace' => 'Vendor\Package\App\Http\Controllers'],
function ($app) {
require __DIR__.'/../../routes/web.php';
}
);
}
}
Here is my vendor/package/src/routes/web.php:
<?php
$app->get('/sms/send.json', 'JsonController#send');
Here is my vendor/package/src/app/Http/Controllers/JsonController.php:
<?php
namespace Vendor\Package\App\Http\Controllers;
use Vendor\Package\App\Http\Controllers\BaseController;
use Vendor\Package\App\Repositories\Contracts\SmsService;
class JsonController extends BaseController
{
public $service;
public function __construct(SmsService $service)
{
$this->service = $service;
}
public function send()
{
$response = $this->service->sendSms(1, 2, 3);
return $response;
}
}
Here is my vendor/package/src/app/Repositories/Contracts/SmsService.php:
<?php
namespace Vendor\Package\App\Repositories\Contracts;
class SmsService
{
public function sendSMS($from, $to, $text);
}
Finally, here is my vendor/package/src/app/Repositories/Services/SmsJson.php:
<?php
namespace Vendor\Package\App\Repositories\Services;
use Vendor\Package\App\Repositories\Contracts\SmsService;
class SmsJson implements SmsService
{
public function sendSMS($from, $to, $text)
{
echo 'success';
}
}
When I try to access http://mydomain.dev/sms/send.json, I get this error:
FatalErrorException in SmsService.php line 7: Non-abstract method
Mitto\SmsBackendCore\App\Repositories\Contracts\SmsService::sendSMS()
must contain body
Where did I go wrong?
Your contract is declared as a class, it must be an interface.
I have a class which acts like a storage (add/get item). I try to bind it as a singleton in one service provider, and resolve it in another's boot method.
The code is changed for simplicity.
app/Providers/BindingProvider.php
<?php namespace App\Providers;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\ServiceProvider as ServiceProvider;
class MyBindingFacade extends Facade {
public static function getFacadeAccessor() {
return 'my.binding';
}
}
class MyBinding {
protected $items = [];
public function add($name, $item) {
$this->items[$name] = $item;
}
public function get($name) {
return $this->items[$name];
}
public function getAll() {
return $this->items;
}
}
class BindingProvider extends ServiceProvider {
public function register() {
$this->app->singleton('my.binding', function($app) {
return $app->make('App\Providers\MyBinding');
});
}
public function provides() {
return [
'my.binding',
];
}
}
app/Providers/ResolvingProvider.php
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider as ServiceProvider;
use App\Providers\MyBinding;
class ResolvingProvider extends ServiceProvider {
public function boot(MyBinding $binding) {
$binding->add('foo', 'bar');
// $manual = $this->app->make('my.binding');
// $manual->add('foo', 'bar');
}
public function register() {}
}
app/Http/Controllers/WelcomeController.php
<?php
namespace App\Http\Controllers;
use App\Providers\MyBindingFacade;
class WelcomeController extends Controller {
public function index()
{
dd(MyBindingFacade::getAll()); // debug items
}
}
When I try to debug MyBinding state in my WelcomeController I'm getting empty item array. However, if I uncomment $manual part from my ResolvingProvider it returns an array containing 'foo' => 'bar'. Does it mean IoC resolution is broken in ServiceProvider::boot() method or am I misusing Laravel functionality?
Laravel version: 5.0.28
UPDATE: Added code sample from WelcomeController.
With this:
$this->app->singleton('my.binding', function($app) {
return $app->make('App\Providers\MyBinding');
});
You're saying: my.binding is a singleton and resolves to an instance of App\Providers\MyBinding.
That doesn't mean that App\Providers\MyBinding is registered as singleton too. What you should do instead is this:
$this->app->singleton('App\Providers\MyBinding');
$this->app->bind('my.binding', function($app) {
return $app->make('App\Providers\MyBinding');
});
Because the Facade binding uses $app->make() you should get the same instance you registered with $this->app->singleton() right above.
In the first example you are not using the Facade, you should be using:
use App\Providers\MyBindingFacade as MyBinding;
Which will in fact call make it using 'my.binding'.
I have some trouble with namespace and use.
I get this error: "Trait 'Billing\BillingInterface' not found"
These are the files in my Laravel application:
Billing.php
namespace Billing\BillingInterface;
interface BillingInterface
{
public function charge($data);
public function subscribe($data);
public function cancel($data);
public function resume($data);
}
PaymentController.php
use Billing\BillingInterface;
class PaymentsController extends BaseController
{
use BillingInterface;
public function __construct(BillingPlatform $BillingProvider)
{
$this->BillingProvider = $BillingProvider;
}
}
How to i use use and namespace properly?
BillingInterface is an interface not a trait. Thus it can't find the non existent trait
Also you have an interface called BillingInterface in a namespace called Billing\BillingInterface, the fully qualified name of the interface is: \Billing\BillingInterface\BillingInterface
Perhaps you mean
use Billing\BillingInterface\BillingInterface;
// I am not sure what namespace BillingPlatform is in,
// just assuming it's in Billing.
use Billing\BillingPlatform;
class PaymentsController extends BaseController implements BillingInterface
{
public function __construct(BillingPlatform $BillingProvider)
{
$this->BillingProvider = $BillingProvider;
}
// Implement BillingInterface methods
}
Or to use it as a trait.
namespace Billing;
trait BillingTrait
{
public function charge($data) { /* ... */ }
public function subscribe($data) { /* ... */ }
public function cancel($data) { /* ... */ }
public function resume($data) { /* ... */ }
}
Again the modified PaymentsController, but with fully qualifies names.
class PaymentsController extends BaseController
{
// use the fully qualified name
use \Billing\BillingTrait;
// I am not sure what namespace BillingPlatform is in,
// just assuming it's in billing.
public function __construct(
\Billing\BillingPlatform $BillingProvider
) {
$this->BillingProvider = $BillingProvider;
}
}
When I search the error 'Target Interface is not instantiable' I get a lot of results. Somehow I can still not find the solution to my problem.
I am not sure where it goes wrong.
PartnerController.php
<?php use CmsBlox\MOD\PartnerInterface;
class PartnerController extends BaseController {
public function __construct(PartnerInterface $partner)
{
$this->partner = $partner;
}
public function Get()
{
return "I'm the Get function in class PartnerController";
}
}
PartnerServiceProvider.php
<?php namespace CmsBlox\Providers;
use App, Illuminate\Support\ServiceProvider;
class PartnerServiceProvider extends ServiceProvider {
public function register()
{
}
public function boot()
{
app::bind('CmsBlox\MOD\PartnerInterface') ;
}
}
Routes.php
app::bind('CmsBlox\MOD\PartnerInterface') ;
PartnerInterface.php
<?php namespace CmsBlox\MOD;
interface PartnerInterface {
public function get();
}
I have also added the provider to App.php (for testing also a app::bind() in the routes.php)
'CmsBlox\Providers\PartnerServiceProvider'
As far I know every file should be correct. Somehow I am missing something!
I just found the answer! Thanks to the Laravel.io forums.
<?php use CmsBlox\MOD\PartnerInterface;
class PartnerController extends BaseController implements PartnerInterface {
public function get() {...}
}