Error on Creating Facade in Laravel 5.3 - php

I try to create facade in laravel.
My Facade :
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class someclass_f extends Facade
{
protected static function getFacadeAccessor()
{
return new \App\Someclass();
}
}
My Base Class :
namespace App;
class Someclass
{
public function get($data = [])
{
echo "foo";
}
}
In Calling :
use App\Facades\someclass_f;
class my_class{
function(){
someclass_f::get();
}
}
I got error as :
Class 'App\Facades\someclass_f' not found
What's wrong with this ?
Any suggestions ?

Why do you call Someclass::get()? Your facade name is someclass_f.
Call it like: someclass_f::get(); and that will call get() function in \App\Someclass.

Related

Why I am unable to assert that method has been called on my mocked service?

I have made a simple class named MyService
namespace App\Services;
class MyService
{
private $anotherService;
public function setService(AnotherService $anotherService)
{
$this->anotherService = $anotherService;
}
public function getService()
{
if(empty($this->anotherService)){
$this->setService(new AnotherService());
}
return $this->anotherService;
}
public function call()
{
$anotherService = $this->getService();
$anotherService->SetXY(5,10);
}
}
As you can se via a setter I set as Depedency the AnotherService:
namespace App\Services;
class AnotherService
{
public function SetXY($x,$y)
{
}
}
In order to test whether the MyService runs as expected I made the following test:
namespace Tests\Services;
namespace Tests\Services;
use App\Services\MyService;
use App\Services\AnotherService;
use PHPUnit\Framework\TestCase;
use Mockery;
class MyServiceTest extends TestCase
{
public function testService()
{
$mockedAnotherService = Mockery::spy(AnotherService::class);
$mockedAnotherService->shouldReceive('SetXY');
$service = new MyService();
$service->setService($mockedAnotherService);
$service->call();
$mockedAnotherService->shouldHaveReceived()->setXY(5,10);
}
}
But for some reason seems that I am unable to assert that setXY is called despite the opposite. The error is:
1) Tests\Services\MyServiceTest::testService
Mockery\Exception\InvalidCountException: Method setXY(<Any Arguments>) from Mockery_0_App_Services_AnotherService should be called
at least 1 times but called 0 times.
/var/www/html/api/vendor/mockery/mockery/library/Mockery/CountValidator/AtLeast.php:47
/var/www/html/api/vendor/mockery/mockery/library/Mockery/Expectation.php:310
/var/www/html/api/vendor/mockery/mockery/library/Mockery/ReceivedMethodCalls.php:46
/var/www/html/api/vendor/mockery/mockery/library/Mockery/VerificationDirector.php:36
/var/www/html/api/vendor/mockery/mockery/library/Mockery/HigherOrderMessage.php:46
/var/www/html/api/tests/Services/MyServiceTest.php:23
phpvfscomposer:///var/www/html/api/vendor/phpunit/phpunit/phpunit:60
Do you know why that does happen?
There is a typo in:
$mockedAnotherService->shouldHaveReceived()->setXY(5,10);
Should be:
$mockedAnotherService->shouldHaveReceived()->SetXY(5,10);

Class 'App\TestService' not found - Laravel

Problem
I created service TestService, that I use in colntroller file TestController in function test().
When I called test(), I got an error:
local.ERROR: Class 'App\TestService' not found {"userId":1,"exception":"[object] (Error(code: 0): Class 'App\TestService' not found at /Backend/app/Http/Controllers/TestController.php:8)
Code
TestController.php:
<?php
namespace App\Http\Controllers;
use App\TestService;
class TestController extends Controller
{
public function test()
{
return response()->json(TestService::getTest());
}
}
TestService.php:
<?php
namespace App;
use App\TestService;
class TestService
{
public static function getTest()
{
return "test";
}
}
What I tried
I checked all the names and they are correct.
When I wrote in the colntroller file use App\TestService;
I had autocomplete, so the service and name are visible.
I used these commands to refresh the files: php artisan serve and php artisan clear-compiled.
But it still doesn't work.
You have not correctly defined Namespace.
The namespace must be a directory path where you have created a file.
TestService.php:
<?php
namespace App\Services\Tests;
class TestService
{
public static function getTest()
{
return "test";
}
}
TestController.php:
<?php
namespace App\Http\Controllers;
use App\Services\Tests\TestService;
class TestController extends Controller
{
public function test()
{
//Call service class like.
return response()->json(TestService::getTest());
}
}

create a custom external class in laravel 5.5

I would like to create an external class in Laravel, I want to use this class in my cotroller function.
What is the best way to do that?
thanks
Create a class and define its namespace. For example:
<?php
namespace App\Services;
class MyClass
{
public function doSomething()
{
dd('It\'s working');
}
}
Run composer du command.
You'll be able to use the class in a controller with:
(new App\Services\MyClass)->doSomething();
Or with IoC:
app('App\Services\MyClass')->doSomething();
If you're using IoC, you'll also be able to inject the class into controller constructor:
use App\Services\MyClass;
protected $myClass;
public function __construct(MyClass $myclass)
{
$this->myClass = $myClass;
}
public function index()
{
$this->myClass->doSomething();
}
Used common function any file like controller,models and all blade file valid.
Please try your helpers.php file inner created.
file path like : laravel/app/helpers.php
Code
if (!function_exists('classActivePath')) {
function classActivePath($path) {
return Request::is($path) ? ' class="active"' : '';
}
}

Laravel interface dependency inject

I have a interface dependency inject Question,
the error message look like can`t catch the class,
have any ideal?
Error Message:
FatalThrowableError in UserController.php line 27:
Class 'App\Http\Controllers\App' not found
My folder path :
UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Domain\Services\Social\SocialService;
use Domain\Services\Social\SocialInterface;
use Domain\Services\Social\Facebook;
class UserController extends Controller
{
public function fb(Request $request)
{
App::bind(SocialInterface::class, Facebook::class);
$target = App::make(SocialService::class);
return $target ->getCallbackData($request);
}
}
SocialService.php
namespace Domain\Services\Social;
class SocialService
{
public function getCallbackData(SocialInterface $social,$request)
{
return $social->getCallbackData($request);
}
}
Facebook.php
namespace Domain\Services\Social;
class Facebook implements SocialInterface
{
public function getCallbackData($request)
{
//Somethig inside...
}
}
public function fb(Request $request)
{
\App::bind(SocialInterface::class, Facebook::class);
$target = \App::make(SocialService::class);
return $target ->getCallbackData($request);
}
Try it with backslash.

Laravel 5 Resolving dependencies in ServiceProvider

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'.

Categories