M
M
Muhammad2015-07-26 13:58:46
Laravel
Muhammad, 2015-07-26 13:58:46

Best practices when writing Laravel applications?

I recently started to learn Dependency Injection and Laravel repositories and immediately a few questions arose:
1. Where to store the repository files? I created a Repositories folder and added it to Composer. Did I do the right thing?
2. Do I need to split the folder with repositories into subfolders (UserRepository, MailerRepositiory, etc.)
3. Where is it better to bind interfaces to the implementation - in the bingings helper or in the service provider?
4. Where to store service providers? While stored in the Providers folder
Other:
5. The User model has a register method where validation takes place. Now, in case of an error, a custom InvalidDataException (or NotFoundException, depending on the situation) is thrown, which is in the folder with the models, and the controller is already processing:

try {
  $user = $this->users->register($data);
} catch (InvalidDataException $e) {
  return Redirect::back()->with('global', $e->getMessage())->withInput($data)->withErrors($e->getErrors());
}

Is it correct to do this or is it better to put it in a separate folder?
6. How to test sending E-mail (without various catchers, etc.)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-07-26
@muhammad_97

first, stop thinking in folders and start thinking in namespaces
1) wherever you want. A simple option is in the Repository namespace. In general, read about the repository pattern
2) What's the point? It makes sense to divide by supported storages (Database, InMemory, etc), only in this way we can have two implementations of UserRepository
3) in the service provider is more logical.
4) Keep it there, you can also rename it to ServiceProvider
5) Usually, the user registration is new User, or in the case of Laravel, a static factory ... or a service. In general, validation should also take place at the controller level, but that's how it goes.
6) without various catchers - swift mailer has a spool, but as for me it is not so convenient.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question