Class 'App\TestService' not found - Laravel - php

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

Related

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

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.

Laravel external class files

I have a file that i put in app\Classes\myVendor\dev_client_api.php. This file has a class in it:
class someClass{
//stuff
}
I want to use this class in a controller.
In my controller I have done the following:
namespace App\Classes\myVendor;
use dev_client_api;
class myController extends Controller
{
///stuff
public function processData(Request $request){
$client = new someClass($vars);
}
}
When i execute this page I get:
Class 'App\Classes\myVendor\Controller' not found
I have to admit I am not sure what exactly I am doing. Any help would be great.
I assume your Controllers are in Laravel's default App\Http\Controller directory.
namespace App\Classes\myVendor;
class someClass {
//stuff
}
namespace App\Http\Controllers;
use App\Classes\myVendor\someClass;
class myController extends Controller
{
///stuff
public function processData(Request $request){
$client = new someClass($vars);
}
}

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

How to call a model function inside the controller in Laravel 5

I have been facing a problem of not able to use the model inside the controller in the new laravel framework version 5. i created the model using the artisan command
"php artisan make:model Authentication" and it created the model successfully inside the app folder, after that i have created a small function test in it, and my model code looks like this.
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
echo "This is a test function";
}
}
Now i have no idea, that how shall i call the function test() of model to my controller , Any help would be appreciated, Thanks in advance.
A quick and dirty way to run that function and see the output would be to edit app\Http\routes.php and add:
use App\Authentication;
Route::get('authentication/test', function(){
$auth = new Authentication();
return $auth->test();
});
Then visit your site and go to this path: /authentication/test
The first argument to Route::get() sets the path and the second argument says what to do when that path is called.
If you wanted to take this further, I would recommend creating a controller and replacing that anonymous function with a reference to a method on the controller. In this case, you would change app\Http\Routes.php by instead adding:
Route::get('authentication/test', 'AuthenticationController#test');
And then use artisan to make a controller called AuthenticationController or create app\Http\Controllers\AuthenticationController.php and edit it like so:
<?php namespace App\Http\Controllers;
use App\Authentication;
class AuthenticationController extends Controller {
public function test()
{
$auth = new Authentication();
return $auth->test();
}
}
Again, you can see the results by going to /authentication/test on your Laravel site.
Use scope before method name
<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class Mainmenu extends Model
{
public function scopeLeftmenu() {
return DB::table('mainmenus')->where(['menu_type'=>'leftmenu', menu_publish'=>1])->orderBy('menu_sort', 'ASC')->get();
}
}
above code i tried to access certain purpose to call databse of left menu
than we can easy call it in Controller
<?php
Mainmenu::Leftmenu();
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function scopeTest(){
echo "This is a test function";
}
}
Just prefix test() with scope. This will become scopeTest().
Now you can call it from anywhere like Authentication::Test().
For me the fix was to set the function as static:
public static function test() {..}
And then call it in the controller directly:
Authentication::test()
You can call your model function in controller like
$value = Authentication::test();
var_dump($value);
simply you can make it static
public static function test(){
....
}
then you can call it like that
Authentication::test();
1) First, make sure your Model is inside a Models Folder
2) Then supposing you have a model called Property inside which you have a method called returnCountries.
public function returnCountries(){
$countries = Property::returnCountries();
}
of course, in your case, replace Property by the name of your Model, and returnCountries by the name if your function, which is Test
and in the Model you write that function requesting the countries
so in your Model, place a:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return $test = "This is a test function";
}
}
and this is what your Controller will be getting
You should create an object of the model in your controller function then you can model functions inside your controller as:
In Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return "This is a test function"; // you should return response of model function not echo on function calling.
}
}
In Controller:
namespace App\Http\Controllers;
class TestController extends Controller
{
// this variable is used to store authenticationModel object
protected $authenticationModel;
public function __construct(Request $request)
{
parent::__construct($request);
$this->authenticationModel= new \App\Authentication();
}
public function demo(){
echo $this->authenticationModel->test();
}
}
Output:
This is a test function

When exactly do you need to import in PHP?

When autoloading classes, the following runs without a problem:
<?php
namespace App\Resources;
class Home extends Controller {
public function index() {
echo 'home/index';
}
}
How does this work? I never imported the Controller class:
<?php
namespace App\Resources;
use App\Resources\Controller;
class Home extends Controller {
public function index() {
echo 'home/index';
}
}
If you use non-qualified class name (without the namespace), PHP assumes you mean the current namespace. The code above works because both Home and Controller are in the same namespace App\Resources.

Categories