MVC PHP - Libraries - Directory Structure

Asked

Viewed 757 times

1

I have a question regarding the folder structure of the application.

Next, doubt arises when there is a need to write a class that integrates with a API Which part would this class be allocated to? Because I believe that this class would not have behavior of a component (it does not have any kind of business logic) but maybe of a lib (Library) which would be an interface to consume API resources.

Imagine the following structure:

/app/
 - controllers/
 - components/
 - models/
 - vendors/

But the question is, if API is a package written by me, she shouldn’t go to vendors (since vendors are only for third-party packages), am I right? Or maybe it would be ideal to have a new directory librarys/ for internal application packages?

2 answers

1


In cases like this the organization is made as follows: /vendors/Company/Library/Company/PackageName. If your code is generic and can be reused in other projects without much need for adaptation, that is, as you said yourself, if you have no business rule, its place is at vendors.

  • Thank you for answering @Hamboldt, I understand, so I can package all classes that do not have logic with the application but are used by the same, in a package of its own within vendors?

  • Exactly. When we are going to make a software we have to think about how we are going to make it so that it has the greatest possibility of having reusable parts. So if we can separate reusable things from specific business rule codes it means that we’re going to have less and less work in the future, being able to reuse our past codes without much effort. It is important to keep in mind also that we should organize so that we can assume where each thing is for example: * /library/Outlook/Email/Author.php * /library/Outlook/SMTP/Request.php * /library/Outlook/SMTP/Response...

1

My answer goes in the sense of my experience in MVC and specifically in PHP as put in the question and as regards its structure.

Without going into basic concepts, the experience has led me over the years to adopt a different structure than the one it presents however I know well that this depends a lot of case by case. For a project that also implements a joint api:

/projecto/
   |--api/
   |    |--resources/
   |    |index.php
   |    |.htaccess
   |
   |--application/
   |    |--config/
   |    |--controllers/
   |    |--models/
   |    |--libraries/
   |    |--views/
   |    |--(etc... dentro da lógica)
   |    |.htaccess (deny from all)
   | 
   |--public/
   |--vendor/
   |
   |index.php
   |.htaccess

As you can see models stays inside the application and had not been his components I think it will be equivalent to libraries and as to the vendors the rule is to call it vendor in the singular case working with Composer... it is more useful.

The .htaccess is placed in each directory that is at the root of the project and therein enters my direct answer to your question about the API. For an additional service to the project I normally include the directory API na raíz and .htaccess, this way defends the resources present there by way of a friendly url.

Remember that an MVC separates the layout of the PHP code and as regards the API should separate the implementation of resources from the logic of the models that for reasons of maintenance of the code should use the implementations in application/models, in this case :)

  • Thank you for replying @chambelix, but in this case I would not be generating redundancy since my intention is not actually to provide a API but the 'class' of API in fact will be seeking information from a third party API. I am correct, that would not be a good?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.