I personally like to put out most of my business logic (services, models, etc.) into its own namespace. I'll then inject that logic into the controllers, and use it as needed throughout. That keeps my controllers really simple, able to properly handle exceptions, and easier to understand.
Folder structure is something like this.
- app
- controllers
- Acme
- FooService.php
- (other folders)
Then the controllers look something like this.
<?php
use Acme\FooService;
class HomeController extends BaseController
{
protected $fooService; // This handles services specific to foos.
public function __construct(FooService $fooService)
{
parent::__construct();
$this->fooService = $fooService; // Now we can use the fooService's logic.
}
public function index()
{
$foobars = $this->fooService->getActiveFoobars();
return View::make('foobars.index', compact('foobars'));
}
// ...rest of controller.
Let me know if I haven't explained the ideas behind this enough! Again, this is normally how I separate out my business logic in a reusable way. There are plenty of other ways to do what you're looking for, so I'm sure you can keep looking around to find a way that will work best for you.
You have to define the responsibility of the controller..For me, i only use controller to send and receive response to the client.
I separate everything to different layers.
Thanks guys. You gave me some very good leads.
You have to define the responsibility of the controller..For me, i only use controller to send and receive response to the client.
I separate everything to different layers.
- Service
- Repository - database transaction logics
- Validators - validation logic
- Entity - data structure
My Folder Structure
- Folder/
- Folder/Service
- Folder/Repository
- Folder/Validators
- Folder/Entity
@johndavedecano I am also a newbie and I am very interested in your folder structure. problem is, I have read so many theories on architecture that are have similar structures to what you mention but don't provide a real implementation example. Please provide me with an example how to tie all this together. It will be of great help. Also is your structure based on any particular architecture? Thank you.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community