Hello I want to know if there is a special naming convention for laravel models and controllers files, I created a model post.php and when I tried to use this model I got a fatal error said the class post is not found but when I changed the file name to be Post.php instead of (post.php) it works fine and the exception is gone anyone could explain why this happened?
Thanks.
Asked
Active
Viewed 1,460 times
3

Ahmed AbdElNasser
- 79
- 1
- 10
-
Possible duplicate of [Laravel 5 Naming Conventions](https://stackoverflow.com/questions/41655535/laravel-5-naming-conventions) – Andy Holmes Feb 05 '19 at 11:08
-
I would recommend using camelcase for the controller and in php class name should start with a capital letter so is with model and controller as they also define a class – Adesh Kumar Feb 05 '19 at 11:08
-
@AdeshKumar Thanks for your response but I figured out something the filename must be the same as the class name to avoid getting this exception, briefly If you named your file model post.php you must define your class like this class post and if you named your file Post.php you must define your class like this class Post – Ahmed AbdElNasser Feb 05 '19 at 11:21
-
you should use artisan commands to make controller and models to avoid such things – Adesh Kumar Feb 05 '19 at 11:42
1 Answers
9
What you're describing sounds less like a naming convention issue and more of a PSR-4 compliance issue.
Specifically, point 2.6:
All class names MUST be referenced in a case-sensitive fashion.
So for example, since you named the file post.php
then the file must contain a class post
and be referenced as new post()
. This will ensure the composer PSR-4 autoloader will pick it up.
If you, however, have a file called Post.php
then it must contain a class Post
and be referenced as new Post()
.

apokryfos
- 38,771
- 9
- 70
- 114