Laravel 8 dependency injection error: Target class ... does not exist - php

Could someone explain what wrong with this code and what should I do?
I have a CustomerManager.php file located in app\BusinessLogic\Managers folder:
<?php
namespace app\BusinessLogic\Managers;
class CustomerManager
{
public function putItem()
{
// some code
}
}
This class should be injected into RetailController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
use app\BusinessLogic\Managers;
class RetailController extends Controller
{
protected $customerManager;
public function __construct(CustomerManager $customerManager)
{
$this->customerManager = $customerManager;
}
}
But as result error appers:
Illuminate\Contracts\Container\BindingResolutionException
Target class [app\BusinessLogic\Managers\CustomerManager] does not exist.

Related

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());
}
}

ReflectionException Container.php on line 826

I have two classes in the services layer
CashTransactionServices.php
namespace App\Services\Cash;
use App\Models\Cash\CashTransaction;
use App\Models\Cash\CashBoxTransaction;
use DB;
class CashTransactionServices {
protected $AccountingAPI;
public function __construct(AccountingAPI $acc) {
$this->AccountingAPI = $acc;
}
CashBoxTransactionServices.php
namespace App\Services\Cash;
use App\Models\Cash\CashBoxTransaction;
class CashBoxTransactionServices extends CashTransactionServices {}
and in the controller
namespace App\Http\Controllers\Cash;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\Cash\CashBoxTransactionServices;
class CashBoxTransactionController extends Controller {
//
private $services;
public function __construct(CashBoxTransactionServices $cash_box_transaction_services) {
$this->services = $cash_box_transaction_services;
}
I have this error
ReflectionException: Class App\Services\Cash\CashBoxTransactionServices does not exist in file C:\xampp\htdocs\easyerp\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 826
I made sure of using the right namespaces and the right classes names!
PS: when I remove the inheretance from CashBoxTransactionServices class It works!

Call the UI function in controller class in laravel

UI Code: in resources\views\DistributorRegistration.php
<?php
class DistributorRegitrationForm
{
public function distributorRegitrationFormHtml(){
return '<h1>Hello</h1>';
}
}
?>
In Controler Class.....
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use resources\views\DistributorRegistration;
class DistributorRegistration extends Controller
{
function VestigePOS_GRNHandler(Request $request){
$id = $request->input('id');
return view(DistributorRegitrationForm::distributorRegitrationFormHtml()) ;
}
}
When I call this controller in routes
Fatal error: Class 'App\Http\Controllers\DistributorRegitrationForm' not found
Which file contains the class DistributorRegitrationForm? I'm missing an use App\...\DistributorRegitrationForm; in the controller, if it's not in the same namespace.
Calling DistributorRegitrationForm::distributorRegitrationFormHtml() won't work, unless the method becomes a static method (public static function).
You have some typos in there ;-)

Laravel 5 - Where to define functions and call them in views & controllers

I have following function and want to call it from view. Basically i want to put all common functions in one file. I am not sure where to create that file and how to call it inside controller and view.
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
public function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
So far i have created CommonController.php in app/Http/Controllers and put above function in it.
Then in other controller i have tried to call it following way:
use App\Http\Controllers\Common;
class SongsController extends Controller {
public function index($id)
{
echo Common::BytesToMB('7012187');
}
}
But i am getting error:
Class 'App\Http\Controllers\Common' not found
Ok, new try. You missed to use the complete class name and add the static keyword:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
public static function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
And then:
<?php
namespace App\Http\Controllers;
// You do not need to define this, if you are in the same namespace
use App\Http\Controllers\CommonController;
class SongsController extends Controller {
public function index($id)
{
echo CommonController::BytesToMB('7012187');
}
}
Another and more OOP solution is to use the function from the parent class:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class CommonController extends Controller {
protected function BytesToMB($bytes=0)
{
if(empty($bytes))
return 0;
$kb = ceil($bytes/1024);
$mb = ceil($kb/1024);
return $mb;
}
}
And then:
<?php
namespace App\Http\Controllers;
// You do not need to define this, if you are in the same namespace
use App\Http\Controllers\CommonController;
class SongsController extends CommonController {
public function index($id)
{
echo $this->bytesToMB('7012187');
}
}

symfony2 routing issues

I am getting the following error:
The autoloader expected class "Acme\HelloBundle\Controller\HelloController" to be defined in file "/var/www/Symfony/app/../src/Acme/HelloBundle/Controller/HelloController.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
The controller code I have is actually:
namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
any idea why this is?
<?php namespace Acme\HelloBundle\Controller;
....
Just add the "*LESS_THAN*"?php tag at the beginning. Try if works.
Your controller should extend Symfony\Bundle\FrameworkBundle\Controller\Controller
namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use namespace Acme\HelloBundle\Controller;
class HelloController extends Controller
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}

Categories