29
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.
In this first episode, ffrey 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.
Here are discussed the Laravel installation requirements and tools Laravel depends on.
composer.json
in the root of your project.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.cd Laravel-From-Scratch-Blog-Project
and run the artisan command php artisan serve
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
.composer global require laravel/installer
.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
http://<project-folder>.test
using Laravel Valet. How does a Laravel app turn a URI request to a response? Through Routes
route::get('/', function() {
return view("welcome);
});
/
is always meant for homepage) is received, a function is called which returns a view (HTML) whose name is welcome..blade.php
extension with the view file welcome in the route definition. It is because Laravel automatically recognize it as a blade file.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
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.
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.Route::get('/posts/{post}', function ($slug) {
//
})->where('post', '[a-zA-Z\-]+');
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
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));
Thank you for following along with me. Any suggestions/advice for improvement will be appreciated.
~ Happy Coding
29