Answer the question
In order to leave comments, you need to log in
How to organize the project structure to separate user rights?
We only have installed slim3 (+doctrine).
we add: services are injected into the controllers, repositories are injected into the services.
correctly I imagine DI?
//... dependencies.php
$container[Services\PostService::class] = function ($c) {
$postRepo = $c->get('em')->getRepository('Entities\Post');
return new Services\PostService($postRepo);
};
$container[Controllers\PostController::class] = function ($c) {
$postService = $c->get('Services\PostService');
return new Controllers\PostController($postService);
};
//... routes.php
$app->get('/posts', 'Controllers\PostController:getAll');
//... PostController.php
namespace Controllers;
use Services\CourierService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class PostController extends Controller
{
protected $postService;
public function __construct(PostService $postService)
{
$this->postService = $postService;
}
public function getAll(ServerRequestInterface $request, ResponseInterface $response)
{
try {
$post = $this->postService->getAll();
return $this->respondWithData($response, $$post, 200);
} catch (\Exception $e) {
return $this->respondWithData($response, [], 400, $e->getMessage());
}
}
}
//...PostService.php
namespace Services;
use Entities\Post;
use Controllers\Controller;
use Repositories\PostRepository;
class PostService extends Service
{
protected $postRepo;
public function __construct(PostRepository $postRepo)
{
$this->postRepo = $postRepo;
}
public function getAll()
{
return $this->postRepo->findAll();
}
}
WriterPostService extends PostService
AdminPostService extends WriterPostService
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question