SOLID Principles in PHP-Laravel
Mohammad Kamruzzaman
Servant Leader | Software Architect | E-Commerce | E-Ticketing | Health Tech | Delivery Tech | Team Building & Management | Director of Engineering @Shohoz
SOLID principles are a set of best practices for software design that help make code easier to maintain and scale. Here are some examples of how these principles can be applied in Laravel:
In followings, these are just a few examples of how SOLID principles can be applied in Laravel:
class UserController
{
? ? public function index()
? ? {
? ? ? ? // Responsible for handling incoming requests and returning responses
? ? ? ? $users = User::all();
? ? ? ? return view('users.index', ['users' => $users]);
? ? }
}
class User
{
? ? // Responsible for handling data access and manipulation
? ? public function all()
? ? {
? ? ? ? return self::query()->get();
? ? }
}
2. Open/Closed Principle
领英推荐
interface Logger
{
? ? public function log(string $message);
}
class FileLogger implements Logger
{
? ? public function log(string $message)
? ? {
? ? ? ? // Log to a file
? ? }
}
class DatabaseLogger implements Logger
{
? ? public function log(string $message)
? ? {
? ? ? ? // Log to a database
? ? }
}
class UserController
{
? ? protected $logger;
? ? public function __construct(Logger $logger)
? ? {
? ? ? ? $this->logger = $logger;
? ? }
? ? public function index()
? ? {
? ? ? ? $this->logger->log('Accessing user index');
? ? ? ? // ...
? ? }
}
3. Liskov Substitution Principle:
interface Repository
{
? ? public function find($id);
? ? public function save($data);
}
class UserRepository implements Repository
{
? ? public function find($id)
? ? {
? ? ? ? return User::find($id);
? ? }
? ? public function save($data)
? ? {
? ? ? ? $user = new User;
? ? ? ? $user->fill($data);
? ? ? ? $user->save();
? ? }
}
class UserController
{
? ? protected $repository;
? ? public function __construct(Repository $repository)
? ? {
? ? ? ? $this->repository = $repository;
? ? }
? ? public function store(Request $request)
? ? {
? ? ? ? $this->repository->save($request->all());
? ? ? ? // ...
? ? }
}
4. Interface Segregation Principle:
interface Job
{
? ? public function handle();
}
interface QueueableJob extends Job
{
? ? public function queue();
}
class SendEmailJob implements QueueableJob
{
? ? public function handle()
? ? {
? ? ? ? // Code to send an email
? ? }
? ? public function queue()
? ? {
? ? ? ? // Code to add the job to a queue
? ? }
}
class UserController
{
? ? public function sendEmail(Request $request)
? ? {
? ? ? ? dispatch(new SendEmailJob($request->all()));
? ? ? ? // ...
? ? }
}
5. Dependency Inversion Principle:
interface Mailer
{
? ? public function send($to, $subject, $message);
}
class SMTPMailer implements Mailer
{
? ? public function send($to, $subject, $message)
? ? {
? ? ? ? // Code to send an email using SMTP
? ? }
}
class UserController
{
? ? protected $mailer;
? ? public function __construct(Mailer $mailer)
? ? {
? ? ? ? $this->mailer = $mailer;
? ? }
? ? public function sendEmail(Request $request)
? ? {
? ? ? ? $to = $request->input('to');
? ? ? ? $subject = $request->input('subject');
? ? ? ? $message = $request->input('message');
? ? ? ? $this->mailer->send($to, $subject, $message);
? ? ? ? // ...
? ? }
}
?? Empowering Teams ?? Cultivating Joyful Workplaces ?? Nurturing Leaders
2 年Hredoy Sen?fyi!
?? Empowering Teams ?? Cultivating Joyful Workplaces ?? Nurturing Leaders
2 年Thank you bhaiya. I started coding in Laravel recently. Already fascinated. Got to explore the SOLID concept you described here. Btw, love to read your posts.
Marketer | Growth Strategist | Data Science Enthusiast
2 年Thanks for sharing. It's helpful to me. I'm going to start a small project with Laravel.