<?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: Iwobi Okwudili Frank </title>
    <description>The latest articles on DEV Community by Iwobi Okwudili Frank  (@frankliniwobi).</description>
    <link>https://dev.to/frankliniwobi</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%2F1386243%2Fc0131b20-3547-4ad0-a6a8-05cf65c37e69.jpg</url>
      <title>DEV Community: Iwobi Okwudili Frank </title>
      <link>https://dev.to/frankliniwobi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/frankliniwobi"/>
    <language>en</language>
    <item>
      <title>Mailpit and Laravel Integration: A Complete Guide for Local Email Testing</title>
      <dc:creator>Iwobi Okwudili Frank </dc:creator>
      <pubDate>Sat, 26 Oct 2024 11:20:52 +0000</pubDate>
      <link>https://dev.to/frankliniwobi/mailpit-and-laravel-integration-a-complete-guide-for-local-email-testing-3il7</link>
      <guid>https://dev.to/frankliniwobi/mailpit-and-laravel-integration-a-complete-guide-for-local-email-testing-3il7</guid>
      <description>&lt;p&gt;&lt;a href="https://mailpit.axllent.org/" rel="noopener noreferrer"&gt;Mailpit&lt;/a&gt; is a local email testing tool, similar to MailHog or MailCatcher, used by developers to capture and inspect emails sent by an application during development. It provides a web-based interface where you can view, search, and debug email messages without actually sending them to a live email server.&lt;/p&gt;

&lt;p&gt;It is a very useful and developer friendly tool but there is no adequate documentation on how to set it up in a Laravel app. That's  what we'll do today.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Setup
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Install Mailpit&lt;/strong&gt;: You can install it via Homebrew on macOS.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   brew &lt;span class="nb"&gt;install &lt;/span&gt;mailpit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are on Windows or Linux you can download the static binary. Read &lt;a href="https://mailpit.axllent.org/docs/install/" rel="noopener noreferrer"&gt;this guide&lt;/a&gt; for more information.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Run Mailpit&lt;/strong&gt;:
Start the Mailpit server by running:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   mailpit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or set it up to run automatically by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   brew services start mailpit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Access Mailpit Web Interface&lt;/strong&gt;:
Open your browser and go to &lt;code&gt;http://localhost:8025&lt;/code&gt; to view incoming emails in the Mailpit UI.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Laravel Setup
&lt;/h3&gt;

&lt;p&gt;Go to you &lt;code&gt;.env&lt;/code&gt; file and input this setup&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MAIL_MAILER=smtp
MAIL_HOST=localhost
MAIL_PORT=1025
MAIL_USERNAME=your username @ your system name here
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can get your user and system name by running the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;whoami&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;@&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;hostname&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all! Now whenever an email is sent from your laravel app you would be able to view it in the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F71ezlrkn5g1p10zrwsz7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F71ezlrkn5g1p10zrwsz7.png" alt="Final look" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>development</category>
      <category>testing</category>
    </item>
    <item>
      <title>Deploy Laravel Projects to Namecheap Server on Github Push with 3 Easy Steps (The only tutorial you would every need)</title>
      <dc:creator>Iwobi Okwudili Frank </dc:creator>
      <pubDate>Sat, 27 Jul 2024 17:42:28 +0000</pubDate>
      <link>https://dev.to/frankliniwobi/deploy-laravel-projects-to-namecheap-server-on-github-push-with-3-easy-steps-the-only-tutorial-you-would-every-need-a8h</link>
      <guid>https://dev.to/frankliniwobi/deploy-laravel-projects-to-namecheap-server-on-github-push-with-3-easy-steps-the-only-tutorial-you-would-every-need-a8h</guid>
      <description>&lt;p&gt;Alright, I know the title is a bit cliche 😌 but you're about to find out why!&lt;/p&gt;

&lt;p&gt;I've been a Laravel Developer for 3 years now, I've built some projects to test my skills during these times but lately I have this need to showcase other people what I've been up to and also have some projects on &lt;a href="https://frankliniwobi.vercel.app" rel="noopener noreferrer"&gt;My Portfolio&lt;/a&gt;. Since these projects may get updates regularly and I definately do not want to always re-upload to my Namecheap Server every single time I make a change, I found a way to make Laravel Deployment to Namecheap work as seamlessly as VueJs to Vercel.&lt;/p&gt;

&lt;p&gt;You may ask why I chose Namecheap and not other Hosting providers like Hostinger or Server Managemment tools like Laravel Forge and Plio.io that already have this functionality baked in, well, simple answer, "I'm broke 🥲", and I do what broke people do, find the cheapest alternative. Enough about me, let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step One - Set Up Git On Namecheap Server.
&lt;/h2&gt;

&lt;p&gt;In order for this post not to get too long, i'll assume you already:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Registered a &lt;a href="https://namecheap.com" rel="noopener noreferrer"&gt;Namecheap Account&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Bought a Hosting Account (I use shared hosting).&lt;/li&gt;
&lt;li&gt;Have a domain setup on the hosting account, you can follow (&lt;a href="https://www.namecheap.com/support/knowledgebase/article.aspx/10237/2255/video-how-to-connect-a-domain-name-to-your-shared-hosting/" rel="noopener noreferrer"&gt;this guide&lt;/a&gt;), if you have an existing hosting server, you can check out &lt;a href="https://www.namecheap.com/support/knowledgebase/article.aspx/897/29/how-to-create-an-addon-domain/" rel="noopener noreferrer"&gt;this guide&lt;/a&gt; to add a domain as an addon to a server.&lt;/li&gt;
&lt;li&gt;Have a Github Account.&lt;/li&gt;
&lt;li&gt;Have a Laravel Project you want to host.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With all these set up, we're good to go.&lt;/p&gt;

&lt;p&gt;Log into your Namecheap Hosting Server and visit the "Terminal" tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbd8g0h6uundbn8lr4kk1.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbd8g0h6uundbn8lr4kk1.jpeg" alt="Terminal Tab" width="800" height="503"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also search for it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft3ukiu3adpqrjyr8w3oe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft3ukiu3adpqrjyr8w3oe.png" alt="Search terminal tab" width="800" height="579"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If for some reason you do not have the Terminal Tab, go to the Manage Shell Tab and make sure the enable ssh access is toggled on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdoyowk5q5e05p0p582g1.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdoyowk5q5e05p0p582g1.jpeg" alt="shell" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F46v2tlfbt78bkaka95kf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F46v2tlfbt78bkaka95kf.png" alt="ssh assess" width="800" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Haven done that, you can now visit the terminal tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd9xwe4ygdaggj2ons8va.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd9xwe4ygdaggj2ons8va.jpeg" alt="terminal tab" width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Git is already installed on all Namecheap Hosting Servers but to confirm you can run this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure your Git version is &lt;strong&gt;at least 2.28&lt;/strong&gt; before proceeding. Update Git if it isn't.&lt;/p&gt;

&lt;p&gt;Now it's time link your Github account to your Namecheap Server. You should first set up your local profile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.name &lt;span class="s2"&gt;"Your Name"&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.email &lt;span class="s2"&gt;"yourname@example.com"&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;replace with your name and the email address you use for Github&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you use private GitHub email address, the second command will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.email &lt;span class="s2"&gt;"123456789+franklin@users.noreply.github.com"&lt;/span&gt; &lt;span class="c"&gt;# Remember to use your own private GitHub email here. &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GitHub recently changed the default branch on new repositories from master to main. Change the default branch for Git using this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; init.defaultBranch main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To enable colorful output with git, type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; color.ui auto
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll also likely want to set your default branch reconciliation behavior to merging.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; pull.rebase &lt;span class="nb"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To verify that things are working properly, enter these commands and verify whether the output matches your name and email address.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--get&lt;/span&gt; user.name
git config &lt;span class="nt"&gt;--get&lt;/span&gt; user.email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create an SSH Key&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An SSH key is a cryptographically secure identifier. It’s like a really long password used to identify your machine. GitHub uses SSH keys to allow you to upload to your repository without having to type in your username and password every time.&lt;/p&gt;

&lt;p&gt;First, we need to see if you have an Ed25519 algorithm SSH key already installed. Type this into the terminal and check the output with the information below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; ~/.ssh/id_ed25519.pub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a message appears in the console containing the text “No such file or directory”, then you do not yet have an Ed25519 SSH key, and you will need to create one. If no such message has appeared in the console output, you can skip the next step.&lt;/p&gt;

&lt;p&gt;To create a new SSH key, run the following command inside your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh-keygen &lt;span class="nt"&gt;-t&lt;/span&gt; ed25519
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When it prompts you for a location to save the generated key, just push &lt;code&gt;Enter&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Next, it will ask you for a password; enter one if you wish, but it’s not required.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Link your SSH key with GitHub&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, you need to tell GitHub what your SSH key is so that you can push your code without typing in a password every time.&lt;/p&gt;

&lt;p&gt;First, you’ll navigate to where GitHub receives our SSH key. Log into GitHub and click on your profile picture in the top right corner. Then, click on &lt;code&gt;Settings&lt;/code&gt; in the drop-down menu.&lt;/p&gt;

&lt;p&gt;Next, on the left-hand side, click &lt;code&gt;SSH and GPG keys&lt;/code&gt;. Then, click the green button in the top right corner that says &lt;code&gt;New SSH Key&lt;/code&gt;. Name your key something that is descriptive enough for you to remember what device this SSH key came from, for example &lt;code&gt;Namecheap Server&lt;/code&gt;. Leave this window open while you do the next steps.&lt;/p&gt;

&lt;p&gt;Now you need to copy your public SSH key. To do this, we’re going to use a command called &lt;code&gt;cat&lt;/code&gt; to read the file to the console. (Note that the &lt;code&gt;.pub&lt;/code&gt; file extension is important in this case.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; ~/.ssh/id_ed25519.pub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Highlight and copy the entire output from the command. If you followed the instructions above, the output will likely begin with &lt;code&gt;ssh-ed25519&lt;/code&gt; and end with your &lt;code&gt;username@hostname&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, go back to GitHub in your browser window and paste the key you copied into the key field. Keep the key type as &lt;code&gt;Authentication Key&lt;/code&gt; and then, click &lt;code&gt;Add SSH key&lt;/code&gt;. You’re done! You’ve successfully added your SSH key!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Testing you key&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Follow the &lt;a href="https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection?platform=linux" rel="noopener noreferrer"&gt;GitHub directions for testing your SSH connection.&lt;/a&gt; Make sure the fingerprint output in the terminal matches &lt;a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/githubs-ssh-key-fingerprints" rel="noopener noreferrer"&gt;one of the four GitHub’s public fingerprints.&lt;/a&gt; &lt;strong&gt;(Don’t forget to omit the &lt;code&gt;$&lt;/code&gt; when you copy and paste the code!).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You should see this response in your terminal: &lt;strong&gt;Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.&lt;/strong&gt; Don’t let GitHub’s lack of providing shell access trouble you. If you see this message, you’ve successfully added your SSH key and you can move on. If the output doesn’t correctly match up, then try going through these steps again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step Two - Clone Laravel Project To Your Namecheap Server.
&lt;/h2&gt;

&lt;p&gt;Search for "SSH Access" on your cPanel and click on the first result. Click on &lt;code&gt;Manage SSH Keys&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F42vfgvrpitlwidjoxk70.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F42vfgvrpitlwidjoxk70.jpeg" alt="ssh access" width="800" height="526"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Find the SSH key you just generated (it should the only one for you, or last one), click and &lt;code&gt;manage&lt;/code&gt;, and &lt;code&gt;Authorize&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, search for &lt;code&gt;Git version control&lt;/code&gt; and open the first option. Click on &lt;code&gt;Create&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fartuokpdfana6k2af75r.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fartuokpdfana6k2af75r.jpeg" alt="clone repo" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Set the clone Repo URL and set the Repository Path (directory to clone your Github Repo).&lt;/p&gt;

&lt;p&gt;Set the Repository Name and click create.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember that the directory you are cloning to must be empty, else you would get an error.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If everything goes well you should have something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd47d8j2ompwhqopd8ay5.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd47d8j2ompwhqopd8ay5.jpeg" alt="cloned" width="800" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you can easily go to your terminal, &lt;code&gt;cd&lt;/code&gt; into your project directory and run &lt;code&gt;composer install&lt;/code&gt; and all those other Laravel commands but where's the fun in that 😁&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Step - Set Up Deploy On Push.
&lt;/h2&gt;

&lt;p&gt;This is the topping on the ice, for this we'll use Github Webhooks and good ole' &lt;code&gt;.htaccess&lt;/code&gt; file. Let's see how.&lt;/p&gt;

&lt;p&gt;Laravel has a thing where it use the public directory as the root directory, this way, sensitive files and directory are secured, database management tools like Laravel Forge does this for you automatically but we'll have to it ourselves.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To get your project up and ready, you should create a &lt;code&gt;.env&lt;/code&gt; file on the root of your project directory and setup Application Key and every other thing you would normally do in a typical Laravel app.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, you should create a &lt;code&gt;.htaccess&lt;/code&gt; file on the root directory. Then paste the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt;IfModule mod_rewrite.c&amp;gt;
    RewriteEngine On

    &lt;span class="c"&gt;# Redirect Trailing Slashes If Not A Folder...&lt;/span&gt;
    RewriteCond %&lt;span class="o"&gt;{&lt;/span&gt;REQUEST_FILENAME&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nt"&gt;-d&lt;/span&gt;
    RewriteRule ^&lt;span class="o"&gt;(&lt;/span&gt;.&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;/&lt;span class="nv"&gt;$ &lt;/span&gt;/&lt;span class="nv"&gt;$1&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;L,R&lt;span class="o"&gt;=&lt;/span&gt;301]

    &lt;span class="c"&gt;# Ensure Authorization header is passed to PHP&lt;/span&gt;
    RewriteCond %&lt;span class="o"&gt;{&lt;/span&gt;HTTP:Authorization&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
    RewriteRule .&lt;span class="k"&gt;*&lt;/span&gt; - &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;E&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;HTTP_AUTHORIZATION:%&lt;span class="o"&gt;{&lt;/span&gt;HTTP:Authorization&lt;span class="o"&gt;}]&lt;/span&gt;

    &lt;span class="c"&gt;# Redirect to the public folder&lt;/span&gt;
    RewriteCond %&lt;span class="o"&gt;{&lt;/span&gt;REQUEST_URI&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;^/public/
    RewriteRule ^&lt;span class="o"&gt;(&lt;/span&gt;.&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;/public/&lt;span class="nv"&gt;$1&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;L]
&amp;lt;/IfModule&amp;gt;

&lt;span class="c"&gt;# Deny access to .env file&lt;/span&gt;
&amp;lt;Files .env&amp;gt;
    Order allow,deny
    Deny from all
&amp;lt;/Files&amp;gt;

&lt;span class="c"&gt;# Deny access to composer.json and composer.lock&lt;/span&gt;
&amp;lt;FilesMatch &lt;span class="s2"&gt;"^composer&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="s2"&gt;(json|lock)$"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    Order allow,deny
    Deny from all
&amp;lt;/FilesMatch&amp;gt;

&lt;span class="c"&gt;# Deny access to artisan&lt;/span&gt;
&amp;lt;Files artisan&amp;gt;
    Order allow,deny
    Deny from all
&amp;lt;/Files&amp;gt;

&lt;span class="c"&gt;# Deny access to anything starting with a dot&lt;/span&gt;
&amp;lt;Files ~ &lt;span class="s2"&gt;"^&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    Order allow,deny
    Deny from all
&amp;lt;/Files&amp;gt;

&lt;span class="c"&gt;# Prevent directory listing&lt;/span&gt;
Options &lt;span class="nt"&gt;-Indexes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This piece of code sets the public directory as the root directory and enables other security checks.&lt;/p&gt;

&lt;p&gt;The next thing we want is a public route that is accessible to the internet, however we do not want everyone to have access to this file, rather we want only the Github IP to access it, since we will be receiving events from there. We can also use a &lt;code&gt;.htaccess&lt;/code&gt; file for this.&lt;/p&gt;

&lt;p&gt;Go to the &lt;code&gt;public&lt;/code&gt; directory, create a file to store our deployment script, I would call mine &lt;code&gt;deployment.php&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc6n5yf3abmohmfz9men3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc6n5yf3abmohmfz9men3.png" alt="file" width="800" height="222"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Open the file and paste the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt;?php

// Your secret key &lt;span class="k"&gt;for &lt;/span&gt;verification
&lt;span class="nv"&gt;$secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'webhook password'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // you will use this when creating a webhook

// Get the payload from GitHub
&lt;span class="nv"&gt;$payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; file_get_contents&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'php://input'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$signature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_SERVER&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'HTTP_X_HUB_SIGNATURE'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Verify the payload signature
list&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$algo&lt;/span&gt;, &lt;span class="nv"&gt;$hash&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; explode&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'='&lt;/span&gt;, &lt;span class="nv"&gt;$signature&lt;/span&gt;, 2&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$hash&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; hash_hmac&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$algo&lt;/span&gt;, &lt;span class="nv"&gt;$payload&lt;/span&gt;, &lt;span class="nv"&gt;$secret&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    header&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'HTTP/1.1 403 Forbidden'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


// Define the project root directory
&lt;span class="nv"&gt;$projectRoot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'/home/servername/projectdirectory'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Pull the latest changes from the repository
&lt;span class="nv"&gt;$output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; shell_exec&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cd &lt;/span&gt;&lt;span class="nv"&gt;$projectRoot&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; git pull 2&amp;gt;&amp;amp;1"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;pre&amp;gt;Git Pull Output:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="nv"&gt;$output&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/pre&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Run composer &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;span class="nv"&gt;$output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; shell_exec&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cd &lt;/span&gt;&lt;span class="nv"&gt;$projectRoot&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; composer install --no-interaction --prefer-dist --optimize-autoloader 2&amp;gt;&amp;amp;1"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;pre&amp;gt;Composer Install Output:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="nv"&gt;$output&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/pre&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Run artisan commands
&lt;span class="nv"&gt;$output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; shell_exec&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cd &lt;/span&gt;&lt;span class="nv"&gt;$projectRoot&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; php artisan optimize:clear 2&amp;gt;&amp;amp;1"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;pre&amp;gt;Artisan Output:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="nv"&gt;$output&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/pre&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; shell_exec&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cd &lt;/span&gt;&lt;span class="nv"&gt;$projectRoot&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; php artisan optimize 2&amp;gt;&amp;amp;1"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;pre&amp;gt;Artisan Output:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="nv"&gt;$output&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/pre&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; shell_exec&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cd &lt;/span&gt;&lt;span class="nv"&gt;$projectRoot&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; php artisan migrate 2&amp;gt;&amp;amp;1"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;pre&amp;gt;Artisan Output:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="nv"&gt;$output&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/pre&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; shell_exec&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cd &lt;/span&gt;&lt;span class="nv"&gt;$projectRoot&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; php artisan storage:link 2&amp;gt;&amp;amp;1"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;pre&amp;gt;Artisan Output:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="nv"&gt;$output&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/pre&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can edit the artisan commands to meet your personal needs.&lt;/p&gt;

&lt;p&gt;The last thing we need to do before moving over to Github is to protect the &lt;code&gt;deployment.php&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;There is a &lt;code&gt;.htaccess&lt;/code&gt; file on the public directory, it comes with all Laravel installation. That's where we would do that. Open the file and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt;Files &lt;span class="s2"&gt;"deployment.php"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    Order allow,deny
    Deny from all
    Allow from 192.30.252.0/22
    Allow from 185.199.108.0/22
    Allow from 140.82.112.0/20
&amp;lt;/Files&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the whole file content should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt;IfModule mod_rewrite.c&amp;gt;
    &amp;lt;IfModule mod_negotiation.c&amp;gt;
        Options &lt;span class="nt"&gt;-MultiViews&lt;/span&gt; &lt;span class="nt"&gt;-Indexes&lt;/span&gt;
    &amp;lt;/IfModule&amp;gt;

    RewriteEngine On

    &lt;span class="c"&gt;# Handle Authorization Header&lt;/span&gt;
    RewriteCond %&lt;span class="o"&gt;{&lt;/span&gt;HTTP:Authorization&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
    RewriteRule .&lt;span class="k"&gt;*&lt;/span&gt; - &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;E&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;HTTP_AUTHORIZATION:%&lt;span class="o"&gt;{&lt;/span&gt;HTTP:Authorization&lt;span class="o"&gt;}]&lt;/span&gt;

    &lt;span class="c"&gt;# Redirect Trailing Slashes If Not A Folder...&lt;/span&gt;
    RewriteCond %&lt;span class="o"&gt;{&lt;/span&gt;REQUEST_FILENAME&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nt"&gt;-d&lt;/span&gt;
    RewriteCond %&lt;span class="o"&gt;{&lt;/span&gt;REQUEST_URI&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;.+&lt;span class="o"&gt;)&lt;/span&gt;/&lt;span class="err"&gt;$&lt;/span&gt;
    RewriteRule ^ %1 &lt;span class="o"&gt;[&lt;/span&gt;L,R&lt;span class="o"&gt;=&lt;/span&gt;301]

    &lt;span class="c"&gt;# Send Requests To Front Controller...&lt;/span&gt;
    RewriteCond %&lt;span class="o"&gt;{&lt;/span&gt;REQUEST_FILENAME&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nt"&gt;-d&lt;/span&gt;
    RewriteCond %&lt;span class="o"&gt;{&lt;/span&gt;REQUEST_FILENAME&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nt"&gt;-f&lt;/span&gt;
    RewriteRule ^ index.php &lt;span class="o"&gt;[&lt;/span&gt;L]
&amp;lt;/IfModule&amp;gt;

&amp;lt;Files &lt;span class="s2"&gt;"deployment.php"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    Order allow,deny
    Deny from all
    Allow from 192.30.252.0/22
    Allow from 185.199.108.0/22
    Allow from 140.82.112.0/20
&amp;lt;/Files&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Set up Github Webhook&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Log into your Github account and open your project repository. Go to &lt;code&gt;Settings&lt;/code&gt; and click on &lt;code&gt;Webhooks&lt;/code&gt;, located on the left side panel, press &lt;strong&gt;Add New&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Provide all required informations and proceed to add webhook. The payload type should be application/json and the webhook url should be a link to the deployment script on your server. Here's mine:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2w5yikerwru2mpkf6z6u.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2w5yikerwru2mpkf6z6u.jpeg" alt="file" width="800" height="605"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all! 🥳&lt;/p&gt;

&lt;p&gt;Now, whenever you push to your Github Repository, the code automatically deploys to the server.&lt;/p&gt;

&lt;p&gt;If you did everything correctly, the webhook will look like this on your next push.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffk06syt9yod16axfmk7u.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffk06syt9yod16axfmk7u.jpeg" alt="final" width="800" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have any issues or questions you can comment below and I'll try to help as best as I can.&lt;/p&gt;

&lt;p&gt;You can support by following me on &lt;a href="https://x.com/@OkwudiliTweet" rel="noopener noreferrer"&gt;X (formally Twitter)&lt;/a&gt;. Thank you.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>hosting</category>
      <category>namecheap</category>
    </item>
    <item>
      <title>Mastering Global Functions in Laravel: Easy Methods for Versions 8,9,10,11.</title>
      <dc:creator>Iwobi Okwudili Frank </dc:creator>
      <pubDate>Thu, 04 Apr 2024 18:15:16 +0000</pubDate>
      <link>https://dev.to/frankliniwobi/mastering-global-functions-in-laravel-easy-methods-for-versions-891011-4e02</link>
      <guid>https://dev.to/frankliniwobi/mastering-global-functions-in-laravel-easy-methods-for-versions-891011-4e02</guid>
      <description>&lt;p&gt;If you've ever found yourself seeking for efficient ways to have global functions in your Laravel apps you're in the right place. Global functions offer a convenient way to enhance the functionality and productivity of your Laravel apps as you only have to write a function once and have access to it in any part of your app, in this comprehensive tutorial, we'll explore the easiest methods to achieve this. Whether you're a seasoned Laravel developer or just starting your journey, this guide will equip you with the knowledge and tools to leverage global functions effectively, making your development experience smoother and more efficient. Let's dive in!&lt;/p&gt;

&lt;p&gt;For this example, we'll be displaying the &lt;code&gt;user count&lt;/code&gt; on different pages, it may not a very good example but it would give you an idea on how to implement such functionality in you own app. Let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Get your Laravel App running.
&lt;/h2&gt;

&lt;p&gt;In this step we will install a fresh Laravel project using the Laravel installer, you can also use composer or any other means you use to get your Laravel applications up and running.&lt;/p&gt;

&lt;p&gt;As at the time of writing this post, Laravel is on version 11. You have to make sure you have a PHP version of at least 8.2. We will now create a Laravel project with the &lt;code&gt;new&lt;/code&gt; command:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4yd96r7jclrh6ne2re9n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4yd96r7jclrh6ne2re9n.png" alt="laravel new command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I would not be using any starter kit for this example.&lt;/p&gt;

&lt;p&gt;After installation, let's open the project on our code editor, I'm using Virtual Studio Code. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4k9sswonpn07nln58kl5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4k9sswonpn07nln58kl5.png" alt="Project setup in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Serve the application and open on the browser:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

php artisan serve


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqufn4enudw12hdniojg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqufn4enudw12hdniojg.png" alt="bash"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnc11ip85hmhx9t75p122.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnc11ip85hmhx9t75p122.png" alt="Served application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's get to the main job!
&lt;/h2&gt;

&lt;p&gt;Now that our Laravel app is running, we can now start writing code. For the sake of this example, I'll populate the database with 35 users using the default UserFactory that comes with every Laravel installation. I'll use tinker for ease.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxplvlp612oy4ubfm69x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxplvlp612oy4ubfm69x.png" alt="tinker session"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that let's first display the users count on the home page, let's delete everything on the welcome.blade.php file and write this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

Total &lt;span class="nb"&gt;users&lt;/span&gt;: &lt;span class="o"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;$users&lt;/span&gt; &lt;span class="o"&gt;}}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;While on the web.php file, we do this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&amp;lt;?php

use App&lt;span class="se"&gt;\M&lt;/span&gt;odels&lt;span class="se"&gt;\U&lt;/span&gt;ser&lt;span class="p"&gt;;&lt;/span&gt;
use Illuminate&lt;span class="se"&gt;\S&lt;/span&gt;upport&lt;span class="se"&gt;\F&lt;/span&gt;acades&lt;span class="se"&gt;\R&lt;/span&gt;oute&lt;span class="p"&gt;;&lt;/span&gt;

Route::get&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/'&lt;/span&gt;, &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;view&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'welcome'&lt;/span&gt;, &lt;span class="o"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'users'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; User::count&lt;span class="o"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Our home page should look like this now:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc8flrkr0bzmtj92briug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc8flrkr0bzmtj92briug.png" alt="home page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can now see that if we want to see a count of users on different pages we would have to repeat this line of code on both the view file and route file, this is a lot of work. Let's create a Global Function. There are so many ways to create a global function in Laravel but the best way is using a Service Provider. We'll create it through the terminal, you can name it what ever you want but I'll call mine &lt;code&gt;GlobalFunctionsServiceProvider&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

php artisan make:provider GlobalFunctionsServiceProvider


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This command creates a file on the App\Providers directory. Note that in Laravel 11, you do not need to register this provider in the app.php providers array since it is auto-discovered by Laravel but if you're using a lower version of Laravel you would need to do so. Just open the Config/app.php file and add it to the providers array like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="s1"&gt;'providers'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; ServiceProvider::defaultProviders&lt;span class="o"&gt;()&lt;/span&gt;-&amp;gt;merge&lt;span class="o"&gt;([&lt;/span&gt;
        /&lt;span class="k"&gt;*&lt;/span&gt;
         &lt;span class="k"&gt;*&lt;/span&gt; Package Service Providers...
         &lt;span class="k"&gt;*&lt;/span&gt;/

        /&lt;span class="k"&gt;*&lt;/span&gt;
         &lt;span class="k"&gt;*&lt;/span&gt; Application Service Providers...
         &lt;span class="k"&gt;*&lt;/span&gt;/
        App&lt;span class="se"&gt;\P&lt;/span&gt;roviders&lt;span class="se"&gt;\A&lt;/span&gt;ppServiceProvider::class,
        App&lt;span class="se"&gt;\P&lt;/span&gt;roviders&lt;span class="se"&gt;\A&lt;/span&gt;uthServiceProvider::class,
        // App&lt;span class="se"&gt;\P&lt;/span&gt;roviders&lt;span class="se"&gt;\B&lt;/span&gt;roadcastServiceProvider::class,
        App&lt;span class="se"&gt;\P&lt;/span&gt;roviders&lt;span class="se"&gt;\E&lt;/span&gt;ventServiceProvider::class,
        App&lt;span class="se"&gt;\P&lt;/span&gt;roviders&lt;span class="se"&gt;\R&lt;/span&gt;outeServiceProvider::class,
        App&lt;span class="se"&gt;\P&lt;/span&gt;roviders&lt;span class="se"&gt;\G&lt;/span&gt;lobalFunctionsServiceProvider::class, //our custom provider
    &lt;span class="o"&gt;])&lt;/span&gt;-&amp;gt;toArray&lt;span class="o"&gt;()&lt;/span&gt;,


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: You do not need the above step if you're in Laravel 11&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now let's create a file to store our functions, I'll create mine in App\Functions\GlobalFunctions.php After that, open your GlobalFunctionsServiceProvider.php file and add the following code in the register method&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&amp;lt;?php

namespace App&lt;span class="se"&gt;\P&lt;/span&gt;roviders&lt;span class="p"&gt;;&lt;/span&gt;

use Illuminate&lt;span class="se"&gt;\S&lt;/span&gt;upport&lt;span class="se"&gt;\S&lt;/span&gt;erviceProvider&lt;span class="p"&gt;;&lt;/span&gt;

class GlobalFunctionsServiceProvider extends ServiceProvider
&lt;span class="o"&gt;{&lt;/span&gt;
    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; Register services.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    public &lt;span class="k"&gt;function &lt;/span&gt;register&lt;span class="o"&gt;()&lt;/span&gt;: void
    &lt;span class="o"&gt;{&lt;/span&gt;
        require_once base_path&lt;span class="o"&gt;()&lt;/span&gt;.&lt;span class="s1"&gt;'/app/Functions/GlobalFunctions.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
//remeber to point to the path of the file where your functions are
    &lt;span class="o"&gt;}&lt;/span&gt;

    /&lt;span class="k"&gt;**&lt;/span&gt;
     &lt;span class="k"&gt;*&lt;/span&gt; Bootstrap services.
     &lt;span class="k"&gt;*&lt;/span&gt;/
    public &lt;span class="k"&gt;function &lt;/span&gt;boot&lt;span class="o"&gt;()&lt;/span&gt;: void
    &lt;span class="o"&gt;{&lt;/span&gt;
        //
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now let's write our global function. Open your GlobalFunctions.php file and write the following code&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&amp;lt;?php

use App&lt;span class="se"&gt;\M&lt;/span&gt;odels&lt;span class="se"&gt;\U&lt;/span&gt;ser&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;function &lt;/span&gt;users_count&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;User::count&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We can now remove the users key from our web.php routes file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&amp;lt;?php

use Illuminate&lt;span class="se"&gt;\S&lt;/span&gt;upport&lt;span class="se"&gt;\F&lt;/span&gt;acades&lt;span class="se"&gt;\R&lt;/span&gt;oute&lt;span class="p"&gt;;&lt;/span&gt;

Route::get&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/'&lt;/span&gt;, &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;view&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'welcome'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And edit our welcome.blade.php file to use the function we wrote earlier&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

Total &lt;span class="nb"&gt;users&lt;/span&gt;: &lt;span class="o"&gt;{{&lt;/span&gt; users_count&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;}}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can see now that we have the same result on the browser! With this approach, you can call the &lt;code&gt;users_count()&lt;/code&gt; function from any part of your application, whether view, controller, class, config files, anywhere, and be certain to get the same result.&lt;/p&gt;

&lt;p&gt;You can customize the code to fit your use case, this was just an example.&lt;/p&gt;

&lt;p&gt;The source code for this example is on this &lt;a href="https://github.com/frankliniwobi/mastering-global-functions-in-laravel" rel="noopener noreferrer"&gt;Github repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you. Happy coding.&lt;/p&gt;

&lt;p&gt;You can &lt;a href="https://twitter.com/OkwudiliTweet" rel="noopener noreferrer"&gt;follow me on X(former twitter)&lt;/a&gt; if you found this article helpful.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
