DEV Community

Arif Iqbal
Arif Iqbal

Posted on • Updated on

Week 1 of 100DaysOfCode Laravel Challenge

I already have been working in Laravel for some time but I wanted to go through all of the Laravel concepts from scratch, to enhance my knowledge and experience. I chose the Laracasts Laravel 8 from scratch series to follow along the #100DaysOfCode. The outcome of the course will be a working Blog project. I created a blank Laravel project and pushed it to Github. As we follow the course, we will turn this blank project into a working Blog.

The coding challenge's basic rule is to code for one hour every day for 100 consecutive days. It further suggests to announce this commitment publicly on Twitter to the fellow coder community. It will help you in holding yourself accountable to successfully complete the challenge.

I started the challenge on Nov 22, 2021. I code for one hour every day and log it to my fork of the GitHub repository. I also post to twitter on my progress every day, follow me to stay up to date. On Sunday I write a recap of my week here on DEV.

So, here are some highlights from my first week.

Episode 1: An Animated Introduction to MVC

In this first episode, Jeffrey Way explains the Laravel MVC architecture with the help of an animated video.

Laravel is a Model View Controller (MVC) framework. In Laravel, we can bind a request URI to a particular controller class using the Laravel Routes. Controller receives a request and provides the response to the View after delegating to the Eloquent Model. Laravel Eloquent Model contains the domain specific logic or the business logic and provides a nice API for running any number of SQL queries against your database. View is what your website visitors see in their browser. It is the HTML (CSS/JavaScript) part of your codebase. View receives data from the Controller.

Episode 2: Initial Environment Setup and Composer

Here are discussed the Laravel installation requirements and tools Laravel depends on.

  • Initial environment setup includes choosing a code editor of your choice like VS Code or PHPStorm.
  • Installing PHP and MySQL on your machine. Alternatively, you can use pre-packed solutions like XAMPP or WAMP. Other options include installing Laravel on Sail, which is a Docker container, or installing it on Laravel Homestead, which is a pre-packaged Vagrant Box for Virtual Machines.
  • Installing Composer on your machine. Composer is PHP package manager like Homebrew for Mac. Your composer dependencies are stored in a file composer.json in the root of your project.
  • When Composer is installed, run the command composer create-project laravel/laravel Laravel-From-Scratch-Blog-Project in a directory/folder where you would like to place your project. Understand the structure of this command. Here laravel/laravel is the GitHub repo name and the final parameter is how you would like to name your project.
  • Enter into your project folder by cd Laravel-From-Scratch-Blog-Project and run the artisan command php artisan serve
  • Now your application will be available in your browser at the URL http://127.0.0.1:8000

Episode 3: The Laravel Installer Tool

In the previous section we saw how to install Laravel through Composer using the composer create-project command. An alternate solution is to install the Laravel installer tool and then create projects with even simpler command Laravel new PROJECT_NAME.

  • Laravel Installer is a composer package that we install as a global composer dependency using the command composer global require laravel/installer.
  • In order for the installer tool to work from anywhere on your machine, you must include the composer vendor/bin directory to your system $PATH variable. It means the Installer will then work globally and will be not tied to a particular project folder.
  • cd into your project folder and run the command php artisan serve. Now see your app in the browser at http://127.0.0.1:8000
  • Mac users can access the app at http://<project-folder>.test using Laravel Valet.
  • Tweet

Episode 4: Why Do We Use Tools

In this episode is discussed the aim of the this tutorial series, a working blog project. Here is the completed project on Github Jeffrey Way has uploaded. And here is the link to my project, I started from scratch and push daily to it.

Episode 5: How a Route Loads a View

How does a Laravel app turn a URI request to a response? Through Routes

  • routes/web.php file contains your browser specific routes
  • Route syntax
route::get('/', function() {
    return view("welcome);
});
Enter fullscreen mode Exit fullscreen mode
  • The syntax is quite self-explanatory. It says when a request for the homepage (forward slash / is always meant for homepage) is received, a function is called which returns a view (HTML) whose name is welcome.
  • Blade is Laravel templating engine used for writing views files. Blade files are placed in the resources/views directory. So, our welcome view is located at resources/views/welcome.blade.php.
  • Note that we have not written the .blade.php extension with the view file welcome in the route definition. It is because Laravel automatically recognize it as a blade file.
  • Instead of a view, we can also return a string, an array or any other type from the route definition.

Episode 6: Include CSS and JavaScript

  • In real Laravel apps we use bundling tools like Laravel Mix for compiling CSS and JavaScript files. But at this stage we create CSS and JS files directly in the public folder and then include in our welcome.blade.php

  • Contributed to the Blog project

  • Tweet

Episode 7: Make a Route and Link to it

  • Here we start building the basic structure of our blog. A blog page that lists blog posts titles and excerpts. Blog titles are linked to pages that contains the full post.

  • Contributed to the blog project

  • Tweet

Episode 8: Store Blog Posts as HTML Files

Our Blog project will pull the blog posts from a MySQL database. But we have not touched that part yet. So, for now, we will save our blog posts in HTML files and then get the contents from it using the PHP file_get_contents() function. We use route wildcard to find which file to pick and pass to the view.

Episode 9: Route Wildcard Constraints

  • A wildcard is a placeholder for literally anything. You can pass any random string in a wildcard URL. That's something you will not always want. We can put some constraints on the wildcard so that it accepts only a specific set of characters and denies the rest. This is called route wildcard constraints. For the constraints we can use regular expressions or Laravel helper functions, which, behind the scenes, also use regular expressions.
Route::get('/posts/{post}', function ($slug) {
    //
})->where('post', '[a-zA-Z\-]+');
Enter fullscreen mode Exit fullscreen mode
  • Here the route wildcard {post} will accept only alphabets, both lowercase and uppercase, and a hyphen. The plus sign means one or more.

  • You can find other helper functions like whereAlpha(), whereAlphaNumeric(), and whereNumber

  • Contributed to the Blog project

  • Tweet

Episode 10: Use Caching for Expensive Operations

Blog posts usually don't get changed very frequently. So, reaching the file_get_contents() each time a blog post is viewed seems wasteful, particularly if a lot of users access your blog at the same time. So, why not cache the blog posts for performance?

/**
 * Closure function
 */
$post = cache()->remember('posts'.$slug, 1200, function() use ($path) {
    return file_get_contents($path);
});

/**
 * Or using an arrow function like this (PHP7.4 or above)
 */
$post = cache()->remember('posts'.$slug, 1200, fn() => file_get_contents($path));
Enter fullscreen mode Exit fullscreen mode

Thank you for following along with me. Any suggestions/advice for improvement will be appreciated.

~ Happy Coding

Top comments (0)