When you generate code using Sketch, it creates a foundation for implementing the service repository pattern with empty interfaces that you can extend based on your specific needs.
The BaseRepository class serves as the foundation for all repository implementations. It provides a set of common database operations that your specific repository implementations can inherit. This includes methods for finding, creating, updating, and deleting records using both ID and UUID.
Repository Components
The repository layer consists of two main components:
Repository Interface
Repository Implementation
Service Components
The service layer also consists of two main components:
Service Interface
Service Implementation
Extending the Pattern
After generation, you can extend these classes by:
Adding methods to your repository interface based on your data access needs
Implementing those methods in your repository implementation class
Adding business logic methods to your service interface
Implementing the business logic in your service implementation class
For example, you might extend them like this:
This structure provides a clean foundation that you can build upon while maintaining separation of concerns and following SOLID principles.
<?php
namespace App\Repositories\Post;
interface PostRepository
{
// Define your repository methods here
}
<?php
namespace App\Repositories\Post;
use App\Repositories\BaseRepository;
use App\Models\Post;
class PostRepositoryImp extends BaseRepository implements PostRepository
{
public function __construct(Post $model)
{
parent::__construct($model);
}
// Implement your repository methods here
}
<?php
namespace App\Services\Post;
interface PostService
{
// Define your service methods here
}
<?php
namespace App\Services\Post;
class PostServiceImp implements PostService
{
// Implement your service methods here
}
// PostRepository.php
interface PostRepository
{
public function findPublishedPosts();
public function findByCategory($categoryId);
}
// PostService.php
interface PostService
{
public function getPublishedPosts();
public function getPostsByCategory($categoryId);
}