I am using the Repository pattern in my current project. When I try and access the route I get this error
ReflectionException
Class Repositories\UserRepository does not exist
My folder structure is like so
- Respositories
-- UserRepository
In my controller I am doing
use \Repositories\UserRepository;
class UsersController extends ApiController {
protected $user;
public function __construct(UserRepository $user)
{
$this->user = $user;
}
In the UserRepository
<?php namespace \Repositories
class UserRepository {
I am autoloading in composer
"psr-0": {
"Respositories": "app/"
}
Are my namespaces correct? I can not workout why it can't find the class.
1) Don't use prefixed back slashes when defining the namespace.
// No
<?php namespace \Repositories;
// Yes
<?php namespace Repositories;
2) Check your spelling. In your composer.json file, you have Respositories instead of Repositories.
maybe your laravel hasn't load your repository, if thus, just simply run this
command composer dumpautoload
if this occurred on your deployment server, have read this article regarding on this error http://www.tagipuru.xyz/2016/07/09/repository-class-does-not-exist-in-laravel/
Related
I always get the class not found exception in php laravel 6 when i create a class and extend a parent class named A which is located in the same directory.
However, another child class that is located in same directory could extend class A successfully.
In addition, i couldn't also instantiate the class A due to class not found exception in another .php file.
Please help me on this.
Thanks in an advance.
Parent class: myContext
<?php
namespace config\models;
class myContext {
public static $conn;
...
}
Class myUser: extension is fine.
<?php
namespace config\models;
class myUser extends myContext {
private $name;
...
}
Class oauth: extension returns myContext class not found.
<?php
namespace config\models;
class oauth extends myContext {
private $user;
}
Instantiate the class - returns class not found.
<?php
use config\models\myContext as context;
$cont = new context();
Check whether the namespace is added correctly when the parent class is imported.
Refer
https://www.quora.com/What-is-namespaces-and-use-in-Laravel
external classes and PHP files in Laravel - Class not found
Laravel - Model Class not found
Laravel 6: Class 'Form' not found
To get the provided example codes to work you need to use require_once
<?php
require_once('models/myContext.php');
use app\config\models\myContext as context;
$test = new context();
A search on SO brought me to this source
You try to add this line of code in the composer.json file, then execute the composer dumpautoload command on the command line
In composer.json file,
"autoload": {
"psr-4": {
"App\\": "app/",
"config\\models\\": "config/models"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
After that composer dump-autoload.
have you add autoload for config/ folder in composer.json. Laravel only default autoload for app/
I am new in laravel.
I am writing following code in the controller method.
include_once(app_path() .'/Classes/Pgp_2fa.php');
$pgp = new Pgp_2fa();
Following errors are shown.
FatalThrowableError in AuthController.php line 146: Class
'App\Http\Controllers\Pgp_2fa' not found
You have to use the correct namespace for your class.
If you are not using any namespace, you should add a \ in front of the class to let php know that the class exists in the root namespace. Otherwise php will look in the current namespace which is App\Http\Controllers when you are in a controller.
$pgp = new \Pgp_2fa();
This is not just Laravel behavior but php in general.
When you add new classes there are couple way to use them. (I mean, load them)
If your classes has unique name and don't use namespace, then you can add backslash \ start of the class.
$class = new \Pgp_2fa;
But if you want to define them namespaces and use with same name so
many times, then you have to add these classes in composer.json. Open your
composer.json and come into "autoload" section and define class
directory in classmap
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Classes", // so all classes in this directory are going to load
],
And don't forget to say composer dump-autoload after these changes and creating new classes
If you don't want to dump your composer when you add a new class,
then you may want add those classes inside of the psr-4 section in composer
"autoload": {
"psr-4": {
"App\\": "app/",
"App\\Classes\\": "app/Classes",
}
},
you need to add namespace for your custom class
or just add slash on your class name. like :
$pgp = new \Pgp_2fa();
You can call inside the constructor like below, and you can use the
$Pgp_2fa variable in remaining function
class controllername extends Controller
{
public function __construct(Route $route,Request $request)
{
$Pgp_2fa = new \Pgp_2fa();
}
}
You can easily add your custom classes by added it to Composer's autoload array. Go to composer.json and add it to:
"autoload": {
"files": [
"app\\Classes\\Pgp_2fa.php"
]
},
Now in your app folder, under classes folder add your Pgp_2fa.php file which will have your class. You can use it anywhere you want, it should be automatically auto-load-ed.
After adding your class write command:
composer dumpautoload to refresh autoload class mapping.
For example, There is our class Common.php in a directory called Mylibs in app directory.
app/mylibs/Common.php
we need to namespace App\Mylibs;
namespace App\Mylibs;
class Common {
public function getSite() {
return 'AmirHome.com';
}
}
Now, you can access this class using the namespace within your classes just like other Laravel classes.
use App\Mylibs\Common
in Controller:
namespace App\Http\Controllers;
use App\Mylibs\Common;
class TestController extends Controller {
protected $common;
public function __construct(Common $common) {
$this->common = $common;
}
public function index() {
echo $this->common->getSite();
}
}
I am new to repository in laravel and trying to work on it , But when i run the application it throws the error
Class 'App\Repositories\User\UserRepoServiceProvider' not found
My interface and repository files are located in App\Repositories\User where Service Provider is also located
Here is my service provider
namespace App\Repositories\User
use Illuminate\Support\ServiceProvider;
class UserRepoServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('App\Repositories\User\userinterface','App\Repositories \User\userrepository');
}
}
Here is my userrepository.php
namespace App\Repositories\User
use App\Repositories\User\userinterface;
use App\car;
class userrepository implements userinterface
{
public function __construct(car $car){
$this->car = $car;
}
public function get($id)
{
return $this->car->findCar($id);
}
public function delete($id)
{
return $this->car->deleteCar($id);
}
}
Here is my interface userinterface.php
namespace App\Repositories\User;
interface userinterface{
public function get($id);
public function delete($id);
}
I have also registered it in config/app.php file
App\Repositories\User\UserRepoServiceProvider::class
I did composer dump-autoload -o but no use. I cannot do composer update , when i do it throws the same error
I usually check in bootstrap/cache/config.php for the package, sometimes after composer remove nameofthepackage, the package is still listed there
You have to register the service provider in config/app.php.
https://laravel.com/docs/5.3/providers#registering-providers
I'm facing this problem Class 'App\Respositories\BackendServiceProvider' not found
It was due to spelling mistake in config/app.php file. I changed Respositories to Repositories and open this BackendServiceProvider and changed namespace also. so problem has been solved.
Respositories to Repositories
namespace App\Respositories to namespace App\Repositories;
Working for me. You can try it!
Remove "laravel/ui" reference and its provider from packges.php file.
I know that this question was asked so many times, but none of answers helped me.
I'm getting exception in Laravel 5
BindingResolutionException in Container.php line 785:
Target [App\Contracts\CustomModelInterface] is not instantiable.
What I've done without success:
Register App\Providers\AppRepositoryProvider in app.php providers
php artisan clear-compiled
Everything works if I replace interfaces on repositories in MyService, but I feel that it's wrong (should it be handled by IoC container?).
Structure:
app
- Contracts
- CustomModelInterface.php
- Models
- CustomModel.php
- Repositories
- CustomModelRepository.php
- Providers
- AppRepositoryProvider.php
- Services
- MyService.php
App\Contracts\CustomModelInterface.php
<?php namespace App\Contracts;
interface CustomModelInterface {
public function get();
}
App\Repositories\CustomModelRepository.php
<?php namespace App\Repositories;
use App\Contracts\CustomModelInterface;
use App\Models\CustomModel;
class CustomModelRepository implements CustomModelInterface {
private $Model;
public function __construct(CustomModel $model) {
$this->Model = $model;
}
public function get() {
return 'result';
}
}
App\Services\MyService.php (Keep business logic / layer between controller and repositories)
<?php namespace App\Services;
use App\Contracts\CustomModelInterface;
class MyService {
private $Model;
public function __construct(CustomModelInterface $customModel) {
$this->Model= $customModel;
}
public function getAll() {
return $this->Model->get();
}
}
App\Providers\AppRepositoryProvider.php
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppRepositoryProvider extends ServiceProvider {
public function boot() {}
public function register() {
$models = array(
'CustomModel'
);
foreach ($models as $idx => $model) {
$this->app->bind("App\Contracts\{$model}Interface", "App\Repositories\{$model}Repository");
}
}
}
My controller looks like:
<?php namespace App\Http\Controllers;
use App\Services\MyService;
class SuperController extends Controller {
private $My;
public function __construct(MyService $myService) {
$this->My = $myService;
}
public function getDetails() {
return $this->My->getAll();
}
}
composer.json
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"App\\Models\\": "app/Models/",
"App\\Contracts\\": "app/Contracts/",
"App\\Repositories\\": "app/Repositories/"
}
},
Thank you everyone, but problem was in my AppRepositoryProvider. As it's binding exception, then obviously the problem was with binding :)
Correct file is:
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppRepositoryProvider extends ServiceProvider {
public function boot() {}
public function register() {
$models = array(
'CustomModel',
'CustomModel2',
'CustomModel3'
);
foreach ($models as $model) {
$this->app->bind("App\Contracts\\{$model}Interface", "App\Repositories\\{$model}Repository");
}
}
}
Note, that I'm using "App\Contracts\\{$model}Interface" (not escaping "{" symbol) and it generate correct string App\Contracts\CustomModelInterface instead of App\Contracts\{$model}Interface (with unexpected escaping).
Every time I create a new repository/contract pair I make sure I do the following:
check the classes used in the service provider (copy/paste the namespaces)
register a new binding in config/app.php
php artisan optimize
Many hours of useless debugging led me to this short checklist.
For me, I forgot to bind in app->providers->RepositoryServiceProvider
the repository like this in the register method
public function register()
{
$this->app->bind(
\App\Play\Contracts\PatientRepository::class,
\App\Play\Modules\PatientModule::class
);
}
Make sure your RepositoryServiceProvider is registered in AppServiceProvider.
public function register()
{
$this->app->register(RepositoryServiceProvider::class);
}
I got past this error running:
php artisan config:clear
php artisan clear-compiled
php artisan optimize
php artisan config:cache
Related to:
Target is not instantiable. Laravel 5 - App binding service provider
The problem is solved by adding your repository in app/providers/AppServiceProvider
like the example below.
public function register()
{
$this->app->singleton(UserRepository::class, EloquentUser::class);
}
Dont forget the name space
use Test\Repositories\EloquentUser;
use Test\Repositories\UserRepository;
It worked for me
On App\Services\MyService.php you are passing that interface with dependency injection which tries to instantiate that -
public function __construct(CustomModelInterface $customModel) {
$this->Model= $customModel;
}
which is wrong.
Try implement that in that class - class MyService implements CustomModelInterface { and use the function of that interface like -
$this->get();
Or you are using it - class CustomModelRepository implements CustomModelInterface {
So if you do -
public function __construct(CustomModelRepository $customModel) {
$this->Model= $customModel;
}
then also you can access the interface methods.
I've just experienced an issue similar to this and the cause of my error was that I had set $defer to true in the service provider class but I had not implemented the required provides() method.
If you have deferred the creation of your class until it is need rather than it being loaded eagerly, then you need to also implement the provides method which should simply return an array of the classes that the provider provides. In the case of an interface, I believe it should be the name of the interface rather than the concrete class.
E.g.
public method provides(): array
{
return [
MyInterface::class,
];
}
Current documentation: https://laravel.com/docs/5.5/providers#deferred-providers
I hope this helps somebody else.
Don't worry guys. I have a solution to your problem.
I have an example for you.
Step1: php artisan make:repository Repository/Post //By adding this command you can create a repository and eloquent files
Step2: After adding that file you have to add/use this repository in the controller in which you want to use.
for eg: use App\Repositories\Contracts\PostRepository;
Step3: After adding that repo in your controller if you will run the app you will get an error like " Interface is not instantiable". It comes because you have created a repo and used in a controller, but laravel don't know where this repository is register and bind with which eloquent. So that it throws an error.
Step4: To solve this error you have to bind your repo with your eloquent in AppServiceProvider.
E.g:
AppServiceProvider.php file
<?php
namespace App\Providers;
// **Make sure that your repo file path and eloquent path must be correct.**
use App\Repositories\Contracts\PostRepository; // **Use your repository here**
use App\Repositories\Eloquent\EloquentPostRepository; **// Use your eloquent here**
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider {
/**
* Register any application services.
*
* #return void
*/
public function register() {
**// And bind your repository and eloquent here. **
$this->app->bind(PostRepository::class, EloquentPostRepository::class);
}
}
Step5: After binding repo and eloquent you can use all method of repo in your controller. Enjoy.....
Please let me know if you have any query.
execute this command :
composer dump-autoload
this command will remap your laravel autoload classes together with all other vendor's i had same issue before and this did the trick you can use it together with "-o" param for optimization .
Note that this can also be caused by the _constructor on the class being declared private, or otherwise being blocked...
If it cant call the constructor, the binding will fail
I think the problem here is that you don't bind App\Contracts\CustomModelInterface to anything so Laravel tries to create instance of interface.
In App\Providers\AppRepositoryProvider.php you have only:
$models = array(
'Model'
);
but you should have in this array CustomModel also, so it should look like this:
$models = array(
'Model',
'CustomModel',
);
The last thing you do is to use the interface you bound to the repository.
Set it up and try running your laravel app to make sure you get no errors.
In my case I had a mismatch between my repository and interface.
interface UserRepositoryInterface{
public function get($userId);
}
class UserRepository implements UserRepositoryInterface{
public function get(int $userId);
}
As you can see the interface get method does not include a type hint but the UserRepository class' get method has a type hint.
You won't get this error if you immediately start to use your Interface Binding.
register a new binding in config/app.php
In my case I forgot use App\Repositories\UserRepository in App\Providers\AppRepositoryProvider.php
intelephense wasn't complaining and the error-message did not give me any clue, but somehow I found out that it's missing and adding this line did the trick
I had this error, and found out that I should restart the queue because it runs in the job:
php artisan queue:restart
Just started working through laracasts and trying to move on from direct eloquent use in the controllers.
I have implemented everything that I need to but hitting this error:
Class tva\Repositories\VehicleRepositoryInterface does not exist
My folder structure is:
app/
tva/
repositories/
VehiclesController:
use tva\Repositories\VehicleRepositoryInterface;
class VehiclesController extends \BaseController {
protected $vehicle;
public function __construct(VehicleRepositoryInterface $vehicle)
{
$this->vehicle = $vehicle;
}
}
In the repositories folder:
VehicleRepository:
namespace tva\Repositories;
class VehicleRepository implements VehicleRepositoryInterface {
}
VehicleRepositoryInterface:
namespace tva\Repositories;
interface VehicleRepositoryInterface {
}
And also updated my composer.json:
"psr-0": {
"tva": "app/"
},
To me, this should work?
Issue solved, instead of using psr-0 I added the directory to the classmap and all issues solved.