<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kareem Khaled</title>
    <description>The latest articles on DEV Community by Kareem Khaled (@kareem-khaled).</description>
    <link>https://dev.to/kareem-khaled</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1252508%2Fa1a65d95-a0ba-44d9-b56a-2e526a044390.jpeg</url>
      <title>DEV Community: Kareem Khaled</title>
      <link>https://dev.to/kareem-khaled</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kareem-khaled"/>
    <language>en</language>
    <item>
      <title>Design Patterns in Laravel: Level Up Your Code with Reusable Solutions</title>
      <dc:creator>Kareem Khaled</dc:creator>
      <pubDate>Tue, 02 Jul 2024 10:52:25 +0000</pubDate>
      <link>https://dev.to/kareem-khaled/design-patterns-in-laravel-level-up-your-code-with-reusable-solutions-3pfp</link>
      <guid>https://dev.to/kareem-khaled/design-patterns-in-laravel-level-up-your-code-with-reusable-solutions-3pfp</guid>
      <description>&lt;p&gt;If you're building applications with Laravel, chances are you might have come across people saying, "Design patterns are the way to go." But &lt;strong&gt;what, really, are design patterns&lt;/strong&gt;? And more importantly, &lt;strong&gt;how can they make your Laravel applications better&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;Through this article, let us go through an exampleapatkan with design patterns, in showing just a re-usable solution in helping you go about writing cleaner, more maintainable, and scalable code. We're going to go through practical examples and real-world scenarios so that you can see how the power of patterns works.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Design Patterns, Anyway?
&lt;/h2&gt;

&lt;p&gt;You could safely define design patterns as blueprints or templates for resolving common problems in programming. Being language-agnostic, they are more about battle-tested strategies experienced developers use and reuse to create reliable software.&lt;/p&gt;

&lt;p&gt;Design patterns are embedded in Laravel as a framework, although you can also use them explicitly in your own code. This will bring about the following benefits:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Improved Code Readability&lt;/strong&gt;: Patterns convey, in a single glance, the intent of the code, even for other developers who might not have known your project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Maintainability&lt;/strong&gt;: Changes in code and debugging aren't as painful when the code is structured.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability&lt;/strong&gt;: Patterns will help you design code that grows and adapts to the evolution of your application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Faster Development:&lt;/strong&gt; Reusable patterns will save you time and effort, and you won't have to reinvent the wheel over for each problem.
&lt;strong&gt;Essential Design Patterns for Laravel Developers&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's go through a few of the key patterns most appropriate for Laravel applications:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Repository Pattern&lt;/strong&gt;: This pattern acts as an intermediary between the logic level of your application and the data layer. This gives you a clean interface to the databases, or other data sources, making your code flexible and easier to test.&lt;br&gt;
A good example would be if you were building a blog. In place of having your controllers working directly against Eloquent models, you would abstract that out into something like a &lt;code&gt;PostRepository&lt;/code&gt; to abstract away database operations that dealt with posts, such as creation, fetch, save, delete, and so on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Factory Pattern:&lt;/strong&gt; Factories make the process of getting an object easy, especially when those objects require setup or dependencies that are complex.&lt;br&gt;
One of the nice features of Laravel is that factories can be used to generate test data for your models. This can enable you to write very realistic tests without having to create the data by hand every time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Decorator Pattern: Enables the addition of new behaviors to existing objects dynamically without changing their structure.&lt;br&gt;
One could use this in order to realize caching for some service class. In this way, results of some expensive operations can be cached without touching the core logic of the service.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Observer Pattern:&lt;/strong&gt; The Observer pattern is used to perform events generated by an object to a host of other objects.&lt;br&gt;
One fine example of the Observer pattern is Laravel's event system. You would then be able to create event listeners that respond to some events with other actions. For example, an event that somebody has just become a new user of your website will send a notification or other similar event responses.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Real-World Scenario: Building a Modular E-commerce Platform
&lt;/h2&gt;

&lt;p&gt;Suppose you are asked to build an enormous e-commerce platform. The following is how you could use design patterns to meet this challenge:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Repository Pattern&lt;/strong&gt;: Handle the data of the products, orders, customers through repositories. It will keep business logic independent of the implementation of a database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Factory Pattern&lt;/strong&gt;: Creation of factories to create a realistic set of test data for your models—products, orders, users—to check everything works as it should.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decorator Pattern&lt;/strong&gt;: Implement caching on your product catalog to achieve better performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observer Pattern&lt;/strong&gt;: Use events for actions like sending order confirmation e-mails or updating inventory levels by Result.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Your Turn: Share Your Pattern Prowess
&lt;/h2&gt;

&lt;p&gt;Which design patterns have you applied in your Laravel applications? Did you find any creative ways to use them? Please share your experiences and insights in the comments below! Let's learn from each other and build even better Laravel applications together.&lt;/p&gt;

&lt;h2&gt;
  
  
  Need a Design Pattern Guru?
&lt;/h2&gt;

&lt;p&gt;If you have any design problems that are bugging you, or if you just want a more in-depth discussion about patterns, please don't hesitate to drop me an email at &lt;a href="mailto:kareem_khaled@t-horizons.com/"&gt;kareem_khaled@t-horizons.com&lt;/a&gt;. Let's chat!&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
    </item>
    <item>
      <title>PHP in the Age of AI: Don't Overlook This Web Powerhouse for Your Next Smart Project</title>
      <dc:creator>Kareem Khaled</dc:creator>
      <pubDate>Sun, 30 Jun 2024 05:35:16 +0000</pubDate>
      <link>https://dev.to/kareem-khaled/php-in-the-age-of-ai-dont-overlook-this-web-powerhouse-for-your-next-smart-project-1hie</link>
      <guid>https://dev.to/kareem-khaled/php-in-the-age-of-ai-dont-overlook-this-web-powerhouse-for-your-next-smart-project-1hie</guid>
      <description>&lt;p&gt;Thereafter, PHP might very well prove to be the unsung hero of the AI revolution. While Python gets huge exposure, PHP's varied strengths make it compelling for many AI-fueled web applications. But let me make something clear: This is not about declaring PHP as "the best" for AI. It's a reminder, especially to software engineers: Compare and contrast choices based on the merits of each project, not dismiss PHP when it involves tapping its potential in the AI space.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why PHP &amp;amp; AI? A Practical Perspective
&lt;/h2&gt;

&lt;p&gt;This is the real deal. Here's why you should just integrate artificial intelligence right into your current PHP stack. It's potentially going to be huge for the following reason.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Leverage Your Existing Expertise&lt;/strong&gt;: If your team is already fluent in PHP, adding AI features doesn't mean you'll need to start from scratch. You can leverage your current experience and codebase to save hours and resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Seamless Integration&lt;/strong&gt;: PHP is at the core of web development; it will, hence, easily integrate with your web servers, databases, and front-end technologies. This makes it easier to create a unified end-to-end AI-powered solution.&lt;/p&gt;

&lt;p&gt;Performance &amp;amp; Scalability: While Python is a preeminent language for research, putting forward a great prototyping, PHP turns out more appropriate in production—especially where performance and scalability are concerns. The mature ecosystem of PHP provides tools and libraries to be leveraged for high-traffic web applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Case Study: PHP-Powered AI in Production
&lt;/h2&gt;

&lt;p&gt;Consider a large e-commerce website, in the region of millions of users or products. Suppose the company wants to implement a product recommendation system to boost their sales. Here is why they opted for PHP over Python: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Already Existing Infrastructure&lt;/strong&gt;: The website was written already in PHP and the development team was deep in terms of knowledge regarding the language, thereby mandating an expensive and time-consuming port to Python.&lt;br&gt;
Performance Requirements: The recommendations engine was to scale a huge amount of data in real-time. PHP, combined with optimized algorithms and caching strategies, could be tuned for the required performance at scale.&lt;br&gt;
Integration with Other Systems: The recommendation engine should be seamlessly implemented into the existing website systems, which were PHP-based catalog, shopping cart, and user profile. This was way easier with a PHP-based solution.&lt;/p&gt;

&lt;p&gt;The team trained a machine learning model using the Rubix ML library, which then passively analyzed user behavior and product data to make personalized product recommendations for inclusion in the PHP codebase. Long story short—the result was a huge increase in sales and customer engagement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing the Right Tool for the Job
&lt;/h2&gt;

&lt;p&gt;This case study demonstrates one very important thing: &lt;strong&gt;there is no single answer when developing artificial intelligence&lt;/strong&gt;. Although Python has an exceptionally high support in research and data science, only now, with PHP, would it be possible to prove that it is indeed an equally powerful choice in AI's integration into web applications.&lt;/p&gt;

&lt;p&gt;Consider these factors in the choice of language for your AI project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inherited code and competence of your team—build on top of what you already have.
Performance and scalability—pick a language that will hold the weight of your workload.
System Integration—Your AI pieces should communicate seamlessly with other parts of your application.
Easy Deployment and Maintenance—Consider the long-term costs and complexities.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Your AI Journey with PHP Starts Here
&lt;/h3&gt;

&lt;p&gt;Now, it becomes the duty of every software engineer to embrace the best tools for every job. There is nothing wrong with experimenting and looking into the possibilities with AI using PHP. After all, you never know what you might achieve.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Keep the Conversation Going!
&lt;/h2&gt;

&lt;p&gt;I look forward to getting your feedback on this. Have you ever tried using PHP for AI projects? What are your experiences and insights? Share your stories in the comments below or connect with me on &lt;a href="mailto:kareem_khaled@t-horizons.com/"&gt;kareem_khaled@t-horizons.com&lt;/a&gt;. Let us build a more illuminated, intelligent future together with PHP and AI!&lt;/p&gt;

</description>
      <category>ai</category>
      <category>php</category>
      <category>webdev</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Preventing IDM: A Tactical Guide to Protecting Your Video Content on Website</title>
      <dc:creator>Kareem Khaled</dc:creator>
      <pubDate>Mon, 17 Jun 2024 18:35:06 +0000</pubDate>
      <link>https://dev.to/kareem-khaled/blocking-idm-downloads-a-tactical-guide-to-protecting-your-video-content-on-website-3ilo</link>
      <guid>https://dev.to/kareem-khaled/blocking-idm-downloads-a-tactical-guide-to-protecting-your-video-content-on-website-3ilo</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Ever put your heart and soul into making a stunning video only to see it being ripped and shared all over the internet without your permission? If you nodding, then most probably IDM is familiar to you. This powerhouse of a tool might be deemed a godsend for users in a zeal of downloading videos, but surely a bane upon creators like us enthralled with the prospects of protection from cyber theft of our treasured content.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now, here's the challenge:
&lt;/h2&gt;

&lt;p&gt;Any website has no direct access to your system's processes, so we can't simply ask, "Hey, is IDM running?" But, fellow creators, fear not! We're about to outsmart IDM a little with some clever code and tactical maneuvering.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tell-tale Sign of IDM: The Video Tag
&lt;/h2&gt;

&lt;p&gt;IDM isn't subtle, like most download managers, in targeting a video. It leaves a fingerprint in the form of a custom attribute, "&lt;strong&gt;&lt;strong&gt;idm_id&lt;/strong&gt;&lt;/strong&gt;", on the video tag.  This is how IDM keeps track of which videos to snatch.  But that very attribute is its Achilles' heel, and we're going to exploit it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function checkForIDM() {
    const videos = document.getElementsByTagName('video');
    for (const video of videos) {
        if (video.hasAttribute('__idm_id__')) {
            return true; // IDM is likely running
        }
    }
    return false; // IDM not detected (yet)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  One of our Lines of Defense: The IDM Detector
&lt;/h3&gt;

&lt;p&gt;This JavaScript snippet will check your page for the presence of the "&lt;strong&gt;&lt;strong&gt;idm_id&lt;/strong&gt;&lt;/strong&gt;" attribute once every second. If it is found, we know IDM is on the prowl.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let idmCheckInterval;

function startIDMCheck() {
    idmCheckInterval = setInterval(() =&amp;gt; {
        if (checkForIDM()) {
            clearInterval(idmCheckInterval);
            blockContent(); // (see next snippet)
        }
    }, 1000); // Check every second
}

startIDMCheck(); // Begin monitoring immediately
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Block and Redirect: When IDM Strikes
&lt;/h3&gt;

&lt;p&gt;If IDM is detected, we need to act fast.  Here's how we'll block the content and give the user clear instructions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function blockContent() {
    const content = document.body.innerHTML;
    document.body.innerHTML = `
        &amp;lt;h1&amp;gt;IDM Detected&amp;lt;/h1&amp;gt;
        &amp;lt;p&amp;gt;Please disable IDM and reload the page to view this content.&amp;lt;/p&amp;gt;
        &amp;lt;button onclick="location.reload()"&amp;gt;Reload&amp;lt;/button&amp;gt;
    `;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Bait-and-Switch: Protecting Your Video URL
&lt;/h3&gt;

&lt;p&gt;But we're not done yet. Though we block IDM, it might have taken your video URL. To counter this, we're going to do a little trickery.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the page loads, embed your video with a fake URL (like some very short, silent video).&lt;/li&gt;
&lt;li&gt;After 5 seconds, start a timer.&lt;/li&gt;
&lt;li&gt;If there is no IDM activity within those 5 seconds, we know we are in the clear.&lt;/li&gt;
&lt;li&gt;Replace this with your actual video source.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const realVideoSrc = "your-actual-video.mp4";
const fakeVideoSrc = "dummy-video.mp4";

function swapVideoSrc() {
    const video = document.querySelector('video');
    video.src = realVideoSrc;
}

setTimeout(() =&amp;gt; {
    if (!checkForIDM()) {
        swapVideoSrc();
    }
}, 5000); // Replace after 5 seconds if safe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Don't Forget..
&lt;/h3&gt;

&lt;p&gt;This is a good base but this fight against downloaders never gets really won. You may need to tune these techniques from time to time when new tools are invented .&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Chat!
&lt;/h2&gt;

&lt;p&gt;I'd love to hear your thoughts on this approach. Have you faced similar challenges? Do you have other tricks up your sleeve? Feel free to drop a comment below or reach out to me via email at &lt;a href="https://mailto:kareem_khaled@t-horizons.com/" rel="noopener noreferrer"&gt;karemkhaled945@gmail.com&lt;/a&gt; .&lt;br&gt;
Let's geek out about video protection together!&lt;/p&gt;

</description>
      <category>security</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>GraphQL vs. REST: A Dev's Guide to Picking Your API Poison (and Why You Should Argue With Me)</title>
      <dc:creator>Kareem Khaled</dc:creator>
      <pubDate>Mon, 17 Jun 2024 02:53:00 +0000</pubDate>
      <link>https://dev.to/kareem-khaled/graphql-vs-rest-a-devs-guide-to-picking-your-api-poison-and-why-you-should-argue-with-me-55cd</link>
      <guid>https://dev.to/kareem-khaled/graphql-vs-rest-a-devs-guide-to-picking-your-api-poison-and-why-you-should-argue-with-me-55cd</guid>
      <description>&lt;p&gt;If you're building anything web-related these days, chances are you've got a nagging question at the back of your mind: REST or GraphQL? It certainly feels like the old, trusty workhorse pitted against the shiny new toy in this battle. Which one will be right for your project?&lt;/p&gt;

&lt;p&gt;Let's hash this out.&lt;/p&gt;

&lt;h2&gt;
  
  
  REST: Good Old Days
&lt;/h2&gt;

&lt;p&gt;It is no wonder that REST has been here for a long time; it is very easy and liberal. This method is so easy to understand and start working on. REST operates with the client-server model and uses the following conventional HTTP methods to execute the CRUD: GET, POST, PUT, and DELETE.&lt;/p&gt;

&lt;p&gt;The beauty of REST lies in the simplicity and wide adoption. It is almost like a trusty toolbox that everyone knows how to use – a huge amount of documentation, libraries, and tools are available, and pretty much any developer you hire will at least have experience with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  …but REST isn't perfect….
&lt;/h2&gt;

&lt;p&gt;The probably largest disadvantage of REST at this point is that you always end up either over- or under-fetching data. You either get too much information back at a time, wasting bandwidth and processor cycles, or it makes a bunch of requests to get all the data you want. It's really a pain, especially when it comes to bandwidth-hungry mobile apps.&lt;/p&gt;

&lt;p&gt;Another annoyance is the rigidity of REST endpoints. You need to define them up front, and when your front-end requirements change, you have the potential pain of redesigning your API.&lt;/p&gt;

&lt;h2&gt;
  
  
  GraphQL: The Shiny New Toy
&lt;/h2&gt;

&lt;p&gt;GraphQL was born out of frustrations at Facebook with the limitations of REST. It's a query language that lets one request exactly what is needed and nothing more or less. You basically define a schema that describes your data, and then clients can send in queries to retrieve specific information as per their requirements.&lt;/p&gt;

&lt;p&gt;The really cool thing about GraphQL is that there's only one endpoint for all of your requests. That could help a lot in reducing the number of round trips to the server, making your application snappier. And the strongly typed schema allows you to catch errors way too early; it also makes it easier to maintain your API.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hold Your Horses, GraphQL Ain't All Sunshine and Rainbows
&lt;/h3&gt;

&lt;p&gt;While GraphQL is undoubtedly very powerful, it comes with its set of challenges. First of all, there is a &lt;strong&gt;bigger learning curve&lt;/strong&gt; since you need to understand the concept of schemas, queries, and mutations, which might become overwhelming at the very start.&lt;/p&gt;

&lt;p&gt;Another problem is &lt;strong&gt;caching&lt;/strong&gt;. Since GraphQL queries are capable of being so dynamic, having efficient caching strategies in place is trickier compared to REST.&lt;/p&gt;

&lt;p&gt;And then there's the performance factor. &lt;strong&gt;Unless you're careful&lt;/strong&gt;, complex GraphQL queries can really tax your server.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Smackdown: When to Choose Which
&lt;/h2&gt;

&lt;p&gt;So, when should you go with GraphQL and when should you stick with REST? Here's my opinion, but don't worry - I love a good argument about this:&lt;/p&gt;

&lt;h2&gt;
  
  
  Use GraphQL if:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Your app has complicated data requirements.&lt;/li&gt;
&lt;li&gt;You're developing a mobile app and you want to reduce the number of network requests.&lt;/li&gt;
&lt;li&gt;You're using microservices architecture.&lt;/li&gt;
&lt;li&gt;Your frontend requirements are changing all the time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Stick with REST if:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You have a small API with simple data requirements. &lt;/li&gt;
&lt;li&gt;You are building a public API that must be easily consumed by a broad base of external developers. &lt;/li&gt;
&lt;li&gt;Caching is of the highest priority for your app. &lt;/li&gt;
&lt;li&gt;The Hybrid Option &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, there's no rule saying you must pick one or the other. In many cases, a hybrid approach that combines GraphQL and REST might be the perfect solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now it's your turn!
&lt;/h2&gt;

&lt;p&gt;On a final note, now that I have shared my two cents, I would be interested in hearing from you on what you think. So, have you tried GraphQL or REST in your projects? What was your feeling? Have you had any unexpected challenges? Maybe you just have a bit of a completely different angle about this whole debate.&lt;/p&gt;

&lt;p&gt;Drop a line in the comments below, or hit me up on (&lt;a href="mailto:kareem_khaled@t-horizons.com"&gt;kareem_khaled@t-horizons.com&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Let's start this chat!&lt;/p&gt;

</description>
      <category>api</category>
      <category>restapi</category>
      <category>graphql</category>
    </item>
  </channel>
</rss>
