panthro
panthro

Reputation: 24061

Location of Models in a Package?

I wish to use models in my package. I've looked on laravel forums/blogs but can't find a definitive answer to this question.

Where should my models be placed and how should they be namespaced?

I know how to autoload my models etc but should the models go in:

panthro/package/src/models

and be namespaced:

panthro/package

OR be located at:

panthro/package/src/panthro/package/models

and be namespaced:

panthro/package/models

What is correct or is there another solution?

Upvotes: 0

Views: 142

Answers (1)

JofryHS
JofryHS

Reputation: 5874

Your models can reside anywhere, as long as they can be found. However to keep things more consistent, I would advise you to put them in panthro/package/src/ (or whatever the name of your package is), since src folder is almost the equivalent to app folder in your Laravel main project.

As for namespacing, I think it's fine just keep your namespacing as consistent as possible throughout your package, which usually starts with Author\PackageName for example:

namespace Panthro\Package;

Though I'd be much more specific than naming the package name as Package.

If you take a look at laravel's plugin libraries, you can learn how they do their namespacing and folder structure too.

Example:

APIRouter - Very simple library

Intervention (Laravel) - More comprehensive and bigger codebase but still easy to follow (structural-wise)

Depending on your package, if you have many types of models, it'll be wise to add another level of namespace for your models, otherwise you don't really have to.

Upvotes: 1

Related Questions