Asked by: StudentX
Alright, so the Laravel is behaving strangely for me for the past few days.
I am developing a Laravel Package and have the following directory structure:
vendor
-student
--myPackage
---src
----controllers
----models
----mail
----routes
----views
Example of one of my model file:
<?php
namespace Student\myPackage\Model;
use Illuminate\Database\Eloquent\Model as BaseModel;
class MyClass extends BaseModel
{
//
}
I am trying to access the model file in my controller, like this use Student\myPackage\Model\MyClass as aMyClass
. I have several models in the models directory and every model is under the same namespace, which is Student\myPackage\Model
The problem is I am getting the following error, when visiting the route.
FatalThrowableError
Class 'Student\myPackage\Model\MyClass' not found
I was having this error on only one model class, and that too when it is referenced in a controller, if I reference it in the route file, like below, then there is no problem.
use Student\myPackage\Model\MyClass as aMyClass
Route::get('/test', function() {
$g = new aMyClass; ($g);
})->middleware('web');
I even wiping the model file out then re-coded everything, yet the problem persisted. Then in my controller I re-wrote the use statement for all the model classes that were being used and it worked. Remember I even re-wrote the use statement of the said class many times but it just didn't work. Anyways, so now it is working, but now I have the same issue with an another class, a mailable class that is.
What could be the problem, is it about the order in which the classes are referenced or something about namespaces I am missing ?
Composer.json of the package
"autoload": {
"psr-0": {
"Student\\myPackage\\": "src/"
}
}
Answers
Answered by: OuailB at 2017-07-11 06:15PM
Your forgot to pluralize your models namespace.
Try with this use
statement :
use Student\myPackage\Models\MyClass as aMyClass;
Your models are in src/models
, composer will namespace your models as Student\myPackage\Models
Answered by: Chandraarnav at 2017-07-11 06:56PM
Your namespacing is not correct
Student\myPackage\Model\MyClass
Change it to
Student\myPackage\Models\MyClass
After you have checked this, register your service provider (aliases if any). Using aliases will help you in saving time and effort.
No comments:
Post a Comment